Please correct my code. I want to enter the name of one student and want to see his detail

0 sharoon amjid · May 2, 2015
import java.util.Scanner;

public class Students {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);

String[] iftekhar = { "Subject : BCS", "College Num : 1243",
"Age : 21", "Term : 5th Semester", "Location : Gulberg" };
String[] ali = { "Subject : BCS", "College Num : 8942", "Age : 20",
"Term : 5th Semester", "Location : Gulberg" };
String[] salman = { "Subject : BCS", "College Num : 1245", "Age : 22",
"Term : 5th Semester", "Location : Warsak Road" };
String[] najeeb = { "Subject : BCS", "College Num : 8976", "Age : 20",
"Term : 5th Semester", "Location : Civil Quarter" };
String[] shahbaz = { "Subject : BCS", "College Num : 7854", "Age : 23",
"Term : 5th Semester", "Location : University Road" };

System.out.println("List of students\n");
String[] students = { "iftekhar", "ali", "salman", "najeeb", "shahbaz" };
for (int i = 0; i < students.length; i++) {
System.out.println(students);
}
System.out.println("\nEnter the name of student to see his details");
String name = input.nextLine();

if (name.equals(iftekhar))
for (int i = 0; i < iftekhar.length; i++) {
System.out.println(iftekhar);
}
else if (name.equals(ali)) {
for (int i = 0; i < ali.length; i++) {
System.out.println(ali);
}
} else if (name.equals(salman)) {
for (int i = 0; i < salman.length; i++) {
System.out.println(salman);
}
} else if (name.equals(najeeb)) {
for (int i = 0; i < najeeb.length; i++) {
System.out.println(najeeb);

}

} else if (name.equals(shahbaz)) {
for (int i = 0; i < shahbaz.length; i++) {
System.out.println(shahbaz);
}

} else
System.out.println("Not matched");

}
}

Post a Reply

Replies

Oldest  Newest  Rating
0 hrday sunil · May 2, 2015
You have to change the println statement in the bodies of your for loops so that the array's name is followed by square brackets with the variable i as the index. Unfortunately , I can't type out how this actually looks as for some reason the square brackets with 'i' inside it doesn't display in my reply.

The counter variable ( i ) is incremented every time and after the change in the code acts as an index for printing the contents of the array in order. So it would print out iftekhar[0] then iftekhar[1] then iftekhar[2] and so on.

Making the same changes for the other students' arrays would solve the problem.
  • 1

Java / Android Development

107,263 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator