Seumas Frew
Seumas Frew

Reputation: 25

Make a formatted table from an array created from input file data in Java

OK, need some help here please. I'm taking an input file with data (info.txt) and storing that data in an array. I've gotten that far, but now I need to output the data in a well formatted table using the array I just made. I feel dumb, but I can't for the life of me figure out how to do this using an array and a for loop. I've made the for loop and told it how to make the table but I don't think it's right.When I run the program at it's current state it is giving me an error which I have no idea why or how to fix it.An array is not mandatory, however the way it's coded is the way the professor wanted the syntax. Yes this is homework and I've scrubbed my textbook and slides coming up empty handed and scratching my head. Can anyone help please? Below is the data and code and how the table is supposed to look. Thank you!

Data (info.txt input file.):

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

Main Class:

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();
}
}

Student Class:

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

Table (This is supposed to be all lined up. I don't know why it's formatted this way here.):

Name Score1 Score2 Score3 Total

Andy Borders 200 250 400 850

The rest of the players here in the same format.

The total number of students in this class is: 7 The average total score of the class is: 778 John Borell got the maximum score of: 1000 Bill Johnson got the minimum score of: 490 (Each of those are supposed to be on separate lines)

Upvotes: 0

Views: 454

Answers (2)

lightbringer
lightbringer

Reputation: 835

you need to use printf to format the output. Reading your code, I don't see any formatting, so Java will just print the data exactly as what you have here

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

If you want the program to output in a nice format, you can use this example code

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

Another issue:

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

I think this is where you get the exception, you only have numStudents = 9, but your loop counts from 0 to 9, which is 10 times. Change it to

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

ParseInt issue:

        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 is null because you haven't done anything to it, therefore the parseInt will throw exception. You should remove the total from constructor's argument list, and calculate it within the constructor Also, in your constructor

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

It should be

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

Upvotes: 1

Jerome
Jerome

Reputation: 1759

JakeWharton wrote an excellent tools to implement it. Here you go flip-tables

Like this:

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

Upvotes: 1

Related Questions