2015-10-21 64 views
0
FileReader myReader = new FileReader(myReaderRef); 
    Scanner input = new Scanner(myReader); 
    int arraySize = 0; 
    while(input.hasNextLine()){ 
     arraySize++; 
     input.nextLine(); 
    } 

    int[] numbers; 
    numbers = new int[arraySize]; 

    while(input.hasNextLine()){ 
     for(int i=0; i < numbers.length; i++){ 
      numbers[i] = input.nextInt(); 
     } 
    } 
    input.close(); 
    System.out.print(numbers[1]); 
} 
} 

和文本文件,它是讀取來自內容如下:掃描儀未賦值到陣列

10 
2 
5 
1 
7 
4 
9 
3 
6 
8 

每當我是System.out.print使用到陣列插槽輸出之一,它僅給出我0無論我呼叫哪個陣列位置。我哪裏錯了?

編輯:我不得不關閉並重新啓動文件閱讀器和掃描儀。謝謝您的幫助!

回答

1

您需要重新計算行數後重新啓動Scanner(因爲它位於File的末尾)。

Scanner input = new Scanner(myReader); 
int arraySize = 0; 
while(input.hasNextLine()){ 
    arraySize++; 
    input.nextLine(); 
} 
input.close(); // <-- close it. 
myReader = new FileReader(myReaderRef); // Create a new FileReader 
input = new Scanner(myReader); // <-- create another scanner instance 
+0

只是做了上,我仍然得到0的所有插槽。 –

+0

如果你做'input.close()'文件讀取器不會被關閉嗎?這意味着你必須再次初始化它... – Codebender

+0

我只是做了這個codebender。它的工作 –

2

您嘗試通過文件兩遍而不回到開頭。第一次是計算行數,第二次是讀取數據。由於您不回到文件的開頭,所以沒有數據被加載/存儲在任何地方。所以你應該重新開始你的Scanner,例如。關閉並重新打開。

或者您可能需要考慮使用ArrayList,因此您只需要讀取一次該文件。

List<Integer> numbers = new ArrayList<Integer>(); 
Scanner input = new Scanner(myReader); 
while (input.hasNextLine()) { 
    numbers.add(input.nextInt()); 
    input.nextLine(); // You might need this to get to the next line as nextInt() just reads the int token. 
} 
input.close() 
+0

即時通訊不太熟悉數組列表,所以我只是試驗我迄今爲止所知道的。在得到我目前正在使用的東西之後,我可能會考慮嘗試一下。感謝您的輸入! –

0

在您的第一個while循環遊標已經到達文件的最後一行。所以它在下一個循環之後無法找到任何東西。所以你必須創建2個Scanner對象。

 Scanner input = new Scanner(new File("D:\\numbers.txt")); 
     int arraySize =0; 
     while(input.hasNextLine()){ 
      arraySize++; 
      input.nextLine(); 
     } 

     int[] numbers; 
     numbers = new int[arraySize]; 
     input.close(); 
     Scanner input2 = new Scanner(new File("D:\\numbers.txt")); 
     while(input2.hasNextLine()){ 
      for(int i=0; i < numbers.length; i++){ 
       numbers[i] = input2.nextInt(); 
      } 
     } 
     input2.close(); 
     System.out.print(numbers[1]); 
-1

我試圖儘可能簡化問題。

爲您的情況添加/刪除元素到數組最簡單的方法是使用ArrayList對象。通讀評論並運行項目。

以下整數的第一個列表是文件的原始輸入。

第二個列表包含數組的打印語句。這些答案可能不是,你希望他們被編入索引,但我敢肯定,這將讓你在正確的道路:)

10 
2 
5 
1 
7 
4 
9 
3 
6 
8 

[10, 2, 5, 1, 7, 4, 9, 3, 6, 8] 
    3 
    1 
    7 
    5 
    2 
    8 







package cs1410; 

    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.util.ArrayList; 
    import java.util.Scanner; 

    import javax.swing.JFileChooser; 

    public class ArrayReader { 

     public static void main(String[] args) throws FileNotFoundException { 
      // Creates an array list 
      ArrayList<Integer> answer = new ArrayList<Integer>(); 

      // Reads in information 
      JFileChooser chooser = new JFileChooser(); 
      if (JFileChooser.APPROVE_OPTION != chooser.showOpenDialog(null)) { 
       return; 
      } 
      File file = chooser.getSelectedFile(); 



      // Scan chosen document 
      Scanner s = new Scanner(file); 
      int count = 0; 


      // Scans each line and places the value into the array list 
      while (s.hasNextLine()) { 
       int input = s.nextInt(); 
       answer.add(input); 
      } 

      // Kaboom 
      System.out.println(answer); 
      System.out.println(answer.indexOf(1)); 
      System.out.println(answer.indexOf(2)); 
      System.out.println(answer.indexOf(3)); 
      System.out.println(answer.indexOf(4)); 
      System.out.println(answer.indexOf(5)); 
      System.out.println(answer.indexOf(6)); 
     } 

    }