• Post Reply Bookmark Topic Watch Topic
  • New Topic

retrieve numbers from string to string array  RSS feed

 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day, All! I've a task to get numbers from String like this:


and put those numbers to Integer array:


I have exception in this code:
Exception in thread "main" java.lang.NumberFormatException: For input string: "2977671
1113208"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:766)
at ca_mot.CA_MoT.main(CA_MoT.java:33)
Java Result: 1

As I understand the problem in Integer.valueOf(numbers[i]); because for the i=1 there are not "2977671", there are two numbers "2977671
1113208"

So, I remake the string like that:


And it works. But, how can I solve the problem more elegantly? Also, there are more numbers than just 10 and deleting of "\n" process is annoying.
Thanks!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

One thing you can try is passing each String to a Scanner's constructor and use its nextInt and hasNextInt methods.
Why do you want a String like that, particularly with line-feeds in? There must be better ways to pass numbers around.
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want a String like that, particularly with line-feeds in?

I'm training here http://www.codeabbey.com/index/task_view/min-of-two

Ok, I've tried:

I couldn't fill the array. Why so?
By the way, the output of looks very nice. There are just numbers from str_input and this is cool, but Ihave no idea how to use it.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are consuming the tokens when you print out the numbers, so there is nothing left to fill the array. At line 46 you have arrived at the end of the String and can't go back. You probably have to create a new Scanner for the same String and repeat the reading.

That website does not give you emough explanation about what you are supposed to do. The animations are difficult to follow.
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That web-site has got a set of tasks just to train yourself practicing. The task was to pick the minimum of two numbers from the list of numbers.
Campbell Ritchie, thanks for your explanations. Now it's clear.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

BTW, as a side note, the original program could have been fixed with a quick change to the regular expression -- the issue was that the regex only split on the space, it could have been modified to split on the space and the carriage return. Definitely a much quicker fix than switching to use the Scanner class.

Henry
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing your split line as shown below would have fixed the problem like Henry said.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!