steelersrawk1 steelersrawk1 - 8 days ago 5
Java Question

Creating a program to read through Integers and Strings in Java

I am trying to create a program that will read from a .txt file that is formatted as such:


Total number of students

Name

Score1

Score2

Score3

Name

Score1

etc


My current code is this:

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

public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("Enter file name: ");
String filename = in.nextLine();
File filetest = new File(filename);
Scanner imp = new Scanner(filetest);
List<String> studentList = new ArrayList<String>();
List<Integer> studentScores = new ArrayList<Integer>();
String total = imp.nextLine();
int i = 0;
try {
while (imp.hasNext()) {
if (imp.hasNextInt()) {
studentScores.add(imp.nextInt());
} else {
studentList.add(imp.nextLine());
i++;
}
}
} finally {
System.out.println("Name\t\tScore1\t\tScore2\t\tScore3");
System.out.println("-------------------------------------------------------");
System.out.println(total);
System.out.println(studentList.get(0) + "\t" + studentScores.subList(0, 3));
System.out.println(studentList.get(2) + studentScores.subList(3, 6));
System.out.println(studentList.get(4) + studentScores.subList(6, 9));
System.out.println(studentList.get(6) + studentScores.subList(9, 12));
imp.close();
in.close();
}

}
}


The format I want to display into the console is to list the name, then the three scores that student received, and to repeat it, but right now it is hard-coded just for the amount of students that are currently there, and I need it to be able to create output regardless of how many students there are.

Current output:


Total

Name [score1 score2 score3]

etc


Desired output:


Total

Name score1 score2 score3 (rather than with the [] )

etc


Any help is greatly appreciated.

Answer

More structural way to do this :

public class Project5 {

    static class Student {

        private String name;
        private final List<Integer> scores;
        private int total;

        public Student() {
            scores = new ArrayList<>();
            total = 0;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void addScore(int score) {
            scores.add(score);
            total += score;
        }

        public String getName() {
            return name;
        }

        public List<Integer> getScores() {
            return scores;
        }

        public int getTotal() {
            return total;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder(name).append('\t').append(total);
            for (Integer score : scores) {
                sb.append('\t').append(score);
            }
            return sb.toString();
        }

    }

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter file name: ");
        String filename = in.nextLine();
        in.close();

        File filetest = new File(filename);
        Scanner imp = new Scanner(filetest);
        int total = Integer.parseInt(imp.nextLine());

        System.out.println("Name\tTotal\tScore 1\tScore 2\tScore 3");

        for (int i = 0; i < total && imp.hasNextLine(); i++) {
            Student student = new Student();
            student.setName(imp.nextLine());
            while (imp.hasNextInt()) {
                student.addScore(imp.nextInt());
            }
            if (imp.hasNext()) {
                imp.nextLine();
            }
            System.out.println(student);
        }
        imp.close();
    }

}