2015-09-04 126 views
-3

我正在使用java從文件中讀取int

我有一個這樣的整數的文件。

1 2 

3 4 

5 6 

我怎麼會:

  • 從文件中讀取第一個詮釋?
  • 從文件中讀取第二個int?
  • 跳過第一行並將其他數字放入二維數組中?

這是我到目前爲止。我不知道如何從文件中讀取第二個int,並將剩下的放在二維數組中。

Scanner f = new Scanner(new File(fName)); 

//read first number 
int num1 = f.nextInt(); 

//skip first line and put rest in 2d array 
while(f.hasNextInt()){ 
    a[i++] = f.nextInt(); 
} 
+0

這是什麼語言?請給它加標籤。 – crafter

+1

對我來說看起來像java,如果我不得不猜測 –

+0

「*我不知道如何從文件中讀取第2個int,並將其餘部分放在2d數組中。」* - 只需添加'int num2 = f.nextInt(); '就在循環之前? –

回答

0
int[][] array = new int[2][2]; 
Scanner f = new Scanner(new File(fName)); 

//skip first line(2 digits that's why two times 
f.nextInt(); 
f.nextInt(); 


int i = 0; 
while(f.hasNextInt()){ 
    int a = f.nextInt(); 
    int b = f.nextInt(); 
    a[i++] = new int[]{a, b}; 
}