this is the code that i have but i have to admit i have become a bit code blind and cant seem to see the problem eventhough i feel it is an easy one. Any help would be much appreciated. Thank you in advance.
do{
System.out.println("Command:");
scan = new Scanner (System.in);
String line;
line = scan.nextLine();
String[]SplitUpText;
SplitUpText=line.split(" ");
Command = SplitUpText[0];
int param1=-1, param2=-1;
if (Command.compareTo("move")==0)
{
if (SplitUpText.length>1)
{
param1=Integer.parseInt(SplitUpText[1]);
if(SplitUpText.length>2)
{
param2=Integer.parseInt(SplitUpText[2]);
}
}g.moveTo (param1, param2);
}else if(Command.compareTo("circle")==0){
if (SplitUpText.length>1)
{
param1=Integer.parseInt(SplitUpText[1]);
}g.circle (param1);
}else if(Command.compareTo("line")==0){
if (SplitUpText.length>1)
{
param1=Integer.parseInt(SplitUpText[1]);
if(SplitUpText.length>2)
{
param2=Integer.parseInt(SplitUpText[2]);
}
}g.lineTo (param1, param2);
} else {
System.out.println("Invalid Command");
}
} while (Command.compareTo ("end")!=0);
It’s generally best practice to avoid it (by adding a condition to the
whileexpression instead), but if you want to end the loop prematurely, you can usebreak;Here’s a simpler example:
Let’s suppose the special condition occurs when
ihas just become3, the output would be:Note how the
breakterminated the loop. It can be used with all kinds of loops (do..while,while, and both kinds offor).If you have nested loops and need to break the outer one from within the inner one, you should probably refactor that code into sub-methods. BUT, if you don’t want to refactor, you can use a directed
break:There,
outerLooplabels the outer loop, andbreak outerLoopcauses both the inner loop and the outer loop to terminate. Again, though, if you get into doing this, it’s probably time to refactor.More from the JLS: http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.15