PDA

View Full Version : How to use If statement in Java


Gajendran3
Oct 19, 2013, 07:40 AM
I want a program in which it should ask a input from the user (eg, line 200 100, Move 200 100, circle 50) and it should draw in the graphic screen. The program should not stop until we press "end" it should keep drawing for all the commands. But in the below codes which I have attached I can run the code only once either circle or line. Please help me out to run all the command continuously.


import java.util.Scanner;

Public class MainFile2 {

/**
* @param args
*/
public static void main(String[] args) {



Scanner s = new Scanner(System.in);

System.out.print("Enter the value to draw :");
String text = s.nextLine(); // define some text

String [] splitupText = text.split(" "); // split the text into multiple elements, and store in an array of String




GraphicsScreen g = new GraphicsScreen();

String line = splitupText[0];

while ( line.equals("stop") == false){

if ( line.equals("line")){

if ( splitupText.length == 3 ) {
int x = Integer.parseInt(splitupText[1]);
int y = Integer.parseInt(splitupText[2]);

g.lineTo(x, y);
}
else {
System.out.println("Enter a correct Integer");
}

}

else if (line.equals("circle")){

int r = Integer.parseInt(splitupText[1]);

g.circle(r);

}

System.out.print("Enter another value : ");
line = s.nextLine();
}
g.close();
s.close();

}
}

Scleros
Nov 30, 2013, 09:41 AM
Think you should split more.