Ghassen Bellagha
Ghassen Bellagha

Reputation: 249

Read text file to an int array in Java

Given a text file as follow :
10
100
99
99
96
96
92
92
91
88
87
86

Where the first number "10" means that the text file is containing 10 integer and the second number 100 means that all the numbers in the text file doesn't exceed 100.
My objective is to read the text and fill an int data[][]; as follow :

data[0][0]=1    data[0][1]=99
data[1][0]=2    data[1][1]=99
data[2][0]=3    data[2][1]=96
data[3][0]=4    data[3][1]=96
data[4][0]=5    data[4][1]=92
data[5][0]=6    data[5][1]=92
data[6][0]=7    data[6][1]=91
data[7][0]=8    data[7][1]=88
data[8][0]=9    data[8][1]=87
data[9][0]=10   data[9][1]=86
cbin = 100 // bin capacity 
nbin = 10 // number of objects 

That's means first raw for index and second for item's value or weights .. and int cbin = \\the second text's value and int nbin = \\the first text's value
the problem that i get an Exception in thread

"AWT-EventQueue-0" java.lang.NullPointerException


Here is my code :

 public static int[][] data=null; \\ in the first of the document

       JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f= chooser.getSelectedFile();

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(f));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(interface1.class.getName()).log(Level.SEVERE, null, ex);
        }
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        int i=0,j=0;
        while (line != null) {

            if(i==0){
                nbin= (int) Integer.parseInt(""+line);
                System.out.println(nbin);
            }
            else if (i==1) {
                cbin=(int) Integer.parseInt(""+line);
                System.out.println(cbin);
            }
          //  sb.append(line);
            line = br.readLine();
        if(i >= 2 && line != null){

               data[j][1]=(int) Integer.parseInt(line);
               data[j][0]=j;
               j++;

             }

            i++;

        }
          }   catch (IOException ex) {
            Logger.getLogger(interface1.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
        try {
            br.close();
        } catch (IOException ex) {
            Logger.getLogger(interface1.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

Any ideas ??

Upvotes: 1

Views: 3679

Answers (2)

necromancer
necromancer

Reputation: 24641

Using java.util.Scanner, statements such as Scanner scanner = new Scanner(System.in);, while (scanner.hasNext()) { /* ... */ }, and int n = scanner.nextInt(); will make it a much simpler program.

Like so:

import java.util.Scanner;

public class Read {
  public static void main (String [] args) {
    try (
      Scanner scanner = new Scanner(System.in);
    ) {
      int nbin = scanner.nextInt();
      int cbin = scanner.nextInt();
      int data[][] = new int[nbin][2];
      for (int i = 0; i < nbin; ++i) {
        data[i][0] = i + 1;
        data[i][1] = scanner.nextInt();
      }
    }
  }
}

Upvotes: 0

Gianmarco
Gianmarco

Reputation: 2552

You are not initializing the array!!

public static int[][] data=null;

This is not properly initializing, you are "declaring" the variable. That's the same as:

public static int[][] data;

No differences.

If you want o be able to add something in your array you have to initialize it:

This is the example:

public static int[][] data;
try{
    data[1][1] = 5;
    System.out.println("Added");
}catch(NullPointerException e){
    System.out.println("First exception catched");
}
data = new int[10][10];
try{
    data[1][1] = 5;
    System.out.println("added at the second try");
}catch(NullPointerException e){
    System.out.println("Second Exception catched");
}

this code will give as result:

First exception catched
added at the second try

I how you understood why

Upvotes: 1

Related Questions