-1

I am working on a project in Java that requests user inputs information like name, id, score in array.I need to help about calculate a average grade that user input and how to find out who have highest score. Here is my code:

package finalproject;
import java.util.Scanner;
/**
 *
 * @author vincephng
 */
public class FinalProject {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) 
    {

        Scanner input= new Scanner(System.in);
        Cis84[] student= new Cis84[50];

        int option;

        for (int c = 0; c<50;c++)
        student[c]=new Cis84();

        do{
            System.out.print("");
            System.out.println("1) Add Information");
            System.out.println("2) Show report");
            System.out.println("3) Exit");
            System.out.print("\nEnter option: ");


            option = input.nextInt();

            switch(option)
            {
                case 1:
                    String n;

                    double g;
                    int index, i;

                    System.out.println("Which position of the student?");
                    index=input.nextInt();

                    System.out.println("What is the student's name:");
                    n=input.nextLine();
                    n=input.nextLine();

                    System.out.println("What is student's Id");
                    i=input.nextInt();


                    System.out.println("What is student's score");
                    g=input.nextDouble();

                    student[index].setName(n);
                    student[index].setGrade(g);
                    student[index].setId(i);
                    break;

                case 2:
                    for(int c=0; c<50;c++)
                    System.out.println(student[c]);
                    break;
                case 3:
                    System.out.println("You are done");
                    break;
                default:
                    System.out.println("Try again");
                    break;
            }

        }while (option != 3);



    }
}   

and class

package finalproject;


public class Cis84 
{
    private String name;
    private int id;
    private double grade;

    public Cis84()
    {
        name="not input yet";
        id= 00000;
        grade= 0.0;
    }
    public Cis84(String n, int i, double g)
    {
       name= n;
       id= i;
       grade=g;
    }

    public void setName(String n)
    {
        name=n;
    }
    public void setId(int i)
    {
        id=i;
    }
    public void setGrade(double g)
    {
        grade=g;
    }

    public String getName()
    {
        return name;
    }
    public int getId()
    {
        return id;
    }
    public double getGrade()
    {
        return grade;
    }

    public String toString()
    {
        return String.format("%s\n%d\n%.2f\n", name, id, grade);
    }
}
vincephng
  • 19
  • 2
  • 6

4 Answers4

1

Your toString method isn't defined correctly - it should be toString, with a lower case t, not ToString as you currently have. This is, by the way, why it's recommended to use the @Override annotation when overriding a superclass' method. In case you made a trivial mistake (such as a typo, or mixing up cases like you did here), the compiler would have alerted you that you're doing something wrong, instead of just allowing you to declare another method, which has nothing wrong with it per se, except for not being the method you want to override.

To sum up, here's how the code should look:

@Override // override annotation
public String toString() { // correct method name
    return String.format("%s\n%d\n%.2f\n", name, id, grade);
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Please do not answer duplicates (or typo questions for that matter), you should close instead, as per the [FAQ](http://meta.stackexchange.com/a/10844) – Tunaki May 16 '16 at 19:23
1

Your error is here:

You wrote this:

public String ToString() { 
    return String.format("%s\n%d\n%.2f\n", name, id, grade); 
}

And that is not the method toString() of the class object.. (note the difference in the name, java defines methods camelCase but you did it PascalCase)

You mean instead

@Override
public String toString() { 
     return String.format("%s\n%d\n%.2f\n", name, id, grade); 
}

After that you will be able to see the content of student[c]

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Please do not answer duplicates (or typo questions for that matter), you should close instead, as per the [FAQ](http://meta.stackexchange.com/a/10844) – Tunaki May 16 '16 at 19:23
0

You should override public String toString() method for Cis84 class. Then you can use one of the following ways:

for(Cis84 std: student){
    System.out.println(std);
}

or

Arrays.deepToString(student);
STaefi
  • 4,297
  • 1
  • 25
  • 43
0

In place of

case 2:
                    for(int c=0; c<50;c++)
                    System.out.println(student[c]);
                    break;

Try this:

case 2:
                    for(int c=0; c<50;c++)
                    System.out.println(student[c].getName()+" "+student[c].getId()+" "+student[c].getGrade());
                    break;

You are printing the object but actually you want the data which is inside that object.

sAm
  • 319
  • 2
  • 20