Results 1 to 7 of 7

Thread: String Split

  1. #1
    sarovarc is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default String Split

    Java Code:
    System.out.println("Enter a coordinate pair:");
    input = sc.next();
    String[] points;
    points = input.split("\\s");
    System.out.println(points[0]+"\t"+points[1]);
    Hey all,

    I am trying to split a string with coordinates in it. Like lets say I input '-10 10' and then split it using a whitespace. This should put two strings in my points string array -> '-10' and '10' but when I print my points array I only get the first input value and thats it. The second value is never written to the points array. Any idea where I am going wrong?

    Thanks

  2. #2
    sarovarc is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    I think I got where I am going wrong. When I enter '-10 10' on the console,
    input = sc.next(); only reads the first int!!

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    sarovarc is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Isn't that what I was trying to do?

    Input is defined as a String and scanner sc.next(); returns a string. However in this case if I input '-10 10' it stops reading after '-10'. If someone can tell me where the problem was I'd really appreciate it.

    On a side note I was able to fix the problem by not using a scanner, but using a buffered reader and input stream reader instead. Here's how:

    <CODE>
    BufferedReader bufferedreader = new BufferedReader(newInputStreamReader(System.in));
    String number = bufferedreader.readLine();
    <CODE>

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    you can insert a loop for reinput

    Java Code:
    String[] points;
    
    for(int i=0; i<required_input_times; i++)
    {
    input = sc.next();
    points = input.split("\\s");
    
    for(int j=0; j<points.length; j++)
    {
     System.out.print(points[j]+"\t");
    }
    System.out.println();
    }
    Report if that helps
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sarovarc View Post
    Isn't that what I was trying to do?

    Input is defined as a String and scanner sc.next(); returns a string. However in this case if I input '-10 10' it stops reading after '-10'. If someone can tell me where the problem was I'd really appreciate it.

    On a side note I was able to fix the problem by not using a scanner, but using a buffered reader and input stream reader instead. Here's how:

    Java Code:
    BufferedReader bufferedreader = new BufferedReader(newInputStreamReader(System.in));
    String number = bufferedreader.readLine();
    We can do that in several ways, reading the console. After that handling them is the problematic base on your requirement. As you observed next() reads the next int only, which you don't want to do that, nextLine() will do.

    For multiple item handling you've to use arrays like Webuser point to you.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sarovarc View Post
    Isn't that what I was trying to do?

    Input is defined as a String and scanner sc.next(); returns a string. However in this case if I input '-10 10' it stops reading after '-10'. If someone can tell me where the problem was I'd really appreciate it.

    On a side note I was able to fix the problem by not using a scanner, but using a buffered reader and input stream reader instead. Here's how:

    Java Code:
    BufferedReader bufferedreader = new BufferedReader(newInputStreamReader(System.in));
    String number = bufferedreader.readLine();
    We can do that in several ways, reading the console. After that handling them is the problematic base on your requirement. As you observed next() reads the next int only, which you don't want to do that, nextLine() will do.

    For multiple item handling you've to use arrays like Webuser point to you.

Similar Threads

  1. Please help me with Split method> String
    By javanew in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 06:15 PM
  2. string split
    By gisler in forum New To Java
    Replies: 6
    Last Post: 12-17-2009, 02:23 PM
  3. I wish to split a string xxx/yyyy/zzz so that ...
    By nmvictor in forum New To Java
    Replies: 10
    Last Post: 10-23-2009, 09:47 AM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •