2012-10-04 55 views
1

不久前我发布了一个问题,这个问题得到了回答,并且帮了我很大的忙。很抱歉,请尽快发布。 =/数组的结果为空?

我已经过了这个,不知道我做错了什么,打印的结果是几个'空'字。我正在尝试使用方法从文本文件中填充数据。然后在主类中运行该方法以打印出已填充的数组。

任何帮助将不胜感激,谢谢你提前。 =)

主类:

public static void main(String[] args) throws IOException { 

    ScoreProcessor scores = new ScoreProcessor(); 

    String[][] table = scores.readFile(); 


    for (int row = 0; row < table.length; row++) { 
     for (int col = 0; col < table[row].length; col++) { 
      System.out.print(table[row][col] + "\t"); 
     } 
    } 
} 

另一类中,包括方法:

public class ScoreProcessor { 

    static public String[][] readFile() throws IOException { 

     File filedata = new File("src/JavaApp2/Data.txt"); 
     Scanner file = new Scanner(filedata); 

     int row = 0, col = 0; 

     String[][] scores = new String[8][5]; 


     while (file.hasNext()) { 

      Scanner readfile = new Scanner(filedata); 
      readfile.nextLine(); 
      readfile.useDelimiter(","); 

      while (readfile.hasNext(",")) { 
       String line = readfile.next(); 


      line = scores[row][col] ; 
       col++; 
      } // end innerloop 
      row++; 
      col = 0; 


     } // end outerloop 

     return scores; 
    } // end method 
} // end class 

数据文件

Student,Q1,Q2,Q3,Q4 
abcd0001,50,55,59,72 
efgh0002,82,78,84,86 
ijkl0003,42,45,46,52 
mnop0004,66,74,72,78 
qrst0005,82,86,89,88 
uvwx0006,77,83,87,82 
yzab0007,62,64,70,68 
+0

你的Data.txt文件是怎么样的? –

+0

我认为你的内循环不会在最后一个逗号之后读取条目*。这可能是一个问题吗? – Thilo

+0

用数据编辑。 – user1719605

回答

2

正如其他人指出的,file.hasNextInt()返回false,因为您正在读取的行不是int。由于它是空的,你的代码永远不会进入循环,并且数据不会被传送。

Scanner readfile = new Scanner(filedata); 
    String line1 = readfile.nextLine(); 
    readfile = readfile.useDelimiter(","); 

不要再次打开该文件,只是读每一行并把它分解 ''

String line = file.nextLine(); 

for (String each: line.split(",")) { 
    scores[row][col] = each; 
} 
+0

非常感谢大家。它现在似乎按预期工作。 :) – user1719605

2

线

while (file.hasNextInt())

将返回false。因此你的数组不会被数据初始化并返回null打印。

如果readfile.nextLine()返回null或没有,我会建议您检查一下事实。

0

查看hasNextInt()的定义。如果下一个标记不是int,则它将返回false。

我看到它的方式,您的文件以“学生”开头,或者如果您跳过标题行,使用“abcd0001”,但不是int。因此hasNextInt()会立即返回false,并且您的内循环永远不会被执行。

除此之外,我不清楚为什么你实例化第二个Scanner

像这样的问题可以用一个调试器很容易被追踪到,我建议你熟悉这种方法:)

0

使用file.hasNext()insted的file.hasNextInt()的,它应该解决的问题

1

试试这个:

public class ScoreProcessor { 

     public static String[][] readFile() throws IOException { 

      File filedata = new File("src/JavaApp2/Data.txt"); 
      Scanner file = new Scanner(filedata); 

      int row = 0; 

      String[][] scores = new String[8][5]; 

      while(file.hasNextLine()){ 
       String line=file.nextLine(); 
       scores[row]=line.split(","); 
       row++; 
      } 

      return scores; 
     } 

     public static void main(String[] args) throws IOException { 

      String[][] table = readFile(); 

      for (int row = 0; row < table.length; row++) { 
       for (int col = 0; col < table[row].length; col++) { 
        System.out.print(table[row][col] + "\t"); 
       } 
       System.out.print("\n"); 
      } 
     } 

    } 
0

是不是也该(除HasNextInt())问题的核心:

line = scores[row][col] ; 

shou也可以用其他方式。这样你的数组永远不会被填充,当然总是只包含空值。

scores[row][col] = line;