从Java中的输入文件数据创建的数组中创建格式化表

时间:2014-12-05 00:30:34

标签: java arrays io

好的,请在这里需要一些帮助。我正在获取带有数据(info.txt)的输入文件并将该数据存储在一个数组中。我已经走得那么远了,但现在我需要使用我刚制作的数组在格式良好的表格中输出数据。我觉得愚蠢,但我不能为我的生活找出如何使用数组和for循环来做到这一点。我已经制作了for循环并告诉它如何制作桌子,但我不认为它是正确的。当我按照目前的状态运行程序时,它给了我一个错误,我不知道为什么或如何解决它。数组不是强制性的,但它编码的方式是教授想要语法的方式。是的,这是家庭作业,我已经擦过我的教科书,幻灯片空手而来,挠挠我的头。有人可以帮忙吗?下面是数据和代码以及表格的外观。谢谢!

数据(info.txt输入文件。):

9
Andy Borders
200
250
400
John Smith
120
220
330
Alvin Smith
225
300
278
Mike Borell
250
250
500
Jim Jones
325
325
155
Robert Fennel
200
150
350
Craig Fenner
230
220
480
Bill Johnson
120
150
220
Brent Garland
220
240
350

主类:

import java.util.Scanner;
import java.io.*;
public class Project5 {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the file name: ");
    String fileName = in.nextLine();
    try {
        File file = new File(fileName);
        Scanner inputFile = new Scanner(file);
        String SnumStudents = inputFile.nextLine();
        int numStudents = Integer.parseInt(SnumStudents);
        Student [] studentList = new Student[numStudents];
        for (int i = 0; i <=numStudents; i++)
        {
            String line = inputFile.nextLine();
            String score1 = inputFile.nextLine();
            String score2 = inputFile.nextLine();
            String score3 = inputFile.nextLine();
            String total = null;
            studentList [i] = new Student(line, Integer.parseInt(score1), Integer.parseInt(score2), Integer.parseInt(score3), Integer.parseInt(total));
        }
        System.out.println("Name\t\tScore1\tScore2\tScore3\tTotal");
        System.out.println("---------------------------------------------");
        for (int i=0; i< studentList.length;i++){
            System.out.println(studentList[i].getName() + "\t" + studentList[i].getScore1() + "\t" + studentList[i].getScore2() + "\t" + studentList[i].getScore3() + "\t" + studentList[i].getTotal());
        }
        System.out.println("---------------------------------------------");
        inputFile.close();
    } catch (IOException e) {
        System.out.println("There was a problem reading from " + fileName);
    }
    finally {
    }
    in.close();
}
}

学生班:

public class Student {
private String name;
private int score1;
private int score2;
private int score3;
private int total;

public Student(String n, int s1, int s2, int s3, int t){
    name = n;
    score1 = s1;
    score2 = s2;
    score3 = s3;
    total = t;
    t = s1 + s2 + s3;
}

public String getName(){
    return name;
}
public int getScore1(){
    return score1;
}
public int getScore2(){
    return score2;
}
public int getScore3(){
    return score3;
}
public int getTotal(){
    return total;
}
}

表格(这应该是全部排成一行。我不知道为什么它会以这种方式格式化。):

名称得分1得分2得分3总计

Andy Borders 200 250 400 850

其余玩家的格式相同。

本课程的学生总数为:7 该班的平均总分为:778 John Borell的最高得分为:1000 比尔约翰逊获得了最低分:490分(其中每一分都应该分开)

2 个答案:

答案 0 :(得分:1)

您需要使用printf格式化输出。 阅读您的代码,我没有看到任何格式,因此Java将只打印与您在此处的数据完全相同的数据

System.out.print(studentList[i].getName() + ":" + studentList[i].getScore1() + ":" + studentList[i].getScore2() + ":" + studentList[i].getScore3());

如果您希望程序以漂亮的格式输出,则可以使用此示例代码

System.out.printf("%30s %10s %10s %10s\n", studentList[i].getName(), studentList[i].getScore1(), studentList[i].getScore2(), studentList[i].getScore3());

另一个问题:

for (int i = 0; i <=numStudents; i++)

我认为这是你得到异常的地方,你只有numStudents = 9,但是你的循环从0到9计数,这是10倍。将其更改为

for (int i = 0; i < numStudents; i++)

ParseInt问题:

        String line = inputFile.nextLine();
        String score1 = inputFile.nextLine();
        String score2 = inputFile.nextLine();
        String score3 = inputFile.nextLine();
        String total = null;
        studentList [i] = new Student(line, Integer.parseInt(score1), Integer.parseInt(score2), Integer.parseInt(score3), Integer.parseInt(total));

total为null,因为您没有对它做任何事情,因此parseInt将抛出异常。您应该从构造函数的参数列表中删除总计,并在构造函数中计算它 另外,在你的构造函数

public Student(String n, int s1, int s2, int s3, int t){
    name = n;
    score1 = s1;
    score2 = s2;
    score3 = s3;
    total = t;
    t = s1 + s2 + s3;
}

应该是

public Student(String n, int s1, int s2, int s3){
    name = n;
    score1 = s1;
    score2 = s2;
    score3 = s3;
    total = s1 + s2 + s3;
}

答案 1 :(得分:1)

JakeWharton写了一个很好的工具来实现它。在这里你flip-tables

像这样:

String[] headers = { "Test", "Header" };
String[][] data = {
   { "Foo", "Bar" },
   { "Kit", "Kat" },
};
System.out.println(FlipTable.of(headers, data));
╔══════╤════════╗
║ Test │ Header ║
╠══════╪════════╣
║ Foo  │ Bar    ║ 
╟──────┼────────╢
║ Kit  │ Kat    ║
╚══════╧════════╝
相关问题