Results 1 to 3 of 3

Thread: Splitting

  1. #1
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Splitting

    Java Code:
    import java.util.Scanner;
    
    public class Test {
    
    	private static String input = null;
    
    	public static void main(String[] args) {
    
    		System.out.println("Enter a number, an operator or command. 'h' = help menu");
    		Scanner scan = new Scanner(System.in); //reads user input	
    		input = scan.next(); // stores user input
    		String[] tokens = input.split(" ");
    		System.out.println("Count of tokens = " + tokens.length);
    		for (int x = 0; x < tokens.length; x++) {
    			System.out.println(tokens[x]);
    		}
    
    	}
    }
    Output:

    Enter a number, an operator or command. 'h' = help menu
    test a 1
    Count of tokens = 1
    test


    Should be


    Output:

    Enter a number, an operator or command. 'h' = help menu
    test a 1
    Count of tokens = 3
    test
    a
    1




    The string is only being splitted once? How would I fix this?
    Last edited by Dinomite07; 04-06-2016 at 02:31 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Splitting

    Scanner.next() reads the next token using the default delimiter which is white space. Try Scanner.nextLine() to get the complete line.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Need XML splitting
    By Tharun Jay in forum XML
    Replies: 2
    Last Post: 05-19-2014, 04:28 AM
  2. splitting strings.
    By Bimz in forum New To Java
    Replies: 3
    Last Post: 09-13-2011, 02:07 PM
  3. Splitting number
    By cshoya in forum New To Java
    Replies: 2
    Last Post: 06-06-2011, 03:23 AM
  4. Splitting
    By marksey07 in forum New To Java
    Replies: 2
    Last Post: 01-05-2011, 01:55 AM
  5. Array splitting
    By Lunarion in forum New To Java
    Replies: 3
    Last Post: 04-17-2009, 08:00 AM

Posting Permissions

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