Java splitting input
Solution 1
Try using the split() method on a string. It will return an array of Strings. For example
String s = 'walk 10';
String[] splitStrings = s.split(" ")
Now, to access 10, you can do this:
String distanceToWalk = splitStrings[1]
To convert it to an int, use the parseInt() method on Integer
Integer.parseInt(distanceToWalk);
See
- http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29
- http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29
Solution 2
You can split on the space, in which case the number would be the second element (index 1) of the resulting array e.g.
Edit following comment from Adam Liss
Integer x = Integer.parseInt(input.split(" ")[1])
Solution 3
You can use the Integer.parseInt()
method:
int distance = Integer.parseInt(input);
Berk Kalkavan
Updated on July 09, 2022Comments
-
Berk Kalkavan almost 2 years
i'm trying to write a simple code for my project if user types
walk 10
i need to use "walk" command in my walk(distance) method as distance = 10 i have something like that
while (!quit) { Scanner in = new Scanner(System.in); String input = in.nextLine(); // if (walk x is typed) { walk(x); } }
i'm using java.util.scanner and another problem is that my method walk(int x) uses int so i need to convert String input to int
i searched the web for a while but couldnt find what i'm looking for (or i didnt understand it)
so anyway i'd appreciate if you can help me thanks
Thanks a lot for all the answers my initial problem is fixed but now i have another problem when i type just "walk" it gets array out of bounds exception of course because it tries to convert null to an int (not possible) i tried this and checked online
try { Integer.parseInt(splitStrings[1]); } catch(NumberFormatException e) { System.out.println("error: " + e); }
still no help (i'm not good with try/catches) so if user types walk without a distance i need to give an error message thanks a lot again
ok i got it too just used a simple thing
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"); } }
-
mikera about 12 years+1 for a neat solution, though given that this is user input you would also want to strip out excess spaces first (otherwise your number will be in the wrong place in the array).
-
Adam Liss about 12 yearsWait, what??? You can't cast a String to an Integer! Shame on all you upvoters.
-
Berk Kalkavan about 12 yearsthanks a lot. that was exactly what i was looking for both splitting the input in 2 from white space and converting 10 to integer
-
Martin about 12 years@AdamLiss You are absolutely correct, modified the answer accordingly.
-
Adam Liss about 12 years@Strawberry +1 for updating. Also for the audacity to post an answer that wasn't even valid Java ... and still managing to get 3 upvotes!
-
Berk Kalkavan about 12 yearshow can i use tokenizer with java.util.scanner?
-
Paul Vargas about 12 yearsI added for
StringTokenizer
andScanner
.