0

I have a menu so the user can choose from its choices, but when I enter the number of my choice I can't see the result and again the menu will appear instead ! Can someone tell me where is the problem pls? thanks in advance

import javax.swing.JOptionPane;

public class StudentsMarkComputerProject 
{

    public static void main(String args[]) 
    {
        JOptionPane.showMessageDialog(null, "Welcome");
        JOptionPane.showMessageDialog(null, "Students mark Input Project");
        JOptionPane.showMessageDialog(null, "In this program the user is going to enter the students name, class and marks.Then the program is going to output several information about each student.");
        JOptionPane.showMessageDialog(null, "Class Information must be entered as the name of the class, the subject and the number of students");
        String nameofclass = JOptionPane.showInputDialog("Enter the name of the class");
        String nameofsubject = JOptionPane.showInputDialog("Enter the subject"); 
        int numberofstudents =Integer.parseInt(JOptionPane.showInputDialog("Enter the number of students"));

        JOptionPane.showMessageDialog(null,"Class Students Information");
        JOptionPane.showMessageDialog(null,"You must enter the name, surname and " + nameofsubject + " marks for each and every student in class "+ nameofclass);
        StLuciaClassrooms theclass =new StLuciaClassrooms(nameofclass, numberofstudents);
        theclass.enterInformation(numberofstudents);


        int option = 0;
        do
         {

        String ques = "<html><body><b>-----------------Menu Options--------------</b><br/>"
            + "Here you need to enter a number between 1 and 6 according your choice.<br/>"
            + "----------------------------------------------------------------------------<br/>"
            + "1.Search for a particular student.<br/>"
            + "2.Show students names, marks and grades.<br/>"
            + "3.Display passes.<br/>"
            + "4.Display failures.<br/>"
            + "5.Show statistics.<br/>"
            + "6.Exit.<br/>"
            + "Enter you choice<br/></body></html>";
     int opt = Integer.parseInt(JOptionPane.showInputDialog(ques));
     System.out.println(opt);
            switch (option)
            {
                case 1:
                String name = JOptionPane.showInputDialog("Enter the name of the student you want to search for");
                String surname = JOptionPane.showInputDialog("Enter the surname of the class");
                theclass.searchStudents(numberofstudents,name,surname);
                break;
                case 2://show students names, marks and grades
                theclass.showInformation(numberofstudents);
                break;
                case 3://display passes
                theclass.displayPasses(numberofstudents); 
                break;
                case 4://display failures
                theclass.displayFailures(numberofstudents);
                break;
                case 5 :
                theclass.showStatistics(numberofstudents);
                break ;
                case 6 ://exit
                System.out.println("Thanks for using this program , Hope to see you again ! . Have a nice day ");
                break;
                default :
                System.out.println("Error ! ");
            }
        }while(option !=6);

      }
  }

.

import javax.swing.JOptionPane;
 class StLuciaClassrooms 
{
    String nameofclass;
    StLuciaStudents[] students;//array declaration is done only with square brackets "[]"

    public StLuciaClassrooms (String nameofclass, int numberofstudents)
    {
        this.nameofclass = nameofclass;
        students = new StLuciaStudents[numberofstudents];
    }
    public void enterInformation (int numberofstudents)
    {

        System.out.println("*********************************Please enter the student's information**********************");
        for (int i=0;i < numberofstudents; i++)
        {
            String names=JOptionPane.showInputDialog("Enter sudents names");
            String surnames=JOptionPane.showInputDialog("Enter students surnames"); 
            int marks= Integer.parseInt(JOptionPane.showInputDialog("Enter the students marks"));
            StLuciaStudents kid = new StLuciaStudents(names, surnames, marks);
            students[i]=kid; 
        }
    }

    private String generateGrade (int marks)
    {
        String grade = "a";
          if (marks < 50)
          {
            grade = ("F");
          }else if (marks < 65)
          {
            grade = ("D");
          }else if (marks<75)
          {
            grade = ("C");
          }else if (marks <85)
          {
            grade = ("B");
          }else if (marks < 90)
          { 
            grade = ("A");
          }else if (marks >90&&marks<100)
          {
            grade = ("A+");
          }else if (marks == 100)
          {
            grade = ("A++");
          }else;
          {
            grade = "This is an invalid mark";
          }
          return grade ;



    }
    public void showInformation (int numberofstudents)
    {
        for (int i =0; i < numberofstudents ; i++)
        {
            System.out.println("Name : " + students[i].nameofstudents);
            System.out.println("Surname:"+students[i].surnameofstudents);
            System.out.println("Mark:"+ students[i].markofstudents);
            System.out.println("Grade : " + generateGrade(students[i].markofstudents));
        }
    }

    public void searchStudents (int numberofstudents, String nameofstudents, String surnameofstudents)
    {
        boolean flag = false;
        for (int i=0; i<numberofstudents;i++)
        {
            if (students[i].nameofstudents.equalsIgnoreCase(nameofstudents) && students[i].surnameofstudents.equalsIgnoreCase(surnameofstudents))
            {
                flag = true ;
                System.out.println(students[i].nameofstudents + students[i].surnameofstudents +" is found ! ");
            }
        }

    }   
        public void displayFailures(int numberofstudents)
    {
        for (int i=0;i < numberofstudents;i++)
        {
            if (students[i].markofstudents < 50)
            {
                System.out.println(students[i].nameofstudents + students[i].surnameofstudents);
            }
        }
    }
            public void displayPasses(int numberofstudents)
        {
        for (int i=0;i < numberofstudents;i++)
          {
            if (students[i].markofstudents >=50)
            {
                System.out.println(students[i].nameofstudents + students[i].surnameofstudents);
            }
          }
        }

           public void showStatistics(int numberofstudents)
           {

           int total = 0;
           int maxmark = students[0].markofstudents, first_stud=0;
           int minmark = students[0].markofstudents, last_stud=0;
           String maxname = " ",maxsurname = " ";
           String minname = " ",minsurname = " ";

        for (int i =1; i<numberofstudents; i++)
        {
            if (students[i].markofstudents > maxmark )//continue this as staistics are show in a particular case 
            {
                maxmark = students[i].markofstudents;
                maxname = students[i].nameofstudents;
                maxsurname = students[i].surnameofstudents;
            }
        }
        for (int i=1;i>numberofstudents;i++)
        {
            if (students[i].markofstudents < minmark)
            {
                minmark = students[0].markofstudents;
                minname = students[i].nameofstudents;
                minsurname=students[i].surnameofstudents;
            }
        }

        double average = total/numberofstudents;
        System.out.println("The maximum mark of" + maxname + maxsurname + " is " + maxmark);
        System.out.println("The minimum mark of" + minname + minsurname + " is " + minmark);
        System.out.println("The average is " + average);
           }

}
1
  • Show the code of StLuciaClassrooms Commented Nov 16, 2013 at 15:23

1 Answer 1

1

note: int opt = Integer.parseInt(JOptionPane.showInputDialog(ques));
so your option stored in opt. but you use option in switch case. so change like this

switch(opt){

}

Optional

  while(opt > 6);

so you get only one time appear menu and do your action. if you want only one time appear the menu remove the do while loop.

Sign up to request clarification or add additional context in comments.

3 Comments

no i need when i enter my choice , for example i need to see the failures , it will appear the failures not the menu again without any failures shown ..
If you do while(opt > 6); it will cause your code to freeze... the ; on the end of the while loop indicates that there is no block to loop over.
and to put the while loop , what loop do i need to remove pls?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.