• Post Reply Bookmark Topic Watch Topic
  • New Topic

Enter double values into a single line, and then put into an array?  RSS feed

 
Erika Her
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry the question isn't as specific as it should be. I'm trying to input quiz grades on a single line, and then have those quiz grades inputted into a type double array.

So far, my method is to accept the integers as a string, and then "cut out" the white spaces so that we only have the numbers, and then input those numbers into an array. My program compiles, however, when I run it it tells me that there is a java.lang.NumberFormatException: empty String (in sun.misc.FloatingDecimal). I'm confused as to why I'm receiving this error, and as to why my program isn't letting me put in the quiz score numbers (even though I have input.nextLine() right after the "Enter QUIZ scores: " line.

Here is my code:

I also have to find the sum of the array, but I can't do that if I can't input any integers.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible that you have more than one space between numbers?

Try changing this code: ...to this: "\s" stands for any whitespace (space, tab, newline) and "+" says, "match at least one and possibly many."  The "\\" is because you need to escape backslashes in Java Strings.
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.NumberFormatException: empty String

The issue is using the nextLine method AFTER having used any of the next... methods.  Scanner leaves the endline character in its buffer after using a next... method.  The nextLine method will retrieve just the endline character with an empty String.

To fix it, call nextLine one time to get the empty String before trying to read more data.
 
Erika Her
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
java.lang.NumberFormatException: empty String

The issue is using the nextLine method AFTER having used any of the next... methods.  Scanner leaves the endline character in its buffer after using a next... method.  The nextLine method will retrieve just the endline character with an empty String.

To fix it, call nextLine one time to get the empty String before trying to read more data.



So this is what I did



I added a string to that will store the "empty string". My program compiles, and I can now input integers. However, I'm getting this error now... java.lang.NumberFormatException: For input string: "5.5 10" (in sun.misc.FloatingDecimal)
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full text of the error message that gives the line number and the method that gave the exception?

The String with multiple numbers on it must be split and the Strings in the array from split should be able to be parsed.

To see what is in the array from split, use the Arrays class's toString method:
 
Erika Her
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Can you post the full text of the error message that gives the line number and the method that gave the exception?




The error message is occurring at line 21 (which is in the for statement). It's the quizScores[i] = Double.parseDouble(quizScoresInputStr); line.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are passing the string for the line, not separated strings.

 
Erika Her
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You are passing the string for the line, not separated strings.



Uhh... So how would I go about passing it as separated strings?? Any hints/tips?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erika Her wrote:
Carey Brown wrote:You are passing the string for the line, not separated strings.



Uhh... So how would I go about passing it as separated strings?? Any hints/tips?
Compare my response to your previous post.
 
Erika Her
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Erika Her wrote:
Carey Brown wrote:You are passing the string for the line, not separated strings.



Uhh... So how would I go about passing it as separated strings?? Any hints/tips?
Compare my response to your previous post.


OH wow. Didn't even notice you had made any changes! I'll try it out. Thanks!
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is recommended that you replace
with
In your case it will only handle a single space. \\s handles any type of space including tabs. Adding + says at least one but may be more. So if you have a string
it will deal properly with the multiple spaces.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why split the String at all? You can pass a String to a Scanner's constructor and let it do all the hard work for you.Try it with this sort of input, too:-
"1.23 2.34 3.45 4.56 5.67 6.78 7.89 Campbell was here ;-)"

Who wants to use Java5 features when you can use a Stream? What you can do is to create a Stream<String> with the stream method of the Arrays class. That will contain all the text as an array, and each of its elements is passed through the Stream. You can even use its filter() method to remove anything which isn't convertible to a double. Note I shall show you a λ expression to do that. Then you can use the map to double method to create a DoubleStream, which has a toArray method which does exactly what you want.If you are sure you only have numbers in the file, you can omit line 5. You need extra () in lines 2‑3 because the precedence of + is lower than that of .split(). You can simplify line 6 even more, but I am keeping quiet about that for the time being.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!