Shahbaz Malik
Shahbaz Malik

Reputation: 71

Read Double From a .txt File into an Array List

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");
    }
}

Upvotes: 0

Views: 1176

Answers (3)

stackFan
stackFan

Reputation: 1608

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 ] ) );
                }
            }
        }

Upvotes: 1

Shahbaz Malik
Shahbaz Malik

Reputation: 71

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");
    }
}

}

Upvotes: 0

akourt
akourt

Reputation: 5563

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.

Upvotes: 1

Related Questions