Answer to Question #304047 in Java | JSP | JSF for jhon

Question #304047

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


1
Expert's answer
2022-03-02T01:16:15-0500
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);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS