0
如果你有一個文件,第一行是數據集的數量,其餘的,你必須乘以和輸出兩個整數,你會怎麼做?如何在文件ijn java中分割數字?
The file is
9
10 2
11 1
8 2
7 9
-1 10
3 9
6 3
18 2
7 3
And the output is supposed to be
20
11
16
63
-10
27
18
36
21
(注意這是在Java中,並且如果在輸入第一數字被改變時,與該輸出的變化,並且數字數應該是與其他數字互換)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Basic_Attempt {
public static void main(String[] args) {
// Location of file to read
File file = new File("data.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
我可能會使用太多的代碼來模擬文件,但我需要知道如何將掃描器輸入實際拆分爲數組,以及如何檢查每行中的內容。有沒有辦法在java中做到這一點?
問題要求代碼必須表現出對問題的理解最小正在解決。包括嘗試解決方案,爲什麼他們沒有工作,以及預期的結果。 – initramfs
我開始閱讀[Basic I/O](http://docs.oracle.com/javase/tutorial/essential/io/),用['String'](http:// docs.oracle.com/javase/7/docs/api/java/lang/String.html)和['Integer'](http://docs.oracle.com/javase/7/docs/api/java/lang /Integer.html)類,並敲出一些東西... – MadProgrammer
如果我不得不這樣做,我不會這樣做。 –