-2

I'm having trouble getting a for loop in my program to wrok. I'm not sure what I'm doing wrong. I've been working on it for about 3 hours and still have no clue what to do.

I fixed the while statement. That is not the problem. The problem is with the for loop towards the bottom of the code.

Here is my code:

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

        switch(strSelection)
        {
            case "1":

                String strString;
                String strHours;
                int iHours;
                String strMajor;
                boolean blnMajor;
                String strIntent;
                boolean blnIntent;

                adv[iCount]=new Advisee();
                adv[iCount].setName(JOptionPane.showInputDialog(null,"What is the advisee's name?","Name",3));
                adv[iCount].setID(JOptionPane.showInputDialog(null,"What is the advisee's ID?","ID",3));
                adv[iCount].setConcentration(JOptionPane.showInputDialog(null,"What is the advisee's concentration?","Concentration",3));
                adv[iCount].setAdvisor(strAdvisor);
                strHours=JOptionPane.showInputDialog(null,"What are the advisee's completed hours?","Completed Hours",3);
                iHours=Integer.parseInt(strHours);
                adv[iCount].setHoursCompleted(iHours);
                strMajor=JOptionPane.showInputDialog(null,"Does the advisee have the major sheet?","Major Sheet",3);
                blnMajor=Boolean.parseBoolean(strMajor);
                adv[iCount].setMajorSheet(blnMajor);
                strIntent=JOptionPane.showInputDialog(null,"Does the advisee have the intent to graduate form filed?","Intent To Graduate",3);
                blnIntent=Boolean.parseBoolean(strIntent);
                adv[iCount].setIntentFiled(blnIntent);
                iCount++;
                iAccum++;
                break;

            case "2":
                String strUpdateSelection;
                String strOutput="";
                String strAccumulator="";
                System.out.print(iAccum);
                for(iCount=0;iCount==iAccum;iCount++)
                {
                    strOutput=adv[iCount].getName();
                    strAccumulator+=strOutput;
                }

                strUpdateSelection=JOptionPane.showInputDialog(
                                                                null,"~~Please select which advisee's information you need to update~~"+
                                                                "\n"+strOutput+
                                                                "\nWhat is your selection?","Information Update",3);

                break;
            case "5":
                System.exit(0);
                break;
        }

    }                                           
}
}

3 Answers3

0

Change from

while(strSelection!="5")

To

while(!"5".equals(strSelection))
Masudul
  • 21,823
  • 5
  • 43
  • 58
0
while(!strSelection.equals("5"))
neutrino
  • 2,297
  • 4
  • 20
  • 28
0

You are comparing not-equality of a String. The way you should do is following

 while(!"5".equals(strSelection))

Or

 while(!strSelection.equals("5"))

Not

 while(strSelection!="5") 

And change your for loop

 for(iCount=0;iCount<=iAccum;iCount++)

instead of

 for(iCount=0;iCount==iAccum;iCount++)
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115