6 Replies - 1346 Views - Last Post: 01 December 2009 - 04:25 PM Rate Topic: -----

#1 oondi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-November 09

Try/Catch Problem

Posted 01 December 2009 - 09:47 AM

I'm writing this program right now and having an issue where it will keep repeating by itself. It pretty much gets stuck, yet, I can't see why.

		do{		   
			
			
			try {


			System.out.print("Enter height in inches: ");		   
			height = stdIn.nextDouble();		 
			if(height <=0)
				{
				System.out.println("Invalid inches value. Must be positive.");
				badInput = true; 
				}
			badInput = false;
			
			}//end of try
			
			
			catch(RuntimeException ioe)
			{
				System.out.println("Invalid inches value. Mut be a decimal number.");
				badInput = true;
			}//end of catch			
			
			
		}
		while(badInput = true);







Take note that the boolean do loop I use is just a last ditch effort for me to get this to work, it does the same thing with a regular for loop or do while loop. I just can't figure it out. Any speculation why it would do this? Never used try/catch before.


Also, I used an example that someone had posted a few days ago with the badInput = true, and everything like that, I don't need anything complex.

Edit* Also, the error i'm using was just me trying to see what different ones did, just experimenting and trying to learn what it actually does.

This post has been edited by oondi: 01 December 2009 - 09:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Try/Catch Problem

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Try/Catch Problem

Posted 01 December 2009 - 09:52 AM

Remember the difference between the assignment operator and the equality operator.

= and == are not the same.

At the end of the loop you set the boolean value to true everytime, so the loop keeps running.

Change while(badInput = true); to while(badInput == true); and you should be good :)

edit: wooops, I didn't use the == operator :P

This post has been edited by erik.price: 01 December 2009 - 09:59 AM

Was This Post Helpful? 1
  • +
  • -

#3 oondi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-November 09

Re: Try/Catch Problem

Posted 01 December 2009 - 09:54 AM

View Posterik.price, on 1 Dec, 2009 - 08:52 AM, said:

Remember the difference between the assignment operator and the equality operator.

= and == are not the same.

At the end of the loop you set the boolean value to true everytime, so the loop keeps running.

Change while(badInput = true); to while(badInput = true); and you should be good :)



once again, thanks so much, as you were probably typing this post I was think the same thing!

i like java :P
Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Try/Catch Problem

Posted 01 December 2009 - 09:59 AM

No problem. I like Java too. I'm actually posting this from my AP Java class :P

Quote

while(badInput = true); to while(badInput = true);

Haha, woops. I mean while(badInput == true);

This post has been edited by erik.price: 01 December 2009 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#5 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: Try/Catch Problem

Posted 01 December 2009 - 10:38 AM

It's also terribly redundant to write "while(badInput == true)" when "while(badInput)" means exactly the same thing! :)

This post has been edited by LaFayette: 01 December 2009 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

#6 oondi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-November 09

Re: Try/Catch Problem

Posted 01 December 2009 - 11:00 AM

Alright, I have one last question. Now, this seem easy in concept, but in practice it seems to be difficult. I have to use this same program but modify it so it inputs two numbers. Easy right? Well, it has to be feet then inches so an example would be.

Enter height using feet space inches (e.g., 5 6.25): 4 2.3

Now, from my understanding I have to do something similar to this?

		
			double heightF = 0;	  //declaring the height variable
		double heightI = 0;	  //declaring the height inches variable
		double weight = 0;	  //declaring the wieght variable
		double BMI = 0;		 //declaring the BMI variable  
		String fStr, iStr;		 //point in string form
   


		
	while (heightF <= 0) {
		System.out.print("Enter height in inches: ");
		try {
				fStr = stdIn.next();

   
				heightF = Integer.parseInt(fStr);
				
				if(heightF <=0)
				{
				System.out.println("Invalid feet value. Must be positive.");
				}//end of if statment


		}//end of try statement


		catch (Exception e) {
			System.out.println("Invalid feet value. Must be a positive integer.");
			stdIn.next();
		}//end of catch statement
		try{
			
				iStr = stdIn.next();		
		
				heightI = Integer.parseInt(iStr);
				
				if(heightI <=0)
				{
				System.out.println("Invalid inches value. Must be positive.");
				}//end of if statment
		}//end of try statement
				
		catch (Exception e) {
			System.out.println("Invalid inches value. Must be a positive decimal.");
			stdIn.next();
		}//end of catch statement
				
	   



Now when I compile this it works, but when I type in something that is wrong, it just sits there doing nothing.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Try/Catch Problem

Posted 01 December 2009 - 04:25 PM

catch (Exception e) {
			System.out.println("Invalid feet value. Must be a positive integer.");
			stdIn.next();
}//end of catch statement



How do you expect that to work? the stdIn.next() is waiting for more input, (which is the "nothing" that you experienced). But, what happens if the user enters an invalid number in the catch statement? Fatal error...

Also, you parse to an integer and the variable is a double; no problem with this, just a little logic problem there. Also, just use the nextDouble() instead.

I would suggest something like this:
			while (true) {
				System.out.print("Enter height in inches: ");
				try {   
						heightF = stdIn.nextDouble();
						if(heightF <=0) // This I rewrote so that it would go into the catch statement
						{
							throw new Exception();
						}
				}//end of try statement
				catch (Exception e) {
					System.out.println("Invalid feet value. Must be a positive integer.");
					continue; // Resets the loop
				}//end of catch statement

				finally { // If the try block was successful...
					break; // break out of the loop
				}
		   }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1