I am trying to write simple code for my project if the user type
walk 10
I need to use the โwalkโ command in my walk (distance) method, since distance = 10 I have something like this
while (!quit) {
Scanner in = new Scanner(System.in);
String input = in.nextLine();
// if (walk x is typed) {
walk(x);
}
}
I use java.util.scanner and another problem is that my walk (int x) method uses int so I need to convert the String input to int
I searched the Internet for a while, but could not find what I was looking for (or I did not understand it)
So in any case, I would appreciate it if you could help me.
, โโ
"",
, null int ( )
try {
Integer.parseInt(splitStrings[1]);
}
catch(NumberFormatException e) {
System.out.println("error: " + e);
}
- ( try/catch)
, ,
,
if ("walk".equals(splitStrings[0])) {
if (splitStrings.length == 2) {
int distance = Integer.parseInt(splitStrings[1]);
Robot.walk(distance);
}
if (splitStrings.length != 2) {
System.out.println("Entry must be like walk 10");
}
}