3 Replies - 298 Views - Last Post: 27 December 2012 - 08:46 AM Rate Topic: -----

#1 recheej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-December 12

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:

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();



Is This A Good Question/Topic? 0
  • +

Replies To: NumberFormatException onBufferedReader to read doubles into ArrayList.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

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?:
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();


Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: NumberFormatException onBufferedReader to read doubles into ArrayList.

Posted 27 December 2012 - 04:48 AM

View Postrecheej, 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

Was This Post Helpful? 0
  • +
  • -

#4 recheej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-December 12

Re: NumberFormatException onBufferedReader to read doubles into ArrayList.

Posted 27 December 2012 - 08:46 AM

View PostGregBrannon, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1