how to acces arguments using while loop in java

Imam Samudi :

how do I access the contents of arguments starting from the first index using "while loop"? I tried this one:

enter image description here

and get this error:

enter image description here

Kevin Anderson :

Test i < args.length before using args[i]. You're testing i at the top of your loop (wrong test, BTW, you want <, not <=), but by that point, it's too late: your while already accessed args[i] and died.

Try this instead:

while (i < args.length && !args[i].equals("Dua puluh")) {
    textUlang = textUlang + "ulang";
    ++i;
}

That way, you're always sure that i is a valid index before you actually try to do args[i].

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349312&siteId=1