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.");
}
}