Java 是否在循环中打印格式化数组值?

Java 是否在循环中打印格式化数组值?,java,Java,我正在通过一本书自学java,它有一个图表,我应该通过查找数组值和从文本文件循环显示来复制它。我所有的字段都打印得很好,但我似乎无法将它们分开 我也知道自己该做什么,但只是没有做我想做的 public static void main(String[] args) { Scanner input = null; File empFile = new File("employeesSalesRep.txt"); String[][] empArray = new Stri

我正在通过一本书自学java,它有一个图表,我应该通过查找数组值和从文本文件循环显示来复制它。我所有的字段都打印得很好,但我似乎无法将它们分开

我也知道自己该做什么,但只是没有做我想做的

public static void main(String[] args) {
    Scanner input = null;
    File empFile = new File("employeesSalesRep.txt");
    String[][] empArray = new String[9][6];

    try {
        input = new Scanner(empFile);
        for (int outer = 0; input.hasNext() == true; ++outer) {
            for (int inner = 0; inner < empArray[outer].length; ++inner) {
                empArray[outer][inner] = input.next();
            }

            if (empArray[0][4].equals("22")) {

            }


        }
    }

    catch (FileNotFoundException e) {
        System.out.println("Cannot find file.  File must be in your PROJECT folder!");
        System.out.println("System out print: " + e.getMessage());
        System.err.println(e.getMessage());
        System.exit(0);
    }

    finally {
        input.close();
    }

    System.out.printf("%-13s", "Emp #");
    System.out.printf("%-13s", "Last Name");
    System.out.printf("%-13s", "First Name");
    System.out.printf("%-13s", "MI");
    System.out.printf("%-13s", "Sales Rep");
    System.out.printf("%-13s", "Commision");
    System.out.println();
    System.out.printf("%-13s", "---------");
    System.out.printf("%-13s", "---------");
    System.out.printf("%-13s", "-----------");
    System.out.printf("%-13s", "---------");
    System.out.printf("%-13s", "----------");
    System.out.printf("%-13s", "-----------");
    System.out.println();

    for (int x = 0; x < empArray.length; ++x) {

        if (empArray[x][0] == null)
            break;

        for (int i = 0; i < empArray[x].length; ++i) {
            System.out.printf("%-13s", empArray[x][i]);
        }
        System.out.println();
    }
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=空;
File empFile=新文件(“employeesSalesRep.txt”);
字符串[][]empArray=新字符串[9][6];
试一试{
输入=新扫描仪(EMP文件);
for(int outer=0;input.hasNext()==true;++outer){
对于(int-inner=0;inner
我不需要直接的回答。只是一些方向。给我一个代码的基础,我会做剩下的。我真的很想学这个

(还有,如果能帮你找到如何累计总数,那就太好了!)

谢谢

编辑:
我算出了格式部分。但是现在我在积累价值方面遇到了困难


你为什么不一行一行地打印,而不是存储在数组中,通过拆分并使用最后一个索引相加得到总数

double total = 0;

while (input.hasNextLine()){
    String line = input.nextLine();

    if ("".equals(line)){
        System.out.println();
    } else {
        String[] split = line.split("\\s+");
        total += Double.parseDouble(split[5].trim());  // last index
        System.out.println(line);
    }

    System.out.println("-- Total for ALL SALES " + total);
}

您可以创建一个变量来跟踪sales rep的最后一个值。当它发生变化时(即在22和33之间),您可以打印额外的行来分隔该表。您还可以为total添加一个变量,并在更改sales rep的最后一个值时将其重置为零。

您当前的输出是什么样子的?请选中编辑。计算出格式如果格式完全相同,为什么不扫描整行,如果该行不是以
Emp.
开头,则打印该行的系统。这是最简单的方法。不需要数组:),为了避免代码重复,请将该变量初始化为零。这样,标题也将在第一次迭代期间打印出来。这样可以避免重复打印标题的代码。