-1

对于家庭作业,我需要阅读以下格式的文件:

Miller
William
00001
891692 06 <--this is supposed to be the dollar amount in the account

我需要找到一种方法来分割每一美元的金额,也就是每 4 行。

4

2 回答 2

3

使用扫描仪

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++;
    }

 }
}
于 2012-10-11T21:28:13.947 回答
2

您可以使用 aBufferedReader来读取文件内容,同时lineCount为每一行维护 a 。然后String.split在每 4 行使用:

if (lineCount % 4 == 0) {
   String[] dollarAmount = String.split(" ");
}
...
于 2012-10-11T21:28:29.913 回答