Java,根据文件制作银行账户

时间:2012-10-11 21:23:57

标签: java arrays string split

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

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

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

2 个答案:

答案 0 :(得分: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++;
    }

 }
}

答案 1 :(得分:2)

您可以使用BufferedReader读取文件内容,同时为每行保留lineCount。然后在每4行使用String.split

if (lineCount % 4 == 0) {
   String[] dollarAmount = String.split(" ");
}
...
相关问题