Enjoy an ad free experience by logging in. Not a member yet?
Register .
07-07-2012, 04:24 AM
PM User |
#1
Regular Coder
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
split string into array
The split function doesnt return the result as a String[].
Example :
Code:
String a = "some phrase";
String[] b = a.split(" ");
U can see this by running both codes.
Code:
System.out.println(b);
System.out.println(b[0]);
I want it to return into an array like :
b[0] = "some";
b[1] = "phrase";
can someone help me figure out this pls? thanks!
07-07-2012, 04:41 AM
PM User |
#2
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Works fine for me:
PHP Code:
String a = "some phrase" ; String [] b = a . split ( " " ); for ( String s : b ) { System . out . println ( "String part: " + s ); }
So I'm not quite sure I follow you here?
07-07-2012, 05:19 AM
PM User |
#3
Regular Coder
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
yeah i know it works that way. but it not returns as an array.
try printing b[1]
if u print b[0] the output will be :
some
phrase
and i need to assign b[0] to some and b[1] to phrase. o.O
07-07-2012, 06:34 AM
PM User |
#4
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Um, no it doesn't. It works as expected, even the loop shows that.
PHP Code:
String a = "some phrase" ; String [] b = a . split ( " " ); for ( String s : b ) { System . out . println ( "String part: " + s ); } System . out . println ( "Position 0: " + b [ 0 ]); System . out . println ( "Position 1: " + b [ 1 ]);
Code:
String part: some
String part: phrase
Position 0: some
Position 1: phrase
BTW, if you are returning results of b[0] as:
That indicates your string is not separated by a space, rather its separated by a linefeed.
07-07-2012, 02:16 PM
PM User |
#5
Regular Coder
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
Hm....
is
String a = "some phrase";
equal to
e.nextLine();
when u write "some phrase" at the terminal?
because for me it shows
some
phrase
when printing b[0].
o.O
07-07-2012, 05:33 PM
PM User |
#6
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Doesn't matter if they come from the scanner or from hard coded input. The split works as anticipated:
PHP Code:
Scanner s = new Scanner ( System . in ); String a = s . nextLine (); String [] b = a . split ( " " ); System . out . println ( "Chars in a:" ); char [] ca = a . toCharArray (); for ( Character c : ca ) { System . out . println ((int) c ); } System . out . println ( "Chars in position 0:" ); char [] c0 = b [ 0 ]. toCharArray (); for ( Character c : c0 ) { System . out . println ((int) c ); } System . out . println ( "Chars in position 1:" ); char [] c1 = b [ 1 ]. toCharArray (); for ( Character c : c1 ) { System . out . println ((int) c ); }
Terminal:
Code:
some phrase
Chars in a:
115
111
109
101
32
112
104
114
97
115
101
Chars in position 0:
115
111
109
101
Chars in position 1:
112
104
114
97
115
101
Users who have thanked Fou-Lu for this post:
07-07-2012, 07:40 PM
PM User |
#7
Regular Coder
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
OMG thanks a lot!
im new to java so could u display some code example of the try/catch using e.nextLine()?
if its a number to something. if its a line...
thanks!
07-07-2012, 07:49 PM
PM User |
#8
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
PHP Code:
int i = 0 ; try { i = Integer . parseInt ( s . nextLine ()); } catch ( Exception ex ) { System . out . println ( "That was not a number." ); }
07-07-2012, 10:35 PM
PM User |
#9
Regular Coder
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
How can i use it with lots of lines?
like :
Code:
while(e.hasNext()){
int i = 0;
try{
i = Integer.parseInt(e.nextLine());
System.out.println("Your input is a number : "+i);
}catch (Exception ex){
String a = e.nextLine();
System.out.println("Your input is a text : "+a);
}
}
Terminal :
OBVIOUS OUTPUT I KNOW XP
Code:
90
Your input is a number : 90
omg
omgogmgomgomgomg
Your input is a text : omgogmgomgomgomg
I want something like this :
Code:
90
Your input is a number : 90
omg
Your input is a text : omg
....
how can i do that?
Last edited by sorlaker; 07-07-2012 at 10:45 PM ..
07-07-2012, 11:17 PM
PM User |
#10
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
You wouldn't put an input request in the catch. That's to display a message of something being incorrect. Perhaps what you mean is something more along the lines of:
PHP Code:
int i = 0 ; boolean bValid = false ; do { try { System . out .print( "Enter a number: " ); i = Integer . parseInt ( s . nextLine ()); bValid = true ; } catch ( NumberFormatException ex ) { System . out . println ( "That is not a number. Try again." ); } } while (! bValid ); System . out . println ( "You have entered: " + i );
?
07-08-2012, 05:59 PM
PM User |
#11
Regular Coder
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
It says arrayIndexOutOfBoundsException. o.O
07-08-2012, 06:50 PM
PM User |
#12
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
And the code you are using is?
IndexOutOfBoundsException indicates you are trying to access an array outside of the valid range declared. That hasn't a thing to do with casting, for which it tosses a NumberFormatException. The scanner can also throw, but you'll likely only see the IllegalStateException off of it, and the NoSuchElementException is also possible if you try and force information out of it (from say a file).
07-08-2012, 07:55 PM
PM User |
#13
Regular Coder
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
This is way too hard.
There isnt a easier way to make this work?
Code:
e.nextInt();
e.nextLine(); //this line always receives ""
07-09-2012, 03:59 AM
PM User |
#14
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Using nextInt() will leave the linefeed on the buffer. So when you call nextline it will only pull the linefeed. It can lead to a lot of trouble.
Simply call e.nextLine() after any call to a next* method other than nextLine. It'll clear the linefeed off to let the next line of entry through.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 01:17 AM .
Advertisement
Log in to turn off these ads.