0

So we have this program where we have to read double values from a text file into a double ArrayList and make certain calculations.

example of the text file:

Note: there's space between the date and the double values

5/05/1998 4.4 1/01/1999 -4.123 

the problem is that i'm getting is as below:

"NumberFormatException for input string: 1/2/1950 0.0258" error.

Here's my code:

public static void value() throws java.io.IOException{ try{ BufferedReader br = new BufferedReader(new FileReader(new File("SP500-Weekly.txt"))); ArrayList<Double>list = new ArrayList<Double>(); String line; while((line=br.readLine())!=null){ String[] r = line.split(" "); for(int i=0; i<r.length; i++){ double val = Double.parseDouble(r[i]); list.add(val); } }br.close(); System.out.println(list.size()); } catch(IOException io){ System.out.println("error"); } } 
8
  • Can you format the value in text file to be exactly what you are trying to load? Are each record present in new line? Commented Sep 4, 2018 at 21:07
  • yes every record is in a new line. Commented Sep 4, 2018 at 21:09
  • And what is your output supposed to be like? It is all doubles values in a single list? Commented Sep 4, 2018 at 21:11
  • Why are you splitting by 4 spaces? Shouldn't you split by single space and read only the second part of the result? Commented Sep 4, 2018 at 21:12
  • @stackFan, the doubles should be stored in a arraylist and then we have to access one of those randomly using Double value = data.get(r.nextInt(data.size()));. Commented Sep 4, 2018 at 21:13

3 Answers 3

1

Seems like you just need the double value present on each line and want to add it into a List<Double>. Based off of your input, second value is always your double value, but you are trying to parse first value as well which is a date and Double.parseDouble() cannot parse characters that comes in a date such as /, thus you ran into exception. If the double value is what you need then do it simply as :

try { BufferedReader br = new BufferedReader( new FileReader( new File( "SP500-Weekly.txt"" ) ) ); List<Double> list = new ArrayList<Double>(); String line; while ( ( line = br.readLine( ) ) != null ) { String[] r = line.split( "\\s+" ); list.add( Double.parseDouble( r[ 1 ] ) ); } br.close( ); System.out.println( list.size( ) ); } catch ( IOException io ) { e.printStackTrace( ); } 

You can then play around with list variable as your wish.

Also if you want to be more specific and check if it's actually number/double for second value then I'd do following to add a regex which will make sure that the second value is number (with or without - sign) as below:

while ( ( line = br.readLine( ) ) != null ) { String[] r = line.split( "\\s+" ); for ( String string: r ) { if ( string.matches( "^-?[0-9]?.?[0-9]+" ) ) { list.add( Double.parseDouble( r[ 1 ] ) ); } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Closing your resources manually or yet better using a try with resources would have been very nice. Also coding to the implementation rather than the interface is not correct.
@Aris_Kortex thanks for suggestion. Initially I wrote to address the main issue OP was having. Now the code has been updated.
1

A very simple and up to date implementation of this using java.nio and pure Java 8 would the following.

private static List<Double> readDoubles(final String fileName) { try (Stream<String> stream = Files.lines(Paths.get(fileName))) { return stream.map(s -> s.split("\\s+")[1]) .map(Double::parseDouble) .collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); } return Collections.emptyList(); } 

Implementation also takes the liberty of using try-with-resources to auto manager the various resources.

Comments

0

CORRECTCODE:

import java.util.List; class Sample extends SimulateMarket{ public static void value(){ try{ BufferedReader br = new BufferedReader(new FileReader(new File("SP500-Weekly.txt"))); List<Double> list = new ArrayList<Double>(); String line; while((line=br.readLine())!=null){ String[] r = line.split("\\s+"); list.add(Double.parseDouble(r[1])); }br.close(); Random rand = new Random(); int randomIndex = rand.nextInt(list.size()); System.out.println(list.get(randomIndex)); } catch(IOException io){ System.out.println("error"); } } 

}

3 Comments

This also obviously wrong. You catch and throw the same exception. Either do one of them. Also you're still using the concrete implementation for the initialization of the list.
The catch/throw thing i've fixed; however, i'm learning java basics at my college and i'm not familiar with the term "concrete implementation."
This is referring to this ArrayList<Double>list = new ArrayList<Double>();. Use this List<Double> list = new ArrayList<Double>(); instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.