0

I am building a java project in Netbeans. I got my data file (temperature.txt) that contains the high and low temp in the format (low)|(high) .The file should load into 2 dimensional array and then print it to the screen. But when I run my java project, I came across this error and am completely lost. But i don't know how to fix this problem.

Temperature.text:

+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| Day         | 1     |  2    |  3    |   4   |   5   |   6   |   7   |   8   |   9   |  10   |
+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| Temperature | 30|32 | 29|30 | 25|28 | 25|29 | 27|31 | 28|32 | 26|30 | 24|32 | 24|41 | 27|32 |
+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+

Output:

Analysis report of the temperature reading for the past 10 days 

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Lab4Ex2.main(Lab4Ex2.java:48)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

Here is my code:

import java.util.StringTokenizer;
import java.io.*;

public class Exercise {

    public static void main(String[] args) {

        StringTokenizer tokenizer;
        String line;
        String file="temperature.txt";
        int[][] temp=new int[10][2];
        int sumHigh, sumLow;
        FileReader fr=null;
        BufferedReader br=null;

        try
        {
            fr=new FileReader(file);
            br=new BufferedReader(fr);

            line=br.readLine();
            System.out.println("Analysis report of the temperature reading for the past 10 days " + line);

            String [] content=line.split("|");

            for(int row=0; row<=content.length; row++)
            {
               //I am trying to parse the two token into integer..

               if(row != 0)
               {
                   try
                   {
                       //Parse first token into integer and store in current row column 0
                       if(row % 2 != 0) 
                       {
                           sumLow = Integer.parseInt(content[row]);
                           temp[row][0]=Integer.parseInt(content[row]); <---Line 48

                       }
                       //Parse second token into integer and store in current row column 0
                       else if (row % 2 == 0) 
                       {
                           sumHigh = Integer.parseInt(content[row]);
                           temp[row][1]=Integer.parseInt(content[row]); 
                       }
                   }
                   catch(NumberFormatException e)
                   {
                       System.out.println("The code throws an exception");
                   }
               }
               System.out.println();

            }
            br.close();
        }

        catch(FileNotFoundException e)
        {
            System.out.println("The file " + file + " was not found");
        }
        catch(IOException e)
        {
            System.out.println("Reading error");
        }
        catch(NumberFormatException e)
        {
            System.out.println("Parsing error");
        }
        finally
        {
            if(fr != null)
            {
                try
                {
                    fr.close();
                }
                catch(IOException e)
                {
                    System.out.println("Reading error");
                }
            }
        }


    }

}
  • Which line is 48? – bradimus Nov 16 '16 at 14:01
  • You should know that `split()` uses a *regex*. As such, the `|` is a special character which tells it to look for a delimiter which is "Either this or that", in this case "Either nothing or nothing". This is not what you intended. You should use `\\|` instead. But you have many logic errors in your program besides that. – RealSkeptic Nov 16 '16 at 14:05
  • `row<=content.length` ... shouldn't that be less than only? `row < content.length` – A.Sharma Nov 16 '16 at 14:07
  • You should use `row < content.length` in your `for` loop instead of `row <= content.length`. Valid array indices go from 0 up to and including length - 1. – Jesper Nov 16 '16 at 14:07
  • Have you even tried to debug the code ? – Egl Nov 16 '16 at 14:10
  • Dude you've already posted this same question earlier today and I've tried to put a solution there: http://stackoverflow.com/q/40632060/4101876. – mdewit Nov 16 '16 at 14:11

1 Answers1

0

I don't know whether you've already asked this, but here is a working solution for you to try. The key point which you seemed to be struggling was that there was only a single line of the file which actually had data you wanted to read. I split this line using the following regex:

line.split(" \\| ?")

This leaves us with strings in the form 28|32 for example. Each of these high/low pairs can further be split again using pipe (but be careful, you need to escape the pipe, i.e. \\|). Finally, the data can be stored into your array, and output at the end as a sanity check.

public static void main(String[] args) {
    String line;
    String file = "temperature.txt";
    int[][] temp = new int[10][2];
    FileReader fr = null;
    BufferedReader br = null;

    try
    {
        fr = new FileReader(file);
        br = new BufferedReader(fr);

        // eat the first three lines, as they don't contain data you want to use
        br.readLine();
        br.readLine();
        br.readLine();
        line = br.readLine();
        System.out.println("Analysis report of the temperature reading for the past 10 days " + line);

        String [] content=line.split(" \\| ?");
        for (int i=1; i < content.length; ++i) {
            String[] pair = content[i].split("\\|");
            temp[i-1][0] = Integer.parseInt(pair[0]);
            temp[i-1][1] = Integer.parseInt(pair[1]);
        }
        System.out.println(Arrays.deepToString(temp));

    }
    catch (Exception e) {
        System.out.println("An exception occurred.");
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360