Berk Kalkavan
Berk Kalkavan

Reputation: 85

Java splitting input

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");
                }
            }

Upvotes: 3

Views: 27746

Answers (6)

Paul Vargas
Paul Vargas

Reputation: 42020

You can use this:

// For read the input
InputStreamReader streamReader = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(streamReader);
String input = reader.readLine();

If you prefer use Scanner, then

Scanner sc = new Scanner(System.in);
String input = sc.nextLine();

Then:

StringTokenizer st = new StringTokenizer(input);
if (st.countTokens == 2) { // Only for two tokens "walk 100", "return 10"
    String method = st.nextToken();
    if (method.equals("walk") { // if you have more methods like "walk"
        int distance = Integer.parseInt(st.nextToken());
        walk(distance); // walk
    }
}

If you want to read more lines

for(String input; sc.hasNextLine();) {
    input = sc.nextLine();

    // do something

}

Or

for(String input; (input = reader.readLine()) != null;) {

    // do something

}

Upvotes: 0

Martin
Martin

Reputation: 7139

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])

Upvotes: 7

Kyle
Kyle

Reputation: 3885

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

  1. http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29
  2. http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29

Upvotes: 7

David W
David W

Reputation: 10184

Assuming you strip out the integer portion from the string, eg leaving an input of

"walk 10" with "10", just use something like

// say you've parsed the "number" portion into a variable "inputString"
int x;
x= Integer.parseInt(inputString);

Upvotes: 0

poy
poy

Reputation: 10507

Try:

String s = 1;
int i = Integer.parseInt(s); // returns 1

Upvotes: 0

Adam Liss
Adam Liss

Reputation: 48290

You can use the Integer.parseInt() method:

int distance = Integer.parseInt(input);

Upvotes: 1

Related Questions