A teacher asked the class leader to prepare the list of student details (name, Roll number, Branch and Sem) of his section by using read method and also to display the details of the students using java program
public class Student {
private String name;
private int rollNumber;
private String branch;
private int sem;
public Student(String name, int rollNumber, String branch, int sem) {
this.name = name;
this.rollNumber = rollNumber;
this.branch = branch;
this.sem = sem;
}
@Override
public String toString() {
return name + " " + rollNumber + " " + branch + " " + sem;
}
}
import java.util.Scanner;
public class Main {
public static void read() {
Scanner in = new Scanner(System.in);
System.out.println("Number of students:");
Student[] students = new Student[Integer.parseInt(in.nextLine())];
for (int i = 0; i < students.length; i++) {
System.out.println("Name:");
String name = in.nextLine();
System.out.println("Roll number:");
int rollNumber = Integer.parseInt(in.nextLine());
System.out.println("Branch:");
String branch = in.nextLine();
System.out.println("Sem:");
int sem = Integer.parseInt(in.nextLine());
students[i] = new Student(name, rollNumber, branch, sem);
}
for (Student student : students) {
System.out.println(student);
}
}
}
Comments
Leave a comment