Results 1 to 8 of 8
  1. #1
    gevin20 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default spliting string problem

    I want to cut my string from space char but i am getting exception please solve this issue

    Java Code:
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    
    public class NameSurname {
    	public static void main(String args[])
    	{
    		Scanner sc=new Scanner(System.in);
    		String s0,s1=null,s2 = null,s3=null;
    		s0=sc.next();
    			StringTokenizer st=new StringTokenizer(s0, " ");
    				while(st.hasMoreTokens())
    				{
    					s1=st.nextToken();
    					s2=st.nextToken();
    					s3=st.nextToken();
    				}
    								
    			
    			System.out.println(s1+""+s2+""+s3);
    	}
    }
    
    console:
    Lionel andres messi
    Exception in thread "main" java.util.NoSuchElementException
    	at java.util.StringTokenizer.nextToken(Unknown Source)
    	at java.util.StringTokenizer.nextToken(Unknown Source)
    	at com.parikshak.NameSurname.main(NameSurname.java:15)
    I/p -O/p:

    my s0=Lionel andres Messi
    and i want to break it as soon as i find space and save it in s1,s2 and s3
    s1=Lionel
    s2=andres
    s3=messi

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: spliting string problem

    The problem is probably in your while loop. You check for more tokens (which could be only one or two), but then you try and get three. You
    need to get only a single token on each iteration of your loop.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: spliting string problem

    To give you a big clue, add a println statement like the following to diagnose the problem:
    Java Code:
    		...
    		s0 = sc.next();
    		System.out.println("s0 = [" + s0 + "]");
    		...
    See Scanner (Java Platform SE 7 ) to further familiarise yourself with the API in order to make the right method calls.

  4. #4
    gevin20 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: spliting string problem

    Quote Originally Posted by jim829 View Post
    The problem is probably in your while loop. You check for more tokens (which could be only one or two), but then you try and get three. You
    need to get only a single token on each iteration of your loop.

    Regards,
    Jim
    tried this but not working
    Java Code:
                     int i=0;
    		while(st.hasMoreTokens())
    				{
    					if(i==0)
    					{
    						s1=st.nextToken();
    						i++;
    					}
    					else if(i==1)
    					{
    						s2=st.nextToken();
    						i++;
    					}
    					else if(i==2)
    					{
    						s3=st.nextToken();
    						i++;
    					}
    				}

  5. #5
    Cg33k is offline Member
    Join Date
    Mar 2014
    Location
    Leb
    Posts
    9
    Rep Power
    0

    Default Re: spliting string problem

    Quote Originally Posted by gevin20 View Post
    I want to cut my string from space char but i am getting exception please solve this issue

    Java Code:
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    
    public class NameSurname {
    	public static void main(String args[])
    	{
    		Scanner sc=new Scanner(System.in);
    		String s0,s1=null,s2 = null,s3=null;
    		s0=sc.next(); //<---- Here is your problem
    			...
    
    console:
    Lionel andres messi
    Exception in thread "main" java.util.NoSuchElementException
    	at java.util.StringTokenizer.nextToken(Unknown Source)
    	at java.util.StringTokenizer.nextToken(Unknown Source)
    	at com.parikshak.NameSurname.main(NameSurname.java:15)
    As mentioned by previous members you should familiarize yourself with the Scanner class and the API; i.e check the other methods of the Scanner class
    Last edited by Cg33k; 03-30-2014 at 09:51 PM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: spliting string problem

    You're doing the same thing! I said one nextToken per hasMoreTokens check. If you insist on doing it this way, then change each subsequent if statement to:

    Java Code:
    if (st.hasMoreTokens()) {
       //
    } else if ...
    etc

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: spliting string problem

    You might also want to consider the String.split() method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: spliting string problem

    The root of the problem is at
    Java Code:
    s0=sc.next();
    Refer to Scanner (Java Platform SE 7 ). The doc for the next() method states, "Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern." The default delimiter is a whitespace.

    I'll repeat my previous suggestion, add a println() call as follows:
    Java Code:
    ...
    s0 = sc.next();
    System.out.println("s0 = [" + s0 + "]");
    ...
    It should become obvious to you why this is a problem.

    Instead of next(), refer to Scanner (Java Platform SE 7 ) to find the right method to use.

Similar Threads

  1. String spliting
    By emma in forum New To Java
    Replies: 4
    Last Post: 01-28-2013, 04:15 PM
  2. Spliting Large String Data (upto 2 GB)
    By piyush@java in forum Advanced Java
    Replies: 5
    Last Post: 01-12-2011, 02:23 PM
  3. problem in spliting a text
    By vipinvijay in forum New To Java
    Replies: 3
    Last Post: 01-10-2011, 10:09 PM
  4. Spliting a string (45 Characters)
    By BobAmin in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-20-2010, 09:45 AM
  5. spliting a string and checking each token's format
    By Implode in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 08:41 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
  •