简体   繁体   English

将文本文件读取到Java中的int数组

[英]Read text file to an int array in Java

Given a text file as follow : 给定一个文本文件,如下所示:
10 10
100 100
99 99
99 99
96 96
96 96
92 92
92 92
91 91
88 88
87 87
86 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 . 其中第一个数字“ 10”表示文本文件包含10个整数,第二个数字100表示文本文件中的所有数字不超过100
My objective is to read the text and fill an int data[][]; 我的目标是阅读文本并填充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 这意味着索引的第一个原始值,项目值或权重的第二个值.. int cbin = \\\\the second text's valueint nbin = \\\\the first text's value
the problem that i get an Exception in thread 我在线程中得到异常的问题

"AWT-EventQueue-0" java.lang.NullPointerException “ 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 ?? 有任何想法吗 ??

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 我你怎么知道为什么

Using java.util.Scanner , statements such as Scanner scanner = new Scanner(System.in); 使用java.util.Scanner ,诸如Scanner scanner = new Scanner(System.in);语句Scanner scanner = new Scanner(System.in); , while (scanner.hasNext()) { /* ... */ } , and int n = scanner.nextInt(); while (scanner.hasNext()) { /* ... */ }int n = scanner.nextInt(); will make it a much simpler program. 将使它变得更简单。

Like so: 像这样:

import java.util.Scanner; 导入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();
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM