0

I am trying to convert a list of numbers from a txt file into an array of numbers. There is 26 numbers, and each is on a different line in the text file. My code is

import java.io.*;
public class rocket {

    public static void main(String[] args) throws IOException, FileNotFoundException
    {
        BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream("/Users/Jeremy/Documents/workspace/altitude.txt")));
        String[] stringArray = new String[25];
        double[] doubleArray = new double[stringArray.length];
        for(int i=0; i<25; i++)
        {
            stringArray[i] = b.readLine();
            doubleArray[i] = Double.parseDouble(stringArray[i]);
        }
        for(int i = 0; i<doubleArray.length; i++)
        {
            System.out.println(doubleArray[i]);
        }
    }
}

But every time I run it I get a number format exception. And if I try to just print out the strings I get an indexOutOfBounds exception

4
  • 1
    if there are 26 numbers you should be doing this new String[26]. This is causing the indexOutOfBoundsException Commented Mar 23, 2013 at 5:24
  • 3
    readLine() returns a string with the linebreak appended. Also, you don't need to create a string array. You can use 1 tempString variable inside the loop. Not sure how you'd handle the double[] size though, because I don't know Java. edit: For the doubles, use a vector. Commented Mar 23, 2013 at 5:24
  • I thought that the count started at the "zeroeth" term? And @Magtheridon96, I don't know how to do that, but I would appreciate it if you put that into an answer Commented Mar 23, 2013 at 5:41
  • @user1940007 They do however you are specifying the size here. You want 26 elements. Not 25. Commented Mar 23, 2013 at 5:44

4 Answers 4

2

in question you have mentioned that there is 26 strings so declate

String[] stringArray = new String[26];

And The number format exception is occuring as readline returns with the linbreak. To read line you can do the following

 public String[] readLines(String filename) throws IOException {
        FileReader fileReader = new FileReader(filename);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        List<String> lines = new ArrayList<String>();
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            lines.add(line);
        }
        bufferedReader.close();
        return lines.toArray(new String[lines.size()]);
    }

So by this logic you can get double by

public static Double[] readLines(String filename) throws IOException {
    FileReader fileReader = new FileReader(filename);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    List<Double> lines = new ArrayList<Double>();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        lines.add(Double.parseDouble(line));
    }
    bufferedReader.close();
    return lines.toArray(new Double[lines.size()]);
}
Sign up to request clarification or add additional context in comments.

8 Comments

The rest of my method will be using derivatives, so I need the array to be a double, and this returns a string array.
I just showed how you can parse the line .. from this string array you can modify the code to get double array
So I put this in the main, and directly copied and pasted the method that returns a double, and I still get the error message NumberFormatException System.out.println(Arrays.toString(readLines("/Users/Jeremy/Documents/workspace/altitude.txt")));
:s.. just print the return values in system.out.print(readLines(filename)); if there is stil problem.. show the file
the file is just 60 2927 10170 21486 33835 45251 55634 65038 73461 80905 87368 92852 97355 100879 103422 104986 106193 110247 119626 136106 162096 199506 238776 277065 314375 350704 each number is on another line
|
1

Try

    BufferedReader b = new BufferedReader(
            new InputStreamReader(
                    new FileInputStream(
                            "D:/git-repo/general/misc_test/src/java/com/greytip/cougar/module/test/v2/controller/so/dump/data.txt")));

    List<String> lines = new ArrayList<String>();

    String line = null;
    while ((line = b.readLine()) != null) {
        lines.add(line);
    }

    String[] stringArray = lines.toArray(new String[lines.size()]);
    double[] doubleArray = new double[stringArray.length];
    for (int i = 0; i < stringArray.length; i++) {
        doubleArray[i] = Double.parseDouble(stringArray[i]);
    }

    for (int i = 0; i < doubleArray.length; i++) {
        System.out.println(doubleArray[i]);
    }

4 Comments

still has a NumberFormatException
can you share the text file
the file is just 60 2927 10170 21486 33835 45251 55634 65038 73461 80905 87368 92852 97355 100879 103422 104986 106193 110247 119626 136106 162096 199506 238776 277065 314375 350704 each number is on another line
Works, I realized that my file wasn't a pure txt file
1
String[] stringArray = new String[26];

try this

Comments

0
    import java.io.*;
    public class Rocket {

        public static void main(String[] args) throws IOException, FileNotFoundException
        {
            BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream("/Users/Jeremy/Documents/workspace/altitude.txt")));
            String[] stringArray = new String[25];
            double[] doubleArray = new double[stringArray.length];
            for(int i=0; i<25; i++)
            {
                stringArray[i] = b.readLine();
                try{
                    doubleArray[i] = Double.parseDouble(stringArray[i]);
                }catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            for(int i = 0; i<doubleArray.length; i++)
            {
                System.out.println(doubleArray[i]);
            }
        }
    }

Handle the Exception when parse a string to double please.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.