Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

NumberFormatException onBufferedReader to read doubles into ArrayList.

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


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>