loading...

Why does this command loop over and over in Java?

calin92540842 profile image PDS OWNER CALIN (Calin Baenen) ・1 min read

SIDENOTE: I left the imports out of my code...
Why does this code continually loop my command?

class parser {
    public static void parse(String cmd,String[] strings) {
        if(cmd.equals("print")) {
            for(int i = 0; i < strings.length;) {
                if(!(strings[i].equals(null))) {
                    System.out.println(strings[i]);
                }
            }
        }
    }
}

I tried to see if there were any work-arounds but none....
Context (ExtraCode):

public class Dint {
    public static void main(String[] args) {
        System.out.println("Welcome Dint ISE v0.0.1! use \"help\" for help!\n");
        Scanner Input = new Scanner(System.in);
        String In = Input.nextLine();
        boolean Program = true;
        parser DintCode = new parser();
        while(Program) {
            String tok = "";
            String string = "";
            String command = "";
            int state = 0;
            int castStringTo = 0;
            String[] strings = new String[100];
            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 = "";
                    }
                }
                if(tok.equals(" ")) {
                    tok = "";
                } else if(tok.equals("\"")) {
                    if(state == 0) {
                        state = 1;
                    } else {
                        state = 0;
                        string = string.substring(0, string.length() - 1);
                        strings[castStringTo] = string;
                        castStringTo++;
                    }
                    tok = "";
                } else if(tok.equals("out") && state == 0) {
                    command = "print";
                    tok = "";
                }
                i++;
            }
            DintCode.parse(command,strings);
            In = Input.nextLine();
        }
    }
}

Posted on by:

calin92540842 profile

PDS OWNER CALIN (Calin Baenen)

@calin92540842

I am a 13 (as of Oct 30 of 2019) yr/o developer (I have been developing mini-projects for 4, years now, since I was 9), who makes projects in languages like: Java, HTML, Python 3, JS, CSS, and C#.

Discussion

markdown guide
 

Change for(int i = 0; i < strings.length;) => for(int i = 0; i < strings.length; ++i)

That's all :)

 

Thanks, I don't know too much about java...!
:)
Edit:
The code doesn't seem to work, but rather when I do "out" in general, with or without string it kicks me out... is there an error I am missing?
Edit 2:
Nevermind, I found the issue...