Sunday, March 30, 2014

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



No comments:

Post a Comment