While loop problem [I think]

While loop keeps repeating when it shouldn't

Page 1 of 1

5 Replies - 3651 Views - Last Post: 17 July 2009 - 09:34 AM Rate Topic: -----

#1 Fujo  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 51
  • Joined: 10-July 09

While loop problem [I think]

Posted 16 July 2009 - 02:32 PM

Hey guys,

This is my first post to the site. I'm just learning java as a hobby and decided to write a program to create a music collection catalogue. I'm only in the early stages and I've encountered a problem. I started off by trying to create the menu for my program. My goal was to display a main menu with 3 options, which have sub-menus.The first option is to edit the catalogue and the second to view/search it with the third option being to exit the programme. Here is the code I've written so far:

	/** A program to catalogue a music collection so that it can be viewed and
	 * 	searched for artists, albums or tracks. Entries can be created, edited
	 *	or deleted.
	 *	Author: Fujo
	 *	Project begun: 12th July 2009
	 */

import java.util.Scanner;
import static java.lang.System.out;
import static java.lang.System.in;

class MusicCollection
{
	public static void main(String args[])
	{
		Scanner myScanner = new Scanner(in);
		int mainMenuChoice = 0;
		
		out.print("\n\nWelcome to my Music Collection Cataloguing Program");
		
		while(mainMenuChoice != 3) // Keeps repeating until user chooses to exit
		{
			out.print("\n\n\tMAIN MENU");	// Displays the main menu
			out.print("\n\n\t1. Edit Collection");
			out.print("\n\t2. View Collection");
			out.print("\n\t3. Exit Program");
			out.print("\n\nPlease choose an option from the Main Menu:\t");
			mainMenuChoice = myScanner.nextInt(); // Gets the users choice
			
			if(mainMenuChoice == 1) // Edit Collection Menu
			{
				int editMenuChoice = 0;
				
				while(editMenuChoice != 4)
				{
					out.print("\n\n\tEDIT COLLECTION MENU");	// Displays the edit collection menu
					out.print("\n\n\t1. Create new entry");
					out.print("\n\t2. Edit existing entry");
					out.print("\n\t3. Delete existing entry");
					out.print("\n\t4. Return to Previous Menu");
					out.print("\n\nPlease choose an option from the Edit Collection Menu:\t");
					editMenuChoice = myScanner.nextInt(); // Gets the users choice
					
					if(editMenuChoice == 1)
					{
						out.print("\n\n\tCREATE NEW ENTRY MENU");
					}
					if(editMenuChoice == 2)
					{
						out.print("\n\n\tEDIT EXISTING ENTRY MENU");
					}
					if(editMenuChoice == 3)
					{
						out.print("\n\n\tDELETE EXISTING ENTRY MENU");
					}
					if(editMenuChoice == 4)
					{
						out.print("\nYou choose: " + editMenuChoice);
					}
					else
					{
						out.print("\n\nInvalid Choice. Please try again.");
					}
				}
			}
			
			if(mainMenuChoice == 2) // View Collection Menu
			{
				int viewMenuChoice = 0;
				
				out.print("\n\n\tVIEW COLLECTION MENU");
			}
			
			if(mainMenuChoice == 3) // Exit Program Menu
			{
				int exitMenuChoice = 0;
				
		/*		while(exitMenuChoice != 1 || exitMenuChoice != 2)
				{
					out.print("\n\t1. Yes, please exit");
					out.print("\n\t2. No, I made a mistake");
					out.print("\n\nAre you sure you wish to exit?\t");
					exitMenuChoice = myScanner.nextInt();
					
					if(exitMenuChoice == 1)
					{
						mainMenuChoice = 3;
					}
					if(exitMenuChoice == 2)
					{
						mainMenuChoice = 0;
					}
					else
					{
						out.print("\nInvalid choice. Please try again.");
					}
				}
		*/
				
//		This code doesn't work properly - the while loop keeps repeating even if 
//		1 or 2 is chosen. Scanner does change the variable exitMenuChoice..
			
			out.print("\n\nSorry to see you go! Come back soon!\n\n");
			}
			
			else
			{
				out.print("\n\nInvaid Choice. Please try again.");
			}
			
		}
	}
}



Basically the problem I've encountered is with exiting the menu. If I press 3 to exit I get my submenu which is a while loop as can be seen in the code. The while loop keeps repeating even though the scanner does change the variable value. I may be missing something basic or doing something wrong and if I am I apologize for wasting your time. Any suggestions or insights you can give me would be greatly appreciated.

Thanks in advance for your help!

This post has been edited by Fujo: 16 July 2009 - 02:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: While loop problem [I think]

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: While loop problem [I think]

Posted 16 July 2009 - 03:30 PM

i tried it and it works now
	 
import java.util.Scanner;
import static java.lang.System.out;
import static java.lang.System.in;

class MusicCollection
{
	public static void main(String args[])
	{
		Scanner myScanner = new Scanner(in);
		int mainMenuChoice = 0;
	   
		out.print("\n\nWelcome to my Music Collection Cataloguing Program");
	   
		while(mainMenuChoice != 3) // Keeps repeating until user chooses to exit
		{
			out.print("\n\n\tMAIN MENU");	// Displays the main menu
			out.print("\n\n\t1. Edit Collection");
			out.print("\n\t2. View Collection");
			out.print("\n\t3. Exit Program");
			out.print("\n\nPlease choose an option from the Main Menu:\t");
			mainMenuChoice = myScanner.nextInt(); // Gets the users choice
		   
			if(mainMenuChoice == 1) // Edit Collection Menu
			{
				int editMenuChoice = 0;
			   
				while(editMenuChoice != 4)
				{
					out.print("\n\n\tEDIT COLLECTION MENU");	// Displays the edit collection menu
					out.print("\n\n\t1. Create new entry");
					out.print("\n\t2. Edit existing entry");
					out.print("\n\t3. Delete existing entry");
					out.print("\n\t4. Return to Previous Menu");
					out.print("\n\nPlease choose an option from the Edit Collection Menu:\t");
					editMenuChoice = myScanner.nextInt(); // Gets the users choice
				   
					if(editMenuChoice == 1)
					{
						out.print("\n\n\tCREATE NEW ENTRY MENU");
					}
					if(editMenuChoice == 2)
					{
						out.print("\n\n\tEDIT EXISTING ENTRY MENU");
					}
					if(editMenuChoice == 3)
					{
						out.print("\n\n\tDELETE EXISTING ENTRY MENU");
					}
					if(editMenuChoice == 4)
					{
						out.print("\nYou choose: " + editMenuChoice);
					}
					else
					{
						out.print("\n\nInvalid Choice. Please try again.");
					}
				}
			}
		   
			if(mainMenuChoice == 2) // View Collection Menu
			{
				int viewMenuChoice = 0;
			   
				out.print("\n\n\tVIEW COLLECTION MENU");
			}
		   
			if(mainMenuChoice == 3) // Exit Program Menu
			{
				int exitMenuChoice = 0;
			   
			   do
				{
					out.print("\n\t1. Yes, please exit");
					out.print("\n\t2. No, I made a mistake");
					out.print("\n\nAre you sure you wish to exit?\t");
					exitMenuChoice = myScanner.nextInt();
				   
					if(exitMenuChoice == 1)
					{
						mainMenuChoice = 3;
					}
					if(exitMenuChoice == 2)
					{
						mainMenuChoice = 0;
					}
					else
					{
						out.print("\nInvalid choice. Please try again.");
					}
				} while(exitMenuChoice != 1);
		
			   
//		This code doesn't work properly - the while loop keeps repeating even if
//		1 or 2 is chosen. Scanner does change the variable exitMenuChoice..
		   
			out.print("\n\nSorry to see you go! Come back soon!\n\n");
			}
		   
			else
			{
				out.print("\n\nInvaid Choice. Please try again.");
			}
		   
		}
	}
}




EDIT: edited code for correction

This post has been edited by mostyfriedman: 16 July 2009 - 03:38 PM

Was This Post Helpful? 1
  • +
  • -

#3 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: While loop problem [I think]

Posted 16 July 2009 - 03:55 PM

Also you import java.lang. something. Not completely sure but last I checked it will work even if you dont import it. I believe I was told that anything from the lang category is automatically accepted and used. If we had to import every single class for every method we use, that would be a lot of imports o_o So they made it ismple anything in the java.lang is auto accepted no need for imports. Not entirely sure but yeah. If mostyfriedman could verify then yeah.

Goodluck!
Was This Post Helpful? 1
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: While loop problem [I think]

Posted 16 July 2009 - 04:08 PM

View PostFuzzyness, on 16 Jul, 2009 - 02:55 PM, said:

Also you import java.lang. something. Not completely sure but last I checked it will work even if you dont import it. I believe I was told that anything from the lang category is automatically accepted and used. If we had to import every single class for every method we use, that would be a lot of imports o_o So they made it ismple anything in the java.lang is auto accepted no need for imports. Not entirely sure but yeah. If mostyfriedman could verify then yeah.

Goodluck!

you are correct...the lang is automatically imported so you dont have to import it
Was This Post Helpful? 0
  • +
  • -

#5 Fujo  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 51
  • Joined: 10-July 09

Re: While loop problem [I think]

Posted 17 July 2009 - 09:28 AM

Thanks mostyfriedman :^: . I will try that code when I get home later. I'm not familiar with the do command but i will look it up! Have you any idea why the while loop doesn't work?

Fuzzyness
import static java.lang.System.out
saves me from writing System.out - i can just write out i.e
out.print
instead of
System.out.print


I'm not sure exactly why this works as I am only learning java [from a java for dummies book!] but I know that it does work.

Thanks again for your help guys!
Was This Post Helpful? 0
  • +
  • -

#6 ts230  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 225
  • Joined: 11-July 09

Re: While loop problem [I think]

Posted 17 July 2009 - 09:34 AM

View PostFujo, on 17 Jul, 2009 - 08:28 AM, said:

Thanks mostyfriedman :^: . I will try that code when I get home later. I'm not familiar with the do command but i will look it up! Have you any idea why the while loop doesn't work?

Fuzzyness
import static java.lang.System.out
saves me from writing System.out - i can just write out i.e
out.print
instead of
System.out.print


I'm not sure exactly why this works as I am only learning java [from a java for dummies book!] but I know that it does work.

Thanks again for your help guys!

I think you have to say
break;
for option 3 to exit the while loop and then do cleanup after the while loop and exit.

Hope this helps!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1