对于家庭作业,我需要阅读以下格式的文件:
Miller
William
00001
891692 06 <--this is supposed to be the dollar amount in the account
我需要找到一种方法来分割每一美元的金额,也就是每 4 行。
使用扫描仪
import java.util.Scanner;
public class ScannerEx {
public static void main(String[] args) {
Scanner scanner = new Scanner(new File("input.txt"));
int count = 1;
while(scanner.hasNextLine()) {
String nextLine = scanner.nextLine();
if(count % 4 == 0) {
//Dollar amount in nextLine
}
count++;
}
}
}
您可以使用 aBufferedReader
来读取文件内容,同时lineCount
为每一行维护 a 。然后String.split
在每 4 行使用:
if (lineCount % 4 == 0) {
String[] dollarAmount = String.split(" ");
}
...