Why does Java's Random() not work?

twitter logo github logo ・1 min read

I have this chunk of code for Dint, for parsing an int value in the language.... but it returns "" instead of a number in a string, like "3", why?
CHUNK:

if(tok.equals("0") || tok.equals("1") || tok.equals("2") || tok.equals("3") || tok.equals("4") || tok.equals("5") || tok.equals("6") || tok.equals("7") || tok.equals("8") || tok.equals("9")) {
    Number_string += tok;
    tok = "";
}
if(!(tok.equals("|") || tok.equals("<Random>"))) {
    tok = "";
}
if(tok.equals("<Random>")) {
    Random rand = new Random();
    Integer Rand_PNumber = rand.nextInt(100);
    Rand_PNumber += 1;
    Number_string = Rand_PNumber.toString();
    tok = "";
    Rand_PNumber = 0;
}
twitter logo DISCUSS (9)
markdown guide
 

Is the full file/method something you can link to? It's hard to help debug when the context is not there.

For example, I'm not sure what it refers to in your post, and we can't see what you're returning at all in there.

 

FILE:

public class Dint {
    public static void main(String[] args) {
        System.out.println("Welcome Dint ISE v0.1.1! use \"help\" for commands, and use \"changelog\" for recent changes!\n");
        Scanner Input = new Scanner(System.in);
        String In = Input.nextLine();
        boolean Program = true;
        String[] PFuncs = new String[500];
        String[] PFuncVals = new String[500];
        String[] PStrs = new String[1000];
        String[] PStrVals = new String[1000];
        String[] PInts = new String[1000];
        int[] PIntVals = new int[1000];
        parser DintCode = new parser();
        while(Program) {
            String tok = "";
            String string = "";
            String command = "";
            String Number_string = "";
            String FCS = "";
            boolean Pbool = false; 
            int Number_numberInt = 0;
            int[] numberS = new int[256];
            int state = 0;
            int castStringTo = 0;
            int castIntTo = 0;
            int castIntNameTo = 0;
            int castIntValTo = 0;
            int castStrNameTo = 0;
            int castStrValTo = 0;
            int castFuncNameTo = 0;
            int castFuncValTo = 0;
            String[] strings = new String[256];
            for(int i = 0; i < In.length();) {
                tok += In.charAt(i);
                if(state == 1) {
                    string += In.charAt(i);
                    if(tok.equals("\\\"")) {
                        string += "\"";
                    } else if(tok.equals("\\n")) {
                        string += "\n";
                    }
                    if(!(tok.equals("\""))) {
                        tok = "";
                    }
                } else if(state == 2) {
                    if(tok.equals("0") || tok.equals("1") || tok.equals("2") || tok.equals("3") || tok.equals("4") || tok.equals("5") || tok.equals("6") || tok.equals("7") || tok.equals("8") || tok.equals("9")) {
                        Number_string += tok;
                        tok = "";
                    }
                    if(!(tok.equals("|") || tok.equals("<Random>"))) {
                        tok = "";
                    }
                    if(tok.equals("<Random>")) {
                        Random rand = new Random();
                        Integer Rand_PNumber = rand.nextInt(100);
                        Rand_PNumber += 1;
                        Number_string = Rand_PNumber.toString();
                        tok = "";
                        Rand_PNumber = 0;
                    }
                } else if(state == 3) {
                    FCS += In.charAt(i);
                    if(!(tok == "}")) {
                        tok = "";
                    }
                }
                if(tok.equals(" ")) {
                    tok = "";
                } else if(tok.equals("\"") && (state == 0 || state == 1)) {
                    if(state == 0) {
                        state = 1;
                    } else if(state == 1) {
                        state = 0;
                        string = string.substring(0, string.length() - 1);
                        strings[castStringTo] = string;
                        castStringTo++;
                        string = "";
                    }
                    tok = "";
                } else if(tok.equals("|") && (state == 0 || state == 2)) {
                    if(state == 0) {
                        state = 2;
                    } else if(state == 2) {
                        try {
                            state = 0;
                            Number_numberInt = Integer.parseInt(Number_string);
                            numberS[castIntTo] = Number_numberInt;
                            castIntTo++;
                            Number_numberInt = 0;
                        } catch(Exception e) {
                            System.out.println("NoIntError: Tried to parse value <Int> but was returned with <String> = or <Null> = <Blank>\n <Value> = "+Number_string);
                        }
                        Number_string = "";
                    }
                    tok = "";
                } else if(tok.equals("{") && state == 0) {
                    if(state == 0) {
                        state = 3;
                    }
                } else if(tok.equals("}")) {
                    if(state == 3) {
                        state = 0;
                        FCS = FCS.substring(0, FCS.length() - 1);
                    }
                } else if(tok.equals("out") && state == 0) {
                    command = "print";
                    tok = "";
                } else if(tok.equals("help") && state == 0) {
                    command = "help";
                    tok = "";
                } else if(tok.equals("exit")) {
                    System.exit(0);
                } else if(tok.equals("int")) {
                    PInts[castIntNameTo] = strings[0];
                    PIntVals[castIntValTo] = numberS[0];
                    tok = "";
                } else if(tok.equals("String")) {
                    PStrs[castStrNameTo] = strings[0];
                    PStrVals[castStrValTo] = strings[1];
                    castStrNameTo++;
                    castStrValTo++;
                    tok = "";
                } else if(tok.equals("function")) {
                    PFuncs[castFuncNameTo] = strings[0];
                    PFuncVals[castFuncValTo] = FCS;
                    castFuncNameTo++;
                    castFuncValTo++;
                    FCS = "";
                    tok = "";
                } else if(tok.equals("@Functions")) {
                    for(int iterator = 0; iterator < PFuncs.length;) {
                        if(!(PFuncs[iterator] == null)) {
                            System.out.println("FUNCTION: \""+PFuncs[iterator]+"\" - CODE: "+PFuncVals[iterator]);
                        }
                        iterator++;
                    }
                    tok = "";
                } else if(tok.equals("if")) {
                    command = "if";
                    tok = "";
                } else if((tok.equals("true") || tok.equals("1")) && state == 0) {
                    Pbool = true;
                } else if((tok.equals("false") && tok.equals("0")) && state == 0) {
                    Pbool = false;
                } else if(tok.equals("@GlobalBoolean")) {
                command = "Bool";
                tok = "";
                } else if(tok.equals("@Variables")) {
                    for(int iterator = 0; iterator < PStrs.length;) {
                        if(!(PStrs == null)) {
                            System.out.println(PStrs[iterator]+": "+PStrVals[iterator]);
                        }
                        iterator++;
                    }
                    for(int iterator = 0; iterator < PInts.length;) {
                        if(!(PInts == null)) {
                            System.out.println(PInts[iterator]+": "+PIntVals[iterator]);
                        }
                        iterator++;
                    }
                } else if(tok.equals("clr") || tok.equals("cls")) {
                    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                } else if(tok.equals("@CodeBlock")) {
                    command = "SeeCB";
                    tok = "";
                } else if(tok.equals("<null>")) {
                    strings[castStringTo] = null;
                    castStringTo++;
                } else {
                    boolean somethingFound = false;
                    for(int iterator = 0; iterator < PInts.length;) {
                        if(tok.equals(PInts[iterator])) {
                            numberS[castIntTo] = PIntVals[iterator];
                            somethingFound = true;
                        }
                        iterator++;
                    }
                    for(int iterator = 0; iterator < PStrs.length;) {
                        if(tok.equals(PStrs[iterator])) {
                            strings[castStringTo] = PStrVals[iterator];
                            somethingFound = true;
                        }
                        iterator++;
                    }
                    if(somethingFound == true) {
                        tok = "";
                    }
                }
                i++;
            }
            DintCode.parse(command,strings,numberS,Pbool,FCS);
            FCS = "";
            In = Input.nextLine();
        }
    }
}
class parser {
    public static Lexr run = new Lexr();
    public static void parse(String cmd,String[] strings,int[] nums,boolean conditions,String codeBlock) {
        if(cmd.equals("print")) {
            try {
                for(int i = 0; i < strings.length; ++i) {
                    if(!(strings[i] == null)) {
                        System.out.println(strings[i]);
                    }
                }
                for(int i = 0; i < nums.length; ++i) {
                    if(!(nums[i] == 0)) {
                        System.out.println(nums[i]);
                    }
                }
            } catch(Exception e) {
                System.out.println(e);
            }
        } else if(cmd.equals("help")) {
            if(strings[0] == null || strings[0].equals("")) {
                System.out.println("\"out\":\n\tSYNTAX:\n\t\tout <String | Int: message>\n\tTYPE:\n\t\tCommand\n\tDESCRIPTION:\n\t\tPrints any pending strings/numbers, if there is more than 1 \n\t\tstring/number, each string/number will be printed on a new line.");
                System.out.println("\"help\":\n\tSYNTAX:\n\t\thelp <String: command>\n\tTYPE:\n\t\tSub-Command\n\tDESCRIPTION:\n\t\tReturns all help info (if no topic is specified) to the user.");
                System.out.println("\"exit\":\n\tSYNTAX:\n\t\texit\n\tTYPE:\n\t\tCommand\n\tDESCRIPTION:\n\t\tKills the program.");
                System.out.println("\"int\":\n\tSYNTAX:\n\t\t<String: var name> <Int: value> int\n\tTYPE:\n\t\tObject\n\tDESCRIPTION:\n\t\tCreates a variable of type \"int\".");
                System.out.println("\"String\":\n\tSYNTAX:\n\t\t<String: var name> <String: value> String\n\tTYPE:\n\t\tObject\n\tDESCRIPTION:\n\t\tCreates a variable of type \"String\".");
                System.out.println("\"function\":\n\tSYNTAX:\n\t\t<String: var name> {<line of code>} function\n\tTYPE:\n\t\tCommand\n\tDESCRIPTION:\n\t\tCreates a function.");
                System.out.println("\"cls\":\n\tSYNTAX:\n\t\tcls\n\tTYPE:\n\t\tSub-Command\n\tDESCRIPTION:\n\t\tClears the screen by adding \\n whitespace.");
                //System.out.println();
                //System.out.println();
                //System.out.println();
                //System.out.println();
                //System.out.println();
            } else if(strings[0].equals("out")) {
                System.out.println("SYNTAX:\n\tout <String | Int: message>\nTAGS / PARAMETERS:\n\tNone");
            } else if(strings[0].equals("help")) {
                System.out.println("SYNTAX:\n\thelp <String: topic>\nTAGS / PARAMETERS:\n\t<String> - Topic");
            } else if(strings[0].equals("exit")) {
                System.out.println("SYNTAX:\n\texit\nTAGS / PARAMETERS:\n\tNone");
            }
            else {
                System.out.println("The topic \""+strings[0]+"\" could not be found...");
            }
        } else if(cmd.equals("if")) {
            if(conditions == true) {
                run.lex(codeBlock);
            }
        } else if(cmd.equals("Bool")) {
            System.out.println(conditions);
        } else if(cmd.equals("SeeCB")) {
            System.out.println(codeBlock);
        }
    }
}
class Lexr {
    public static void lex(String In) {
        boolean Program = true;
        String[] PFuncs = new String[500];
        String[] PFuncVals = new String[500];
        String[] PStrs = new String[1000];
        String[] PStrVals = new String[1000];
        String[] PInts = new String[1000];
        int[] PIntVals = new int[1000];
        parser DintCode = new parser();
        if(Program) {
            String tok = "";
            String string = "";
            String command = "";
            String Number_string = "";
            String FCS = "";
            boolean Pbool = false; 
            int Number_numberInt = 0;
            int[] numberS = new int[256];
            int state = 0;
            int castStringTo = 0;
            int castIntTo = 0;
            int castIntNameTo = 0;
            int castIntValTo = 0;
            int castStrNameTo = 0;
            int castStrValTo = 0;
            int castFuncNameTo = 0;
            int castFuncValTo = 0;
            String[] strings = new String[256];
            for(int i = 0; i < In.length();) {
                tok += In.charAt(i);
                if(state == 1) {
                    string += In.charAt(i);
                    if(tok.equals("\\\"")) {
                        string += "\"";
                    } else if(tok.equals("\\n")) {
                        string += "\n";
                    }
                    if(!(tok.equals("\""))) {
                        tok = "";
                    }
                } else if(state == 2) {
                    if(tok.equals("0") || tok.equals("1") || tok.equals("2") || tok.equals("3") || tok.equals("4") || tok.equals("5") || tok.equals("6") || tok.equals("7") || tok.equals("8") || tok.equals("9")) {
                        Number_string += tok;
                        tok = "";
                    }
                    if(!(tok.equals("|") || tok.equals("<Random>"))) {
                        tok = "";
                    }
                    if(tok.equals("<Random>")) {
                        Random rand = new Random();
                        Integer Rand_PNumber = rand.nextInt(100);
                        Rand_PNumber += 1;
                        Number_string = Rand_PNumber.toString();
                        tok = "";
                        Rand_PNumber = 0;
                    }
                } else if(state == 3) {
                    FCS += In.charAt(i);
                    if(!(tok == "}")) {
                        tok = "";
                    }
                }
                if(tok.equals(" ")) {
                    tok = "";
                } else if(tok.equals("\"") && (state == 0 || state == 1)) {
                    if(state == 0) {
                        state = 1;
                    } else if(state == 1) {
                        state = 0;
                        string = string.substring(0, string.length() - 1);
                        strings[castStringTo] = string;
                        castStringTo++;
                        string = "";
                    }
                    tok = "";
                } else if(tok.equals("|") && (state == 0 || state == 2)) {
                    if(state == 0) {
                        state = 2;
                    } else if(state == 2) {
                        try {
                            state = 0;
                            Number_numberInt = Integer.parseInt(Number_string);
                            numberS[castIntTo] = Number_numberInt;
                            castIntTo++;
                            Number_numberInt = 0;
                        } catch(Exception e) {
                            System.out.println("NoIntError: Tried to parse value <Int> but was returned with <String> = or <Null> = <Blank>\n <Value> = "+Number_string);
                        }
                        Number_string = "";
                    }
                    tok = "";
                } else if(tok.equals("{") && state == 0) {
                    if(state == 0) {
                        state = 3;
                    }
                } else if(tok.equals("}")) {
                    if(state == 3) {
                        state = 0;
                        FCS = FCS.substring(0, FCS.length() - 1);
                    }
                } else if(tok.equals("out") && state == 0) {
                    command = "print";
                    tok = "";
                } else if(tok.equals("help") && state == 0) {
                    command = "help";
                    tok = "";
                } else if(tok.equals("exit")) {
                    System.exit(0);
                } else if(tok.equals("int")) {
                    PInts[castIntNameTo] = strings[0];
                    PIntVals[castIntValTo] = numberS[0];
                    tok = "";
                } else if(tok.equals("String")) {
                    PStrs[castStrNameTo] = strings[0];
                    PStrVals[castStrValTo] = strings[1];
                    castStrNameTo++;
                    castStrValTo++;
                    tok = "";
                } else if(tok.equals("function")) {
                    PFuncs[castFuncNameTo] = strings[0];
                    PFuncVals[castFuncValTo] = FCS;
                    castFuncNameTo++;
                    castFuncValTo++;
                    FCS = "";
                    tok = "";
                } else if(tok.equals("@Functions")) {
                    for(int iterator = 0; iterator < PFuncs.length;) {
                        if(!(PFuncs[iterator] == null)) {
                            System.out.println("FUNCTION: \""+PFuncs[iterator]+"\" - CODE: "+PFuncVals[iterator]);
                        }
                        iterator++;
                    }
                    tok = "";
                } else if(tok.equals("if")) {
                    command = "if";
                    tok = "";
                } else if((tok.equals("true") || tok.equals("1")) && state == 0) {
                    Pbool = true;
                } else if((tok.equals("false") && tok.equals("0")) && state == 0) {
                    Pbool = false;
                } else if(tok.equals("@GlobalBoolean")) {
                command = "Bool";
                tok = "";
                } else if(tok.equals("@Variables")) {
                    for(int iterator = 0; iterator < PStrs.length;) {
                        if(!(PStrs == null)) {
                            System.out.println(PStrs[iterator]+": "+PStrVals[iterator]);
                        }
                        iterator++;
                    }
                    for(int iterator = 0; iterator < PInts.length;) {
                        if(!(PInts == null)) {
                            System.out.println(PInts[iterator]+": "+PIntVals[iterator]);
                        }
                        iterator++;
                    }
                } else if(tok.equals("clr") || tok.equals("cls")) {
                    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                } else if(tok.equals("@CodeBlock")) {
                    command = "SeeCB";
                    tok = "";
                } else if(tok.equals("<null>")) {
                    strings[castStringTo] = null;
                    castStringTo++;
                } else {
                    boolean somethingFound = false;
                    for(int iterator = 0; iterator < PInts.length;) {
                        if(tok.equals(PInts[iterator])) {
                            numberS[castIntTo] = PIntVals[iterator];
                            somethingFound = true;
                        }
                        iterator++;
                    }
                    for(int iterator = 0; iterator < PStrs.length;) {
                        if(tok.equals(PStrs[iterator])) {
                            strings[castStringTo] = PStrVals[iterator];
                            somethingFound = true;
                        }
                        iterator++;
                    }
                    if(somethingFound == true) {
                        tok = "";
                    }
                }
                i++;
            }
            DintCode.parse(command,strings,numberS,Pbool,FCS);
            FCS = "";
        }
    }
}
 
 

Do you know for sure if you are getting into the block where Random is used? If you are parsing, you might be going character by character. If you are going character by character -- say by incrementing the index and using that index to get a character from the string -- you will only ever get 1 character at a time. <Random> is too many characters if that is the case, and that block will never execute.

 

I just uploaded the code...
And it should read the entire 'tok' variable....

 

Actually this Random code is working. If you remove it and test it independently from the rest of your code, you will see that is running and giving you a random number as a String:

public static void main(String[] args){
    String Number_string = "";
    Random rand = new Random();
    Integer Rand_PNumber = rand.nextInt(100);
    Rand_PNumber += 1;
    Number_string = Rand_PNumber.toString();
    Rand_PNumber = 0;
    System.out.println(Number_string);
}

I just copied your code and added a sysout on it.

On your code: tok is reading only the first char of the Input:
tok += In.charAt(i);

If you type "int", when debugging you can see tok value is "i"

 

Is there anyway to re-integrate this into my code without changing too much? Thanks!

 

You don't actually need to re-integrate this code as it is the same one.
Take a look at the "tok += In.charAt(i);" part: You need to adjust your tokenizer to read the whole words of the typed phrase instead of every char (for example you want to read the input "print int" as two different parts of a command so the first tok value would be "print" and the next one would be "int"). If I understood correctly, I suggest you take a look at this: baeldung.com/java-stringtokenizer

Classic DEV Post from Nov 21 '18

Dev.to Review #9: Top 7 Of The Week, Discussed

Two devs get together to discuss the week's Top 7 posts on dev.to

PDS OWNER CALIN (Calin Baenen) profile image
I am a very interested in making games in Java and making programming languages in Python

Need a change?

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️