aspose file tools*
The moose likes Beginning Java and the fly likes Loop problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loop problem" Watch "Loop problem" New topic
Author

Loop problem

Linda Guy
Greenhorn

Joined: Oct 08, 2006
Posts: 8
I'm going crazy trying to find what I'm doing wrong in the code below. I'm trying to have the user enter a number between 5 and 15 (inclusive), then use a loop to return the right answer to their input, and keep going until they enter a correct number.

At: (number >= 5) && (number <= 15) I get the error:
Syntax error on token "&&", throw expected
Type mismatch: cannot convert from boolean to Throwable

Can someone point me in the right direction to fix this please?

Thanks




Grandmother of 8, and a college student!
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
1) number is never initialized
2) Then you try to compare number to a couple of integers. Since number isn't equal to something how can it be compared to anything?
3) You apparently don't know the syntax for an if statement
4) Then you terminate your if statement header with a semi-colon

etc.
etc.
etc.

Computer programming requires precise syntax, properly placed braces, and easy to read indenting. You aren't off to a good start.

I suggest you write a simple program as an exercise. Your program should declare and assign a variable the value 3. The next line should be an if statement that checks if the variable is equal to 3. Then, only if the variable equals 3, do two things:

1) add 2 to the variable
2) write code to display the variable.

After you get that to run successfully, change the value of the variable to 2 on the first line(instead of 3). Run your program again. This time, there should be no output. Did your program work correctly?
[ October 27, 2006: Message edited by: sven studde ]
Linda Guy
Greenhorn

Joined: Oct 08, 2006
Posts: 8
Your right, I don't really know the syntax for an if statement...that's what I'm really trying to learn.

It was my mistake in naming my INT. I fixed all you suggested that I could. Only error now is: (Syntax error on token "&&", if expected)

Suggestion to fix this? I checked the tutorials at Sun and I think I have it right.
Thanks

sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
1) Integer is never initialized.
1a) If you can't think up a more descriptive name for a variable than Integer, I suggest you give up programming right now.
2) Then you try to compare Integer to a couple of numbers. Since Integer isn't equal to something how can it be compared to anything?
3) You apparently don't know the syntax for an if statement

You knocked number 4) off the list. Congrats!

I suggest you write a simple program as an exercise. Your program should declare and assign a variable the value 3. The next line should be an if statement that checks if the variable is equal to 3. Then, only if the variable equals 3, do two things:

1) add 2 to the variable
2) write code to display the variable.

After you get that to run successfully, change the value of the variable to 2 on the first line(instead of 3). Run your program again. This time, there should be no output. Did your program work correctly?

Personally, I won't respond to this thread again unless you post the code for the exercise I suggested.
[ October 28, 2006: Message edited by: sven studde ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Come on people... be nice... remember the rules of this forum.


Anyway, let's fix one issue. You figured out that the "if" statement will execute the code in the brackets if the condition yields true.



Another concern is that the "if" statement only takes a single boolean value. And the precedence of the language is not designed to parse for it, the way you have it written. You need to make sure it is a single boolean value... like so...



So basically, place the entire expression that you want to conditionally branch on with a overall ()'s.

Henry
[ October 28, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Linda Guy
Greenhorn

Joined: Oct 08, 2006
Posts: 8
Hi Henry!

I got the if statement to work with help from the 'sun' forum. Grumpy sven was there too, but still no help.

I'm now working on changing the problem to a while statement so it will loop until the user enters a correct number.

I have it looping once and then it closes. I'm going to comb the Internet to find how to make it keep going until the correct number is put in.

Here's my code so far if you want a look....

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Linda,

The while loop only executes the next line (assuming you don't use {} to specify a block). So the following code...



Means if the user enter the correct response to keep telling the user the he/she got it right.

This code...



Is after the while loop -- meaning that it will be called after the while loop finishes. This is probably not what you want as you want to try again.

Another issue is that you ask for the user to enter another number, but unlike the first time, you don't save the input, or parse the number.

Henry
[ October 29, 2006: Message edited by: Henry Wong ]
Jing Liang
Ranch Hand

Joined: Oct 23, 2006
Posts: 40
In your code, you shoudl use || operator instead of the && operator. choice can not be both less than 5 and greater than 15.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Originally posted by Jing Liang:
In your code, you shoudl use || operator instead of the && operator. choice can not be both less than 5 and greater than 15.


The code being shown is for a choice greater or equal to 5, and less than or equal to 15.

Where in the code is choice being checked for "both less than 5 and greater than 15"?

Henry
Jing Liang
Ranch Hand

Joined: Oct 23, 2006
Posts: 40
I would use while (choice < 5 || choice > 15) instead of while (choice >=5 && choice <=5)
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
People, people calm down already. Remember the rules of this forum.

Originally posted by Jing Liang:
I would use while (choice < 5 || choice > 15) instead of while (choice >=5 && choice <=5)

Ok, but then your loop would execute when choice is something like 1 or 20, but not when choice is 7 or 10. What makes you think the op wants the loop to execute when choice is 1 or 20, and not when choice is 7 or 10? It seems pretty clear that if someone writes:

if choice>=5 && choice<=15

they want their loop to execute when choice is 7 or 10 and NOT when choice is 1 or 20.
[ October 29, 2006: Message edited by: sven studde ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop problem