java.lang.NumberFormatException: For input string: "100 2 20 2"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at Internship.main(Internship.java:29)
[/coder]
I can't seem to point out why. It seems like a legal move to read in a string, then parse it to a double.
[code]
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStreamReader;
/**
*
*
* Written 12/26/2012
*/
public class Internship {
public static void main(String[] args) throws IOException{
double denomenator = 0.0, numerator = 0.0, probability = 0.0, pEntered = 0, winners = 0, tickets = 0, pGroup = 0;
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
//Array list to hold values.
ArrayList<Double> lotteryInput = new ArrayList<Double>();
System.out.println("Enter nubmers");
//Add values entered from input stream to arraylist
for(int i = 0; i < 4 ; i++){
try {
lotteryInput.add(Double.parseDouble(r.readLine()));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
r.close();
3 Replies - 129 Views - Last Post: 27 December 2012 - 08:46 AM
#1
NumberFormatException onBufferedReader to read doubles into ArrayList.
Posted 27 December 2012 - 12:43 AM
Hello peeps. I'm new to bufferedreader. I'm using it instead of scanner because my program has to save on memory. But it seems like a headache. What I'm trying to do is read in a line of doubles.. I have to all the doubles. from one line. I cannot read then read again. I'm getting a numberFormatException but I can't seem to point it out. Here's the exact error:
Replies To: NumberFormatException onBufferedReader to read doubles into ArrayList.
#2
Re: NumberFormatException onBufferedReader to read doubles into ArrayList.
Posted 27 December 2012 - 01:10 AM
You're getting the error because you're trying to parse the whole line of input as a single double value. The code doesn't know to take a 'token' at a time from the input, broken up by the whitespaces.
Can you do something like?:
Can you do something like?:
String[] inputString = r.readLine().split( " " );
for(int i = 0; i < 4 ; i++)
{
try {
lotteryInput.add(Double.parseDouble( inputString[i] ) );
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
r.close();
#3
Re: NumberFormatException onBufferedReader to read doubles into ArrayList.
Posted 27 December 2012 - 04:48 AM
recheej, on 27 December 2012 - 03:43 AM, said:
I'm using it instead of scanner because my program has to save on memory.
This previous statement just does not make sense. A Scanner is just a easy to use wrapper over a BufferedReader.
Using a Scanner like
for(int i = 0; i < 4; ++i)
lotteryInput.add(scanner.nextDouble() );
should do the job. And also why using double instead of int ? Lotterynumber are integer not real, you can't get 3.14159 you'll get 3 or 4
This post has been edited by pbl: 27 December 2012 - 04:51 AM
#4
Re: NumberFormatException onBufferedReader to read doubles into ArrayList.
Posted 27 December 2012 - 08:46 AM
GregBrannon, on 27 December 2012 - 01:10 AM, said:
You're getting the error because you're trying to parse the whole line of input as a single double value. The code doesn't know to take a 'token' at a time from the input, broken up by the whitespaces.
Thank you very much. That worked perfectly. I did not know that split method worked like that. I had looked at the API but overlooked it.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|