Jump to content

'else' Without 'if'


HomerJaySimpson
 Share

Recommended Posts

Alright i've been trying to figure out why im getting thie else without if error for about a week now and can't find anything on the interweb. So it's time to ask someone who will know...

Here's the code i have so far...I have tried putting brackets in every order i can imagine but i ended up taking them out. I know the {} is probably why im getting errors but i can't figure it out...Please help :(

 

import InOut;

class Test1

{
public static void main (String[]args)

{
 String memType, insureType;
 String name, handicap, full, fiveDay, social;
 int count;
 int memCategory, insurePremium, annualSub;
 
 for (count=1; count <=9; count=count+1)
 {
	 name=InOut.readLine();
	 handicap=InOut.readLine();
	 memCategory=InOut.readInt();
	 insurePremium=InOut.readInt();	
	 
 if (memCategory <= 1000)
 {
	 annualSub = 200+insurePremium;
	 memType="Full";
	 full=full+1;
	 
	 else if (memCategory <= 1500)
   annualSub=120 +insurePremium;
   memType="fiveDay";
   fiveDay=fiveDay+1;	
   
  	 else
     annualSub=40;
     memType="social";
     social=social+1;
   }
	 
	 
	 if (insurePremium <= 15)
	 {
  	 insureType="Comprehensive";
   
  	 
	 else if (insurePremium <=10);
  	 insureType="3rd party injury";
  	 
   
	 else
  	 insureType="None";
	 }
	 
	 
	 
   System.out.println ("Name Handicap Membership Annual Subscription");
   
   System.out.println (""+name+"\t"+handicap+"\t"+memType+"\t"+annualSub);
 }
	 
	 System.out.println ("full fiveDay social");
	 
	 System.out.println (""+full+"\t"+fiveDay+"\t"+social);
}
}

Share this post


Link to post
Share on other sites

   else if (insurePremium <=10);
   insureType="3rd party injury";

 

the semicolon at the end of the else-if line

 

ACTUALLY

 

i'm pretty sure the format goes like this:

 

if (condition)
{
  statement1;
  statement2;
}
else if (condition)
{
  statement3;
  statement4;
}
else
{
  statement5;
  your_mum;
}

Share this post


Link to post
Share on other sites

do something like this

 

if (condition)
{
}
if (condition)
{
}
else (condition)
{
}

If one of the first 2 things id true it will do them (both if both are true) then if neither of the 1st 2 are ture it does the else :)

 

if you need it to do it differently (as in only choosing one option and never beign able to do 2)

 

use nested ifs

 

 

Im tryign to compile your code, but i dont personally use the InOut setup to get input. Im getting errors on your import line for that idk.

 

 

Ok other than InOut related errors, i have this that gives no if else problems, it was a mix of ; and {} errors.

 

 
import InOut;

class Test1
{
public static void main (String[]args)

{
String memType, insureType;
String name, handicap, full, fiveDay, social;
int count;
int memCategory, insurePremium, annualSub;

for (count=1; count <=9; count=count+1)
{
 name=InOut.readLine();
 handicap=InOut.readLine();
 memCategory=InOut.readInt();
 insurePremium=InOut.readInt();

if (memCategory <= 1000)
{
 annualSub = 200+insurePremium;
 memType="Full";
 full=full+1;

 if (memCategory <= 1500)
 {
  annualSub=120 +insurePremium;
  memType="fiveDay";
  fiveDay=fiveDay+1;
 }
 else
 {
    annualSub=40;
    memType="social";
    social=social+1;
 }
}


 if (insurePremium <= 15)
 {
   insureType="Comprehensive";
  
  
	 if (insurePremium <=10)
    insureType="3rd party injury";
  
  
else
   insureType="None";
 }



  System.out.println ("Name Handicap Membership Annual Subscription");
  
  System.out.println (""+name+"\t"+handicap+"\t"+memType+"\t"+annualSub);
}

 System.out.println ("full fiveDay social");

 System.out.println (""+full+"\t"+fiveDay+"\t"+social);
}
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...