Arooj Ahmad
Arooj Ahmad

Reputation: 57

How to display grades in arrays?

Sample output from professor: Sample output given scores.txt file

My code so far:

import java.util.*;
import java.io.*;
import java.text. DecimalFormat;
public class HW10 {
    public static void main(String[] args) throws IOException {

        System.out.println("Which file would you like to read?");

        Scanner keyboard = new Scanner(System.in);
        String name = keyboard.next();
        Scanner reader = new Scanner(new File(name));

        double numbers[][] = new double[reader.nextInt()+1][reader.nextInt()+1]; //+1 for averages

        //loop over all the rows
        for (int i=0; i<numbers.length; i++) { 
            //loop over all the columns in row i
            for (int j=0; j<numbers[i].length; j++) { 
                //this code will execute on each and every cell row i, column j
                numbers[i][j] = reader.nextDouble();
            }
            double sum = 0;
            double average = (sum/(numbers.length));
        }
        //loop over all the rows
        for (int i = 0; i<numbers.length-1; i++) { 
            System.out.println("Assignment #: ");
            System.out.println("array 1, 2, 3, 4, 5, 6, Avg");
            System.out.println("Student 1: ");
            System.out.println("Student 2: ");
            System.out.println("Student 3: ");
            System.out.println("Student 4: ");
            System.out.println("Student 5: ");
            System.out.println("Average: ");

            for (int j=0; j<numbers[i].length-1; j++) { //loop over all the columns in row i
                System.out.print(numbers[i][j] + " "); //print out the cell value and then a space
            }
            //System.out.println("The overall class average is " + (totalResults/(numbers.length))); //the class average
            DecimalFormat numPattern = new DecimalFormat("##.#");
        }
        //overall average code here
    }
}

In order to display all of the content properly, the arrays need to be displayed such that the headings and print lines all make sense. I've done this by putting some placeholders into where I'm not understanding how to display the data.

Though the above code compiles, it gives me an exception error when the scores.txt file is inputted for the name -- the working directory contains the file, so I'm not sure why it's not moving past that in the program, either.

I think I'll be able to display the array just fine if I can get past that hurdle, but I seek the Stack's advice about how my code looks syntactically. Any insight for improvement?

Upvotes: 1

Views: 2100

Answers (3)

K.Hui
K.Hui

Reputation: 153

You can use String.format()

System.out.print(String.format("%10s", numbers[i][j]+""));

You can try this and see

  System.out.println(String.format("%50s","Assignment #: "));
  System.out.println(String.format("%10s%10s%10s%10s%10s%10s%10s%10s","array", "1", "2", "3", "4", "5", "6", "Avg"));
  System.out.println(String.format("%10s","Student 1: "));
  System.out.println(String.format("%10s","Student 2: "));
  System.out.println(String.format("%10s","Student 3: "));
  System.out.println(String.format("%10s","Student 4: "));
  System.out.println(String.format("%10s","Student 5: "));
  System.out.println(String.format("%10s","Student 6: "));

Btw, I think your for loop of printing the table is wrong.
In your code, "Assignment #:" and "array 1, 2, 3, 4, 5, 6, Avg" will repeat.
Also, your should print the marks of the Student before starting a new line

Pseudocode of the for loop of printing the marks:

print "Assignment #:"
print " array 1, 2, 3, 4, 5, 6, Avg "
for i= 1-5 //rows
    print "Student " + i
    for j= 1-6 //column
        print score of assignment j of student i
    end for
    print avg
end for
print "Overall Average" + overallAverage

Upvotes: 1

SatyaTNV
SatyaTNV

Reputation: 4135

to format ouput use String.format().

System.out.println(String.format("%4d", 5));// for int
System.out.println(String.format("%-20s= %s" , "label", "content" ));//for string

Where

%s is a placeholder for you string.

The '-' makes the result left-justified.

20 is the width of the first string

see link

The another question was regarding the file opening.

Giving the file name only doesn't help. Try giving the whole path.

Get the path using System.getProperty("user.dir");

String name = keyboard.next();
String path = System.getProperty("user.dir");
Scanner reader = new Scanner(new File(path + " \\ " + name));

Upvotes: 2

Jordi Castilla
Jordi Castilla

Reputation: 27003

Use \t to put tabs in your text...

System.out.println("\t\t1\t2\t3\t4\t5\t6\tAvg");

Will output:

          1    2    3    4    5    6    Avg

You can also add a pad:

public static void main(String args[]) throws Exception {
 System.out.println(padRight("Howto", 20) + "*");
 System.out.println(padLeft("Howto", 20) + "*");
}
/*
  output :
     Howto               *
                    Howto*
*/

Check this question to know more about padding Strings.

HINT: to reach your result you must print each line with all info in a row OR put each student info in a variable and print them at the end.

Check this answer to know how to manipulate String and use StringBuilder

Upvotes: 1

Related Questions