1

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?

dustinevan
  • 799
  • 8
  • 20
Arooj Ahmad
  • 57
  • 1
  • 7
  • 2
    This clearly Says Assignment – Ankur Anand Jul 15 '15 at 08:23
  • Have a look at `https://docs.oracle.com/javase/tutorial/essential/io/formatting.html` to format your strings. More specifically have a look at the `width` parameter to ensure that the indentation is pleasant to look at. – M. Shaw Jul 15 '15 at 08:30
  • 1
    @AnkurAnand Atleast he tried something rather than asking to do his assignment. – Uma Kanth Jul 15 '15 at 08:32
  • agree with @UmaKanth it's clearly an assignment... but OP tried at least... fail? fair enough! Everyone fails sometimes. I think is enough to help him to go in right direction... – Jordi Castilla Jul 15 '15 at 08:36
  • It is, as you have correctly surmised, an assignment for a class. My professor allows discussion on code so long as the student is really struggling through the work (exams are pencil and paper) -- as you can probably tell by my attempt here, I've tried to frame the problem and ask for help. Is that morally permissible? – Arooj Ahmad Jul 15 '15 at 15:09
  • @tharooj We really appreciate your effort. Ankur Anand told it because there are many cases on SO where people give their assignment questions and ask us to solve for them without even trying to solve it. You did your best. We do encourage people who really work hard. – Uma Kanth Jul 15 '15 at 16:05

3 Answers3

2

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));
Uma Kanth
  • 5,614
  • 2
  • 16
  • 40
SatyaTNV
  • 4,053
  • 3
  • 13
  • 30
1

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

Community
  • 1
  • 1
Jordi Castilla
  • 24,953
  • 6
  • 58
  • 97
1

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
K.Hui
  • 153
  • 1
  • 13