• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

If else loop error

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been having problems in comiling a program and I was wondering how do I get rid of this error:
E:\School\CS161\Workbench\HiLow.java:37: 'else' without 'if'
else

Here is my code( it is not completely done yet...)
import cs1.Keyboard;
public class HiLow
{
//-----------------------------------------------------------------
// Plays a simple guessing game with the user.
//-----------------------------------------------------------------
public static void main (String[] args)
{
String str, another = "y";
int GUESSNUM, MAX = 100;
float RANNUM = 0;
float TRY = 0;
System.out.print ("I'm thinking of a number between 1 and "
+ MAX + ". Guess what it is: ");
GUESSNUM = Keyboard.readInt();
while (another.equalsIgnoreCase("y")) // allows y or Y
{
if (RANNUM = 0)
RANNUM = (int) (Math.random() * MAX) + 1;
else
do
if ( GUESSNUM > RANNUM)
System.out.print ("You are guessing high, try again? (y/n)");
another = Keyboard.readString();
else
if ( GUESSNUM < RANNUM)
System.out.print ("You are guessing low, try again? (y/n)");
another = Keyboard.readString();
else
System.out.print ("Your on the Money way to go!!!");
System.out.print ("You wanna play again? (y/n)");
another = Keyboard.readString();
}
}
}
Thanks
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think this might help you.

if your "if loop" specifies more than one action you need to enclose the conditions in brackets. Same with the else. In fact, I think all Java loops follow this rule!!
hope this helps
Kevin
[ February 06, 2002: Message edited by: Kevin Magee ]
[ February 06, 2002: Message edited by: Kevin Magee ]
 
Brian Walsh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only have one condition in these loops...and that is if (the guessed number is larger then the random number) then it will kick into the if statement..
Thanks I don't know why it isn't seeing the if statement wihtout the else.
 
Brian Walsh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the code to make it easier to look at then the original post.
 
Kevin Magee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I've messed up my explanation.
the code you've post is missing brackets.

I can't seem to add code properly.
The problem your having is after each if (condition) you need a bracket.
This bracket tells the complier, that everything from that point till the next bracket is part of the if. By default, the complier assumes that only one action will be performed.
In your code you there are no brackets, but two actions to be perform if the if condition is met. The complier only sees one action, and when it comes to the else statement, it can't associate that with the if because you have a rogue statement sitting between them.
Just put a bracket a the end of your if line
and before the else. e.g.
if (a > b){
a += 1;
b = c;}
else{
a =c;
b +=1;}
that should work
Kevin
[ February 06, 2002: Message edited by: Kevin Magee ]
 
Brian Walsh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANK YOU...IT DID
 
reply
    Bookmark Topic Watch Topic
  • New Topic