Implementing Student Record in Java program

Implementing Student Record in Java program



Topic : Total View : 105
import java.util.*;

public class Student
{
    private String m_name;
    private int m_age;
    private String m_course;
    private String m_year;
    private String m_section;

    public Student( String name, int age, String course, String year, String section )
    {
        m_name = name;
        m_age = age;
        m_course = course;
        m_year = year;
        m_section = section;
    }

    public String getName()
    {
        return m_name;
    }

    public int getAge()
    {
        return m_age;
    }

    public String getCourse()
    {
        return m_course;
    }

    public String getYear()
    {
        return m_year;
    }

    public String getSection()
    {
        return m_section;
    }

    public String toString()
    {
        return "name: " + m_name + ", age: " + m_age + 
               ", course: " + m_course + ", year: " + m_year +
               ", section: " + m_section;
    }

    public static void main(String[] args) 
    {
       ArrayList<Student> students = new ArrayList<Student>();
       Scanner input = new Scanner(System.in);

       int menuChoice = 4;
       do {
           System.out.println("			Student Record Menu");
           System.out.println("		1. Add Student	2. View Students	3. Search Student	4. Exit");
           try {
               System.out.println("Enter a choice: ");
               menuChoice = Integer.parseInt(input.nextLine());
           } catch (NumberFormatException e) {
               continue;
           }

           if (menuChoice==1)
           {
               System.out.println("Full name:");
               String name = input.nextLine();

               int age = -1;
               do {
                   try {
                       System.out.println("Age:");
                       age = Integer.parseInt(input.nextLine());
                   } catch (NumberFormatException e) {
                       System.out.println("Enter a number!");
                       continue;
                   }
               } while (age <= 0);

               System.out.println("Course:");
               String course = input.nextLine();

               System.out.println("Year:");
               String year = input.nextLine();

               System.out.println("Section:");
               String section = input.nextLine();

               Student student = new Student(name, age, course, year, section);
               students.add(student);

           } else if (menuChoice==2) {
               System.out.println("Students:");
               for (Student student : students)
               {
                   System.out.println(student);
               }
           }
       } while (menuChoice<4);
    }
}
This was implemented independently.
The interesting part is in main() and the use of Scanner. This solution also uses an ArrayList to store the results, and a for-each loop to iterate through them.
We want to call nextLine() to collect an entire line of input at a time. Calling next() will return the next token, and supposing the user enters a series of n words (a space delimited list of tokens) it will consume the next n calls to next().

You saw it skip over name and go straight to age for a similar reason - the call to nextInt() captured your menu choice after the user hits enter but it did not capture the line return itself - the subsequent call to nextLine() did that (consuming both calls on the menuchoice and denying the user the chance to enter a name).

Instead we'll call nextLine every time. For our string values we can assign this directly. For our ints we'll use parseInt() to convert first. We must catch a NumberFormatException in the event the user fails to enter an int, and in this case we'll just prompt them again.


Post By: Art Babu on Oct 08, 2018 08:17:46 Monday IST
0
0
 




Note : If You want to create Your own New Topic, You can create here any topic for discuss with free mind. Share Your problems without obstacles for make Your mind light.