Results 1 to 7 of 7
  1. #1
    josho493 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default How do I split a string of ints at every space

    A project I'm doing for school requires me to get a user input of a a set of ints. The way the will be inputted is like this; "1 4 2 5 345 6 3 234". I need to split them every space and into an array. I've tried doing .split(" ") but that does not seem to yield any results unless I input characters instead of numbers.

    Here's what I have:

    Java Code:
    Scanner input = new Scanner(System.in);
    			
    String votes = input.next();
    String[] array = votes.split(" ");
    The array is currently just storing the first number that was inputted and none of the other ones

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How do I split a string of ints at every space

    Use nextLine() instead.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    josho493 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: How do I split a string of ints at every space

    Awesome, thanks! That seems to have solved it!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: How do I split a string of ints at every space

    Also, I think you want to split on "\\s+" instead of a single space.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    josho493 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: How do I split a string of ints at every space

    Thanks! In the Googleing I did to look for an answer I saw most of the examples included that. Should help eliminate any input errors, even though they tell us what they will be inputting into our code to test it.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: How do I split a string of ints at every space

    Quote Originally Posted by josho493 View Post
    Thanks! In the Googleing I did to look for an answer I saw most of the examples included that. Should help eliminate any input errors, even though they tell us what they will be inputting into our code to test it.
    Splitting on "\\s+" splits the String one one or more spaces (any white space) so any input with just one space between the numbers will be split correctly while the code will be more robust.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: How do I split a string of ints at every space

    Thanks JosAH

Similar Threads

  1. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 05:08 PM
  2. checking for ints in a String
    By SteroidalPsycho in forum New To Java
    Replies: 1
    Last Post: 03-26-2010, 07:09 PM
  3. Java split string, the first char is a space
    By new_cyber in forum New To Java
    Replies: 8
    Last Post: 12-31-2009, 02:27 AM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 09:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10: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
  •