0

I have been attempting to separate values from a text file. The values are not tab separated but varying,and I am trying to put them into different arrays. A sample of the text file is shown below.

1908 1 5.0 -1.4 21 --- 29.7 1908 2 7.3 1.9 8 --- 71.9 1908 3 6.2 0.3 13 --- 101.4 1908 4 8.6 2.1 5 --- 128.6 1908 5 15.8 7.7 0 --- 180.4 1908 6 17.7 8.7 0 --- 196.9 1908 7 18.9 11.0 0 --- 196.1 1908 8 17.5 9.7 0 --- 187.2 1908 9 16.3 8.4 0 --- 99.5 1908 10 14.6 8.0 0 --- 56.1 1908 11 9.6 3.4 6 --- 28.4 1908 12 5.8 0.0 13 --- 10.3 1909 1 5.0 0.1 11 --- 35.6 1909 2 5.5 -0.3 18 --- 49.9 1909 3 5.6 -0.3 17 --- 58.7 1909 4 12.2 3.3 3 --- 188.9 1909 5 14.7 4.8 2 --- 216.8 1909 6 15.0 7.5 0 --- 139.5 1909 7 17.3 10.8 0 --- 151.2 1909 8 18.8 10.7 0 --- 167.5 1909 9 14.5 8.1 0 --- 74.4 1909 10 12.9 6.9 3 --- 101.5

The arrays I am splitting the data into at the moment is just the year; I will then create the code for the remaining sets of data.

The arrays would be like this:

yyyy [0] 1908
     [1] 1908
ect

public class test {
    private static String removeCharacters(String string){
        string = string.replace("*", "");
        string = string.replace("#", "");
        string = string.replace("---", "");
        return string;
    }


    public static void main(String[] args) throws IOException {
        int i = 0;
        double test[]= new double [1200];
        BufferedReader testFile = new BufferedReader(new FileReader("historical_weather_data/sources/bradforddata.txt"));
        String line;

        testFile.readLine();
        testFile.readLine();
        testFile.readLine();
        testFile.readLine();
        testFile.readLine();
        testFile.readLine();
        testFile.readLine();

        while((line = testFile.readLine()) != null) { 
            String splits[] = line.split("  ");
            test[i] = Double.parseDouble(removeCharacters(splits[0]));
            i++;
        }
        for(int h=0;h<test.length;++h){
            System.out.println(test[h]);
        }
    }
}

From this code the following error is produced:

Exception in thread "main" java.lang.NumberFormatException: For input string: "1908   1    5.0    -1.4      21         29.7"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at test.main(test.java:32)

Any help will be very appreciated.

6
  • 1
    Carefully read your exception, you're rtrying to parse the string "1908 1 5.0 -1.4 21 29.7" as a double, which gives you the error. You're making an error when reading in the .txt file. Commented May 9, 2017 at 20:33
  • How is this fixed then? Commented May 9, 2017 at 20:44
  • Could you upload the whole textfile? Commented May 9, 2017 at 20:50
  • it's too long to copy onto here, google drive link to the document - drive.google.com/open?id=0B4YNBcGEuF3hT3JsLURIZUdTdFE Commented May 9, 2017 at 20:53
  • line.split(" "); replace this with line.split("\t"); Should fix your issue. Commented May 9, 2017 at 20:56

1 Answer 1

1

Looking at the linked document, the values are not tab-separated, but use varying numbers of spaces. The split method uses regex, so this should work:

String splits[] = line.split(" +");
Sign up to request clarification or add additional context in comments.

7 Comments

it produces an empty string using that, Exception in thread "main" java.lang.NumberFormatException: empty String
Try using splits[1]. The lines start with spaces, so the first split is between the start of the line (empty string) and the first number.
that's got it! Thank you very much!
@Jamie alternatively, use this String[] newLine = line.trim().split(" +");
@SeanVanGorder I've finished all of the program now, thanks for you help, just so I understand for further programs, what did you + sign do?
|

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.