Reputation: 555
I'm trying to create a program that displays shapes through user input and ouputs them to a window. I have a seperate class which defines the shapes and the graphics window however I can't work out how to get the user input for the shapes. I am new to Java and OOP in general. Here's my code so far:
import java.util.Scanner;
public class Input {
public static void main(String [] args) {
GraphicsScreen g = new GraphicsScreen();
Scanner s = new Scanner(System.in);
int val1 = 0;
int val2 = 0;
String line;
String [] sut;
System.out.println("Please enter your commands here. Type 'end' to quit.");
line = s.nextLine();
while ( line.equalsIgnoreCase("end"));
sut = line.split(", ");
line = sut [0];
if (sut.length > 1) {
val1 = Integer.parseInt(sut[1]);
if (sut.length > 2) {
val2 = Integer.parseInt(sut[2]);
}
}
if (line.equals("moveTo" + val1 + val2)) {
g.moveTo(val1, val2);
}
else if (line.equals("lineTo" + val1 + val2)) {
g.lineTo(val1, val2);
}
else if (line.equals("circle" + val1)) {
g.circle(val1);
}
else {
System.out.println("The commands you have entered are invalid. Please try again.");
}
}
}
Basically the int's defined at the top have to be set to a value else Eclipse returns an compliler error, so I set them to zero. In the last IF statement I have tried concatenating the values on the end but this would obviously result in shapes with zero value.
How would I go about changing this so the user defines val1
and val2
and it outputs that result to the window?
Upvotes: 0
Views: 1135
Reputation: 29266
if (line.equals("moveTo" + val1 + val2)) {
g.moveTo(val1, val2);
}
Shouldn't this just be
if (line.equals("moveTo")) {
g.moveTo(val1, val2);
}
You've already broken the line up at commas.
Upvotes: 2