AllExperts > Experts 
Search      

Java

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Artemus Harper
Expertise
I have a BS in computer science and am working towards a Masters degree.

Experience
I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection, and Java java.nio.* knowledge of Java bytecode and annotations. Basics in c++ and c#
 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > If Else statements and ; expected

Topic: Java



Expert: Artemus Harper
Date: 7/1/2008
Subject: If Else statements and ; expected

Question
I am using If Else statements in a form of repeating Else If statements.
If (calculated = 10); {
           return 55;
       }Else If (calculated = 9); {
Is in my code but on the first Else it asks for a semicolon. Why would this be?

Answer
The compiler is very confused on what you are trying to do since this has many problems.

The problems are:
1. if and else are all lower case, it won't work if any of the letters are upper case

2. Use == in an if statement, not =. == is used for comparison, while = is used for assignment.

3. Do not put a ; after the if (...) statement (or any where a { will follow)

So your code should look like:

if (calculated == 10) {
  return 55;
} else if (calculated == 9) {
...

Also note that if you are planning to do each number, a switch statement works better:

switch(calculated) {
  case 10:
     return 55;
  case 9:
     //do something
  case 8:
     //do something else
  ...
  default:
    //do something when none of the other values are met
}

note that with switch statements, if you do not use a return, use a break instead.

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.