The output I am getting is :

You will be entering the names of ten students  their corrsponding marks in Physics, Chemistry & Maths.
Enter the name of the student : 

student1
Enter the marks in physics of STUDENT1 : 

43
Enter the marks in chemistry of STUDENT1 : 

54
Enter the marks in maths of STUDENT1 : 

65
Enter the name of the student : 

student2
Enter the marks in physics of STUDENT2 : 

54
Enter the marks in chemistry of STUDENT2 : 

64
Enter the marks in maths of STUDENT2 : 

23
1. Display according to the names.
2. Display according to the average marks.
3. Exit.
Enter your choice : 
1
Name	   Physics	 Chemistry	 Maths	 Average
------------------------------------------------------------
STUDENT2	    54	   64	    23	    47.0
STUDENT2	    54	   64	    23	    47.0
1. Display according to the names.
2. Display according to the average marks.
3. Exit.
Enter your choice :

The Student1 & its values are not being displayed & are being overwritten by student 2.
This happens in a array of size 10 as well
The last student name is being displayed.

Need some assistance with this.

Best Regards
KKR

import java.io.*;
class Student 
{
     static String name;
     static int p, c, m;
     static double avg;
      
    static void Input()throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the name of the student : \n");
        name = br.readLine();
        name = name.toUpperCase();
        System.out.println("Enter the marks in physics of "+name+" : \n");
        p = Integer.parseInt(br.readLine());
        System.out.println("Enter the marks in chemistry of "+name+" : \n");
        c = Integer.parseInt(br.readLine());
        System.out.println("Enter the marks in maths of "+name+" : \n");
        m = Integer.parseInt(br.readLine());
    }
    
    static void Display()
    {
       System.out.println(name + "\t    " + p + "\t   " + c + "\t    " + m + "\t    " + avg);   
    }
    
    static void Average()
    {
        avg = (p + c + m)/3;
    }
    
}


import java.io.*;
class prg1 extends Student
{

   public static void main(String args[])throws IOException
   {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       Student st[] = new Student[10];
       System.out.println("You will be entering the names of ten students  their corrsponding marks in Physics, Chemistry & Maths.");
       int i, j, k;
       String s1, s2;
       Student tmp;
       for(i = 0; i < 2; i++)
       {
           Student St = new Student();
           St.Input();
           st[i] = St;
           st[i].Average();
       }
       boolean exit = false;
       int ch = 0;
       int cFlag = 0;
       while (exit != true)
       {
           System.out.println("1. Display according to the names.");
           System.out.println("2. Display according to the average marks.");
           System.out.println("3. Exit.");
           System.out.println("Enter your choice : ");
           ch = Integer.parseInt(br.readLine());
           if ((ch < 1)||(ch > 3))
           {
               System.out.println("Invalid Choice! Try again.");
               exit = false;
           }
           for(i = 1; i <= 3; i++)
           {
               if (i == ch)
               switch(i)
               {
                   case 1:
                      for(j = 0; j < 2; j++)
                      {
                          for(k = j+1; k < 2; k++)
                          {
                              s1 = st[j].name;
                              s2 = st[k].name;
                              cFlag = s1.compareTo(s2);
                              if (cFlag > 0)
                              {
                                  tmp = st[k];
                                  st[k] = st[j];
                                  st[j] = tmp;
                              }
                          }
                      }
                      System.out.println("Name" + "\t   " + "Physics" + "\t " + "Chemistry" + "\t " + "Maths" + "\t " + "Average");
                      for(j = 0; j < 60; j++)
                      {
                          System.out.print("-");
                      }
                      System.out.println();
                      for(j = 0; j < 2; j++)
                      {
                          st[j].Display();
                      }
                      break;
                   case 2:
                      break;
                   case 3:
                      exit = true;
                      break;
               }
           }
       }
       System.out.println();
       System.out.println("Thank you for using the software.");
   }
}

Recommended Answers

All 4 Replies

you've set your variables as being static variables.
look into a better usage of Student: create an Student-Object that stores the same data,but uses setters and getters to handle the information you're storing.

also, you'd better place the code you've added into the Input() method in your prg1 class.

This ought to give you an idea of how to start, if you still can't finish it, we'll be ready to help you further

you've set your variables as being static variables.
look into a better usage of Student: create an Student-Object that stores the same data,but uses setters and getters to handle the information you're storing.

also, you'd better place the code you've added into the Input() method in your prg1 class.

This ought to give you an idea of how to start, if you still can't finish it, we'll be ready to help you further

You have declare the name to be static. Meaning that you will have only "one" name saved in memory. So no matter how many instances you create there will always be ONE name in memory and will hold of course the last value entered.

Each Student instance has a name and a grade. If you make them non static, each time you create a Student, you will have a different name for each of those instances.
All of those methods need to be NON-static. Because each student must have its own name and method for input

Also prg1 class shouldn't extend the Student class. You don't need to.

Yea.. That was the problem & the other was this :

st[i] = new Student();  // I missed this

Coz I missed this i was getting a null pointer exception.
To avoid that I used the static which created that messy problem.

With that line added the program works like a charm :)

Thanx guys for the responses :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.