![]() |
split string into array
The split function doesnt return the result as a String[].
Example : Code:
String a = "some phrase";Code:
System.out.println(b);b[0] = "some"; b[1] = "phrase"; can someone help me figure out this pls? thanks! |
Works fine for me:
PHP Code:
|
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 |
Um, no it doesn't. It works as expected, even the loop shows that.
PHP Code:
Code:
String part: someCode:
some |
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 |
Doesn't matter if they come from the scanner or from hard coded input. The split works as anticipated:
PHP Code:
Code:
some phrase |
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! |
PHP Code:
|
How can i use it with lots of lines?
like : Code:
while(e.hasNext()){OBVIOUS OUTPUT I KNOW XP Code:
90Code:
90how can i do that? |
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:
|
It says arrayIndexOutOfBoundsException. o.O
|
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). |
This is way too hard.
There isnt a easier way to make this work? Code:
e.nextInt(); |
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. |
| All times are GMT +1. The time now is 02:38 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.