12 Replies - 377 Views - Last Post: 19 January 2013 - 08:30 PM Rate Topic: -----

#1 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 31-October 12

menu validation problem for some reason i am stuck in a infinite loop

Posted 16 January 2013 - 10:58 AM

the problem is i am stuck in the loop forever even when the menuChouse is less than 4 and i have no clue on whats went wrong.

// declare main menu variables
  		Scanner in;
  		boolean gotInput;
  		int menuChouse;
  		String inputString = "";
  		
  		// loop valid unitll menuChouse is <= 4
  		do
  		{
  			// display the menu
  			System.out.println();
  			System.out.println("*************************************************");
  			System.out.println("*** Main menu");
  			System.out.println("*** ");
  			System.out.println("*** 1. Proccess New Job (using decimal values)");
  			System.out.println("*** 2. Proccess new job (using RGB hexadecimal values)");
  			System.out.println("*** 3. Calibrate Dispenser");
  			System.out.println("*** 4. Dipsplay Summary of Jobs");
  			System.out.println("*** 0. Exit");
  			System.out.print("***\n*** Please choose a option: ");
  			
  			
  			// loop valid until gotInput true
  			do
  			{
  				// set gotInput to true as i assume i am going to get the input
  				gotInput = true;
  				System.out.println(gotInput); //###################
  				try
  				{
  					// set new scanner
  					in = new Scanner(System.in);
  					
  					inputString = in.next();
  				}
  				catch (Exception e)
  				{
  					System.out.println("A unknow error has occured please contact the admin");
  					gotInput = false;
  				}
  				System.out.println(gotInput); //###################
  			} while (!gotInput);
  			
  			// check if input is a digit
  			try
  			{
  				menuChouse = Integer.parseInt(inputString);
  				// is an integer
  				System.out.println(menuChouse);//###################
  			} 
  			catch (NumberFormatException e) 
  			{
  				// not an integer
  				menuChouse = 200000;
  			}
  			System.out.println(menuChouse);//###################
  			
  		} while (menuChouse > 4);
  		
  		return menuChouse;
  	}



any help is appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: menu validation problem for some reason i am stuck in a infinite loop

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 849
  • View blog
  • Posts: 2,585
  • Joined: 29-July 11

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 16 January 2013 - 11:52 AM

I just commented out return menuChouse; and the loop ended.
Was This Post Helpful? 0
  • +
  • -

#3 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 31-October 12

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 16 January 2013 - 11:57 AM

then the complier returns a error as i use menuChouse in another method
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 16 January 2013 - 12:17 PM

It's not obvious when I read over your code why this loop wouldn't exit.

First diagnostic: where exactly is it looping? Is it looping over just that while loop, or is it maybe calling the method this is in repeatedly?
One way to find out is to insert println "breakpoint" statements at key points. Typically, I'll insert them at the top and bottom of the method, and at places where the flow branches -
System.out.println("entering method Foo");

System.out.println("leaving method Foo");


that sort of thing. That way you can get a more precise idea of where the problem is happening.

This post has been edited by jon.kiparsky: 16 January 2013 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 16 January 2013 - 12:19 PM

When you have a phone conversion with one of your friend do you hang up and redial after each sentence ?

So create the Scanner object in assigned to System.in before entering the first do loop and continue to use that Scanner object for the rest of the life of your program
Was This Post Helpful? 0
  • +
  • -

#6 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 31-October 12

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 16 January 2013 - 12:20 PM

its looking with the first do whille loop inside this method, the condition that is not working is while (menuChouse > 4)
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 16 January 2013 - 12:24 PM

Are you sure of that?

while (menuChouse > 4) 


will not break. If that value is greater than 4, the expression will return true and the loop will loop. If not, then the expression will return false and the loop will not loop. That's iron-clad.

So either menuChouse is less than 4 or you're looping some other how. So when you print out menuChouse as the last step in that loop, what do you see?
Was This Post Helpful? 0
  • +
  • -

#8 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 31-October 12

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 16 January 2013 - 02:11 PM

after choosing a option on the menu i get down to the bottom on the program and print out the menu choose and it is as expected, and under no circumstances have i got past the while loop, which i dont understand as it is very basic and i know that menuChouse is INT so i cant think of anything that could be going wrong
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 16 January 2013 - 02:22 PM

So if you insert a println statement after the end of the loop, and before the return statement, it never shows?
Weird.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 16 January 2013 - 02:31 PM

Or it would be weird if it was happening. I just put your method into a stub and ran it, and it exited fine on an input of 3.

Whatever you're seeing, it's not this code that's causing the problem.
Was This Post Helpful? 0
  • +
  • -

#11 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 31-October 12

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 19 January 2013 - 08:15 PM

what do you recommend my next step be
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 19 January 2013 - 08:27 PM

Well, the error you're seeing is not in this code. That's all I can say based on what I've seen so far.
It might be in the code that calls this, but I couldn't say.
Was This Post Helpful? 0
  • +
  • -

#13 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 31-October 12

Re: menu validation problem for some reason i am stuck in a infinite loop

Posted 19 January 2013 - 08:30 PM

thanks for your help i will look into it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1