0

Ok so I'm not too familiar with the gimmicks of JOptionPane, but basically I'm trying to just call in a dialog box but it keeps on messing up badly when there are constructor statements in-between or before. take a look..

import java.util.Scanner;               //imports Scanner class from util package; takes input from the user
import javax.swing.JOptionPane;         //imports JOptionPane dialog box class package

public class Proj5
{
    public static void main(String[] args)
    {       
        String sName, sInput;
        int iMenuChoice;

        JOptionPane.showMessageDialog(null, "\t\tWelcome to the Advising Manager!" 
                                      + "\n---------- Created by Dr. Bailey and Sanford Gabrielle ----------"
                                      , "Message", 1);

        sName = JOptionPane.showInputDialog(null, "What is the advisor's name?", "Input", 3);

        Advisee a1 = new Advisee();
        Advisee a2 = new Advisee();
        Advisee a3 = new Advisee();

        while(true)
        {
            sInput = JOptionPane.showInputDialog(null, "~~ Please make a selection from the menu below ~~\n\n" 
                                        + "1. Add a new advisee" + "\n2. Update an advisee's information"
                                        + "\n\n3. Display all advisees" + "\n4. Display advisees who are cleared to graduate"
                                        + "\n5. Exit" + "\n\n\nWhat is your selection?", "Input", 3);


            sInput = sInput.trim();
            iMenuChoice = Integer.parseInt(sInput);

            int iAdviseeCounter = 0;

            switch(iMenuChoice) 

When it gets to the menu it messes up and does not even display a dialog box. I have no idea why the default constructor creation statements are interfering. It seems to be failing at the

            Advisee a1 = new Advisee();
            Advisee a2 = new Advisee();
            Advisee a3 = new Advisee();

FYI... there is no error message for my problem. The problem is SIMPLY: THE CONSTRUCTOR STATEMENTS PREVENT THE NEXT DIALOG BOXES FOLLOWING IN THE PROGRAM FROM EVEN APPEARING.

Oh wow. sorry guys heres the Advisee.java

import java.util.Scanner;                   //imports directory needed to take input from the user.
import javax.swing.JOptionPane;             //imports directory needed to use JOptionPane dialog boxes.

public class Advisee
{
    String name;                //declares and sets default to the student's name entered by the advisee.
    String studentId;           //declares and sets default to the student's ID number.
    String concentration;           //declares and sets default to the student's concentration(will be IT,IS, or CS).
    String advisor;         //declares and sets default to the student's advisor's name.
    String studentInfo;             //declares the String that will return a Advisee object's "student information".

    int hoursCompleted = 0;                 //declares and sets default to the number of hours that the student has completed.

    boolean majorSheet;             //declares the student's major sheet.
    boolean intentToGraduate;       //declares the student's intent to graduate.

    public Advisee()                        //creates a default constructor for Advisee's student.
    {       
        setName("XXX XXXXX");
        setStudentId("XXXXXXXXX");
        setConcentration("XX");
        setAdvisor("XXX XXXXX");
        setHoursCompleted(0);
        setMajorSheet(false);
        setIntentToGraduate(false);
    }//end default constructor

    public Advisee(String name,String studentId,String concentration,String advisor,int hoursCompleted,boolean majorSheet,boolean intentToGraduate)
    {
        this.name = name;                               //sets the objects attributes equal to the attributes of the Advisee classes attributes.
        this.studentId = studentId;                     //sets the objects attributes equal to the attributes of the Advisee classes attributes.
        this.concentration = concentration;             //sets the objects attributes equal to the attributes of the Advisee classes attributes.
        this.advisor = advisor;                         //sets the objects attributes equal to the attributes of the Advisee classes attributes.
        this.hoursCompleted = hoursCompleted;           //sets the objects attributes equal to the attributes of the Advisee classes attributes.
        this.majorSheet = majorSheet;                   //sets the objects attributes equal to the attributes of the Advisee classes attributes.
        this.intentToGraduate = intentToGraduate;       //sets the objects attributes equal to the attributes of the Advisee classes attributes.
    }//end Advisee constructor including all attributes of a the advisee class.

    public Advisee(Advisee advisee)
    {
        this.name = advisee.name;                               //copies the attributes of an object passed in as a parameter.
        this.studentId = advisee.studentId;                     //copies the attributes of an object passed in as a parameter.
        this.concentration = advisee.concentration;             //copies the attributes of an object passed in as a parameter.
        this.advisor = advisee.advisor;                         //copies the attributes of an object passed in as a parameter.
        this.hoursCompleted = advisee.hoursCompleted;           //copies the attributes of an object passed in as a parameter.
        this.majorSheet = advisee.majorSheet;                   //copies the attributes of an object passed in as a parameter.
        this.intentToGraduate = advisee.intentToGraduate;       //copies the attributes of an object passed in as a parameter.
    }//end copy constructor

    public void setName(String name)
    {
        this.name = name;
    }//end method setName()

    public void setStudentId(String studentId)
    {
        this.studentId = studentId;
    }//end method setStudentId()

    public void setConcentration(String concentration)
    {
        concentration = concentration.trim();           //trims the empty spaces of the string passed into setConcentration().
        concentration = concentration.toUpperCase();    //puts the string variable passed into setConcentration() to all upper case. 

        //validation check.
        while(!(concentration == "IT" || concentration == "IS" || concentration == "CS"))
        {
            this.concentration = "XX";
        }//end while

        if(concentration == "IT" || concentration == "IS" || concentration == "CS")
        {
            if(concentration == "IT")
            {
                this.concentration = "IT";      //sets the concentration of the object that this method is called on to IT.
            }//end if

            else if(concentration == "IS")
            {
                this.concentration = "IS";      //sets the concentration of the object that this method is called on to IS.
            }//end else if

            else
            {
                this.concentration = "CS";      //sets the concentration of the object that this method is called on to CS.
            }//end if else if
        }//end if
    }//end method setConcentration()

    public void setAdvisor(String advisor)
    {
        this.advisor = advisor;
    }//end method setAdvisor

    public void setHoursCompleted(int hoursComleted)
    {
        this.hoursCompleted = hoursCompleted;
    }//end method hoursCompleted()

    public void setMajorSheet(boolean majorSheet)
    {
        this.majorSheet = majorSheet;
    }//end method setMajorSheet

    public void setIntentToGraduate(boolean intentToGraduate)
    {
        this.intentToGraduate = intentToGraduate;
    }//end method setIntentToGraduate()

    public String getName()
    {
        return name;
    }//end method getName()

    public String getStudentId()
    {
        return studentId;
    }//end getStudentId()

    public String getConcentration()
    {
        return concentration;
    }//end getConcentration()

    public String getAdvisor()
    {
        return advisor;
    }//end method getAdvisor()

    public int getHoursCompleted()
    {
        return hoursCompleted;
    }//end method getHoursCompleted()

    public boolean getMajorSheet()
    {
        return majorSheet;
    }//end method getMajorSheet()

    public boolean getIntentToGraduate()
    {
        return intentToGraduate;
    }//end method getIntentToGraduate()

    public String classification()
    {
        String studentClassification;

        if(hoursCompleted < 30)
        {
            studentClassification = "Freshman";
        }//end if

        else if(hoursCompleted >= 30 && hoursCompleted < 60)
        {
            studentClassification = "Sophomore";
        }//end else if

        else if(hoursCompleted >= 60 && hoursCompleted < 90)
        {
            studentClassification = "Junior";
        }//end else if

        else
        {
            studentClassification = "Senior";
        }//end if else if

        //return the String variable that holds the string of the students classification.
        return studentClassification;
    }//end method classification()

    public boolean metGraduationRequirements()
    {
        if(hoursCompleted >= 120 && majorSheet == true && intentToGraduate == true)
        {
            return true;
        }//end if

        else
        {
            return false;
        }//end else
    }//end method metGraduationRequirements()

    public boolean equals(Advisee student1, Advisee student2)
    {
        if(student1.getName() == student2.getName())
        {
            return true;
        }//end if

        else
        {
            return false;
        }//end else
    }//end method equals()

    public boolean equivalent(Advisee student)
    {
        if(this.concentration == student.concentration &&
           this.advisor == student.advisor &&
           this.hoursCompleted == student.hoursCompleted &&
           this.majorSheet == student.majorSheet &&
           this.intentToGraduate == student.intentToGraduate)
        {
            return true;
        }//end if

        else
        {
            return false;
        }//end else
    }//end method equivalent()

    public String toString()
    {
        String strInfo;

        String sClassification = classification();
        String sCleared = clearedToGraduateMsg();

        strInfo = ("Id:" + studentId

                + "Advisor: " + advisor

                + "Concentration: " + concentration
                + "Completed Hours: " + hoursCompleted
                + "Classification: " + sClassification
                + "Cleared for graduation: " + sCleared );

        return strInfo;
    }//end method toString()

    public String clearedToGraduateMsg()
    {
        String strDisplay;

        if(hoursCompleted >= 120 && majorSheet == true && intentToGraduate == true)
        {
            strDisplay = "Yes, all requirements have been met.";
        }//end if

        else
        {
            if(hoursCompleted >= 120)
            {
                strDisplay = "Yes, hours requirements have been met.";
            }

            else
            {
                strDisplay = "Not completed hours.";
            }

            if(majorSheet == true)
            {
                strDisplay = "Has filled out major sheet.";
            }

            else
            {
                strDisplay = "Has not filed a major sheet.";
            }

            if(intentToGraduate == true)
            {
                strDisplay = "Yes, has filed an intent to graduate.";
            }

            else
            {
                strDisplay = "Has not filed an intent to graduate.";
            }
        }//end else

        return strDisplay;
    }//end method clearedToGraduateMsg()

    public void assign(String name,String studentId,String concentration,String advisor,int hoursCompleted,boolean majorSheet,boolean intentToGraduate)
    {
        name = this.name;
        studentId = this.studentId;
        concentration = this.concentration;
        advisor = this.advisor;
        hoursCompleted = this.hoursCompleted;
        majorSheet = this.majorSheet;
        intentToGraduate = this.intentToGraduate;
    }//end method assign()
}//end Advisee class

Here's the FULL driver class "Proj5.java"

import java.util.Scanner;               //imports Scanner class from util package; takes input from the user
import javax.swing.JOptionPane;         //imports JOptionPane dialog box class package

public class Proj5
{
    public static void main(String[] args)
    {       
        String sName, sInput;
        int iMenuChoice;

        JOptionPane.showMessageDialog(null, "\t\tWelcome to the Advising Manager!" 
                                      + "\n---------- Created by Dr. Bailey and Sanford Gabrielle ----------"
                                      , "Message", 1);

        sName = JOptionPane.showInputDialog(null, "What is the advisor's name?", "Input", 3);

        while(true)
        {   
            sInput = JOptionPane.showInputDialog(null, "~~ Please make a selection from the menu below ~~\n\n" 
                                        + "1. Add a new advisee" + "\n2. Update an advisee's information"
                                        + "\n\n3. Display all advisees" + "\n4. Display advisees who are cleared to graduate"
                                        + "\n5. Exit" + "\n\n\nWhat is your selection?", "Input", 3);

            sInput = sInput.trim();
            iMenuChoice = Integer.parseInt(sInput);

            int iAdviseeCounter = 0;

            switch(iMenuChoice)
            {
                //case 1:  "Add a new advisee"
                case 1:
                    if(iAdviseeCounter == 0)
                    {
                        String s1,s2,s3,s4,s5;
                        int iVal;

                        s1 = JOptionPane.showInputDialog(null, "What is the advisee's name?", "Input", 3);
                        s1 = s1.trim();

                        s2 = JOptionPane.showInputDialog(null, "What is the advisee's student Id?", "Input", 3);
                        s2 = s2.trim();

                        s3 = JOptionPane.showInputDialog(null, "What is the student's concentration?", "Input", 3);
                        s3 = s3.trim();

                        s4 = JOptionPane.showInputDialog(null, "Who is the student's advisor", "Input", 3);
                        s4 = s4.trim();

                        s5 = JOptionPane.showInputDialog(null, "How many hours have they completed?", "Input", 3);
                        s5 = s5.trim();
                        iVal = Integer.parseInt(s5);

                        a1 = new Advisee();
                        a1.assign(s1,s2,s3,s4,iVal,false,false);

                        iAdviseeCounter++;
                    }//end if

                    else if(iAdviseeCounter == 1)
                    {       
                        String st1,st2,st3,st4,st5;
                        int iVal2;

                        st1 = JOptionPane.showInputDialog(null, "What is the advisee's name?", "Input", 3);
                        st1 = st1.trim();

                        st2 = JOptionPane.showInputDialog(null, "What is the advisee's student Id?", "Input", 3);
                        st2 = st2.trim();

                        st3 = JOptionPane.showInputDialog(null, "What is the student's concentration?", "Input", 3);
                        st3 = st3.trim();

                        st4 = JOptionPane.showInputDialog(null, "Who is the student's advisor", "Input", 3);
                        st4 = st4.trim();

                        st5 = JOptionPane.showInputDialog(null, "How many hours have they completed?", "Input", 3);
                        st5 = st5.trim();
                        iVal2 = Integer.parseInt(st5);

                        a2 = new Advisee();
                        a2.assign(st1,st2,st3,st4,iVal2,false,false);

                        iAdviseeCounter++;
                    }//end else if

                    else if(iAdviseeCounter == 2)
                    {
                        String str1,str2,str3,str4,str5;
                        int iVal3;

                        str1 = JOptionPane.showInputDialog(null, "What is the advisee's name?", "Input", 3);
                        str1 = str1.trim();

                        str2 = JOptionPane.showInputDialog(null, "What is the advisee's student Id?", "Input", 3);
                        str2 = str2.trim();

                        str3 = JOptionPane.showInputDialog(null, "What is the student's concentration?", "Input", 3);
                        str3 = str3.trim();

                        str4 = JOptionPane.showInputDialog(null, "Who is the student's advisor", "Input", 3);
                        str4 = str4.trim();

                        str5 = JOptionPane.showInputDialog(null, "How many hours have they completed?", "Input", 3);
                        str5 = str5.trim();
                        iVal3 = Integer.parseInt(str5);

                        a3 = new Advisee();
                        a3.assign(str1,str2,str3,str4,iVal3,false,false);

                        iAdviseeCounter++;
                    }//end else if

                    else
                    {
                        JOptionPane.showMessageDialog(null, "You have reached your maximum number of advisees!", "Message", 1);
                    }//end else
                    break;      //break from case 1.
                //case 2:  "Update and advisee's information"   
                case 2:
                    String sName1 = a1.getName();
                    String sName2 = a2.getName();
                    String sName3 = a3.getName();

                    if(iAdviseeCounter == 0)
                    {
                        JOptionPane.showMessageDialog(null, "There are no advisees in the system yet", "Message", 1);   
                    }

                    else
                    {
                        switch(iAdviseeCounter)
                        {
                            case 1:
                                JOptionPane.showInputDialog(null, "~~ Please select which advisee's information you need to update ~~\n\n" 
                                + "\t1. " + sName1 + "\n\n\nWhat is your selection?" , "Input", 3);
                                break;
                            case 2:
                                JOptionPane.showInputDialog(null, "~~ Please select which advisee's information you need to update ~~\n\n" 
                                + "\t1. " + sName1 + "\n\t2. " + sName2 + "\n\n\nWhat is your selection?" , "Input", 3);
                                break;
                            case 3:
                                JOptionPane.showInputDialog(null, "~~ Please select which advisee's information you need to update ~~\n\n" 
                                + "\t1. " + sName1 + "\n\t2. " + sName2 + "\n\t3. " + sName3 + "\n\n\nWhat is your selection?", "Input", 3);
                                break;
                        }
                    }
                    break;
                //case 3:  "Display all advisees"
                case 3:
                    String sTemp = a1.toString();
                    String sTemp1 = a2.toString();
                    String sTemp2 = a3.toString();

                    if(iAdviseeCounter == 0)
                    {
                        JOptionPane.showMessageDialog(null, "There are no advisees in the system yet", "Message", 1);   
                    }//end if

                    else if(iAdviseeCounter == 1)
                    {
                        JOptionPane.showMessageDialog(null, "Advisee Information" + "\n-------------------------------", sTemp, 1);
                    }//end else if

                    else if(iAdviseeCounter == 2)
                    {
                        JOptionPane.showMessageDialog(null, "Advisee Information" + "\n-------------------------------", sTemp + "\n\n" + sTemp1, 1);
                    }//end else if

                    else
                    {
                        JOptionPane.showMessageDialog(null, "Advisee Information" + "\n-------------------------------", sTemp + "\n\n" + sTemp1 + "\n\n" + sTemp2, 1);
                    }//end if else if
                    break;
                //case 4:  "Display advisees who are cleared to graduate"   
                case 4:
                    if(iAdviseeCounter == 0)
                    {
                        JOptionPane.showMessageDialog(null, "There are no advisees in the system yet", "Message", 1);   
                    }//end if

                    else
                    {

                    }//end else
                    break;
                //case 5:  "Exit"
                case 5:
                    JOptionPane.showMessageDialog(null, "Goodbye!", "System closing", 1);
                    System.exit(0);
                    break;
            }//end switch(iMenuChoice)

        }//end while loop
    }//end method main

}//end class Proj5
  • 1
    Could you add a link to the code for Advisee, or post it here? There might be something in those constructors that are not allowing the code to go to the while true loop. Also, could you add a println inside the while true loop above the sInput box, and see if anything is printed? – Vineet Kosaraju Dec 04 '13 at 03:33
  • Exactly -- you're showing us all the code, except for the important code, that which is messing up. Why? But don't "link" to Advisee. Post it all here. – Hovercraft Full Of Eels Dec 04 '13 at 03:35
  • @Bucco I will test the println right now. –  Dec 04 '13 at 03:41
  • @Bucco I just tryed adding the println where you said. It printed nothing.. –  Dec 04 '13 at 03:45
  • Not related to the main error, but you should use .equals for comparing Strings. See http://stackoverflow.com/questions/767372/java-string-equals-versus – Vineet Kosaraju Dec 04 '13 at 03:45
  • @Bucco Wait what? I have an equals() and an equivalent() method in the Advisee.java file. I just need to know whats wrong with my placement and fashion of the default constructor statements on lines 17,18, and 19. –  Dec 04 '13 at 05:55
  • I'm not sure yet what is wrong with the code and why the constructors are not moving on, but I found another error in the setConcentration method (and I suspect elsewhere) you are using "==" to compare strings instead of the String .equals method. - You do concentration == "IT" for example – Vineet Kosaraju Dec 04 '13 at 05:56

2 Answers2

0

I have figured out why the new dialog boxes are not being called. The reason is that in the constructor for Advisee you are hitting an endless while loop. After looking through the methods being called in Advisee, I realized that it is because of the setConcentration method, for the following three reasons.

1 )

while(!(concentration == "IT" || concentration == "IS" || concentration == "CS"))
{
    this.concentration = "XX";
}//end while

This is setting the value of "this.concentration" the instance variable, not the local method. However in the while loop check, you are only comparing the local method variable. This can be fixed by comparing only the local method variable until the end, when you set the instance variable to the local method variable.

2 )

if(concentration == "IT" || concentration == "IS" || concentration == "CS")

Here you are using double equals to compare strings. This is not how you compare strings in Java, and all of those checks will return false, and so will the while loop checks. In Java you can compare two strings using

str1.equals(str2)

See this post Java String.equals versus ==

3 )

while(!(concentration == "IT" || concentration == "IS" || concentration == "CS"))
{
    this.concentration = "XX";
}//end while

Here, even once the other two issues are fixed, you are stuck in this loop because you are continuing in the loop if concentration is not IT, IS, or CS. XX is not one of those, and so the loop will continue forever. Instead, this should be an if loop.

The final setConcentration method that will fix the infinite for loop is below. The code now works fully on my computer.

public void setConcentration(String concentration)
{
    concentration = concentration.trim();           //trims the empty spaces of the string passed into setConcentration().
    concentration = concentration.toUpperCase();    //puts the string variable passed into setConcentration() to all upper case. 

    //validation check.
    if(!(concentration.equals("IT") || concentration.equals("IS") || concentration.equals("CS")))
    {
        concentration = "XX";
    }//end validation

    if(concentration.equals("IT") || concentration.equals("IS") || concentration.equals("CS"))
    {
        if(concentration.equals("IT"))
        {
            concentration = "IT";      //sets the concentration of the object that this method is called on to IT.
        }//end if

        else if(concentration.equals("IS"))
        {
            concentration = "IS";      //sets the concentration of the object that this method is called on to IS.
        }//end else if

        else
        {
            concentration = "CS";      //sets the concentration of the object that this method is called on to CS.
        }//end if else if
    }//end if

    this.concentration = concentration;
}//end method setConcentration()
Community
  • 1
  • 1
Vineet Kosaraju
  • 5,572
  • 2
  • 19
  • 21
0

If you make the changes he mentioned, it will run, but it won't store any information. I hit 1 to add an advisee and then I hit 4 to see all advisees but it says none have been entered... (Should have been a comment but I can't comment)

Jon5001
  • 27
  • 7