How to modify the code in else if
so that whatever I input in if
will be displayed in the condition 2?
import java.util.*;
public class REPORTS
{
public static void main(String[]args)
{
int Studentid,equipid,quantity,studentid,equipid1;
String Studentname,Studentcourse,Studentlevel,equipmentname,reservationdate,returndate;
STUDENT stud=new STUDENT(1234,"abc","abc","abc");
EQUIPMENT equip;
RESERVATION reserve;
Scanner in = new Scanner(System.in);
int x = choices();
if(x==1)
{
System.out.println("Enter Student ID:");
Studentid=in.nextInt();
in.nextLine();
System.out.println("Enter Student Name:");
Studentname=in.nextLine();
System.out.println("Enter Student Course:");
Studentcourse=in.nextLine();
System.out.println("Enter Student Level:");
Studentlevel=in.nextLine();
stud.setID(Studentid);
stud.setName(Studentname);
stud.setCourse(Studentcourse);
stud.setLevel(Studentlevel);
}
else if(x==2)
{
stud.display();
}
}
I'm thinking of using a looping but I dont know how to properly loop in order for me to fetch the data that is input by the user in the if
statement.
I changed my if else
to switch
and tried a while
loop. But the program runs endlessly and instead of displaying what I input it keeps asking for student name:
while(x!=7)
{
switch(x)
{
case 1:
{
stud.getData();
choices();
break;
}
case 2:
{
stud.display();
break;
}
}
}
A few starting points:
public static void main(String[]args)
{
int Studentid,equipid,quantity,studentid,equipid1;
String Studentname, Studentcourse, Studentlevel, equipmentname,
reservationdate, returndate;
STUDENT stud=new STUDENT(1234,"abc","abc","abc");
...
Rename your STUDENT class to Student. Also, you don't need all these local variables, they just make your code harder to read. Provide a default constructor for Student
public static void main(String[]args)
{
Student stud=new Student(); // call the default constructor, don't enter bogus data
Scanner in = new Scanner(System.in);
int x = choices();
while (x != 7) {
switch(x) {
case 1:
System.out.println("Enter Student ID:");
stud.setID(in.nextInt());
in.nextLine();
System.out.println("Enter Student Name:");
stud.setName(in.nextLine());
System.out.println("Enter Student Course:");
stud.setCourse(in.nextLine());
System.out.println("Enter Student Level:");
stud.setLevel(in.nextLine());
break;
case 2: stud.display(); break;
}
// this must be inside the loop!!
x = choices();
}
}