[HELP] Code doesn't run properly

Joined
Mar 1, 2011
Messages
1
Reaction score
0
I wrote a simple Java program like this :

public class StudentInfoEnter
{
private String studentID;
private String studentName;

//A constructor with no parameters
public StudentInfoEnter()
{
this.studentID = "";
this.studentName = "";
}

//A constructor with two required parameters
public StudentInfoEnter(String studentID, String studentName)
{
this.studentID = studentID;
this.studentName = studentName;
}

//A method that user can enter or change students' ID.
public void setStudentID(String studentID)
{
this.studentID = studentID;
}

//A method that user can enter or change students' Name.
public void setStudentName(String studentName)
{
this.studentName = studentName;
}

//A method that used to show students' information.
public void showStudentInfo()
{
System.out.println("\n - ID : "+this.studentID);
System.out.println(" - Name : "+this.studentName);
}
}


AND :

WITH FOR :

import java.util.Scanner;

public class StudentInfoView
{
public static void main(String[] args)
{
//Declare a scanner as an input method
Scanner sc = new Scanner(System.in);

//Create 2 two students' profiles
int numberOfStudent = 0;
System.out.print("How many students do you want to enter? ");
numberOfStudent = sc.nextInt();
StudentInfoEnter student1 = new StudentInfoEnter("123", "ABC");
StudentInfoEnter[] student = new StudentInfoEnter[numberOfStudent];

for (int i=0;i<numberOfStudent;i++)
{
student = new StudentInfoEnter();
System.out.print("Please Student["+i+"]'s ID : ");
student.setStudentID(sc.nextLine());
System.out.print("Please Student["+i+"]'s Name : ");
student.setStudentName(sc.nextLine());
}


//show two students' information
System.out.print("\nList of Students : ");
student1.showStudentInfo();
for (int i=0;i<numberOfStudent;i++)
{
student.showStudentInfo();
}
}

}

WITH IF :

import java.util.Scanner;

public class StudentInfoView
{
public static void main(String[] args)
{
//Declare a scanner as an input method
Scanner sc = new Scanner(System.in);

//Create 2 two students' profiles
int numberOfStudent = 0;
System.out.print("How many students do you want to enter? ");
numberOfStudent = sc.nextInt();
StudentInfoEnter student1 = new StudentInfoEnter("123", "ABC");
StudentInfoEnter[] student = new StudentInfoEnter[numberOfStudent];

int i = 0;
if (i<numberOfStudent)
{
student = new StudentInfoEnter();
System.out.print("Please Student["+i+"]'s ID : ");
student.setStudentID(sc.nextLine());
System.out.print("Please Student["+i+"]'s Name : ");
student.setStudentName(sc.nextLine());
i++;
}


//show two students' information
System.out.print("\nList of Students : ");
student1.showStudentInfo();
for (i=0;i<numberOfStudent;i++)
{
student.showStudentInfo();
}
}

}

But it seems to ignore the red line in the first loop of For. I don't why.
I have tried to change the FOR loop into a IF loop, but it still bypasses the red line in first loop. Any following loop is normal, just the first loop.
Can anyone explain this ?
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top