# Solved: If-Else statement in Java



## DYS (Dec 29, 2008)

I have yet another problem with Java coding. This is what I have to construct:



> Write a program that reads in a series of numbers from the keyboard (terminating when a negative number is typed), multiples them by 3 and writes the answers into a file, one per line.


And this is the code I have attempted:



> import javax.swing.JOptionPane;
> import java.io.*;
> 
> class MultiplyThreeFile
> ...


I am using JCreator and when I try to compile the file it gives me an error: "_'else' without 'if'_". I have never had any luck with If-Else statements, so can anyone help me out at all, please?

Also, any advice or solutions on how to write this code (if I have gone the wrong way about it) would be much appreciated.

Thanks in advance


----------



## Greg8537481665 (Jan 31, 2009)

There is an ELSE without an IF. I would try correctly bracketing 
IF (boolean evaluation) {
statement 1
statemnt 2
}else
{
statement 1
statement 2
{

Ive not developed in Java in years but if memory serves..this should work.


----------



## mt2002 (Sep 14, 2005)

(C++ programmer here. Syntax is nearly the same though)

I would like to expand on the above post.

You can only use the syntax without brackets if you only have a single statement. So, this is valid:


```
if (expression)
   statement;
```
In the above, statement will be executed if expression==true.

In the below code, it will still work but not be what you expect:


```
if (expression)
   statement1; // only this is inside of the if
   statemen2; // this is outside your if statement
```
The statement2 will be treated outside of the scope of the *if* statement which throws off your *if..else* construct. Only statement1 is inside the *if*. If you want multiple statements to be executed within an if, use brackets (as the above poster mentioned) :


```
if (expression) {
   statement1; // both of these will be executed if the expression is true
   statement2;
}
```


----------



## Greg8537481665 (Jan 31, 2009)

Nice call Mt2002, I remembered some rule about one line after the IF but couldn't fully recall it..Ive not used Java in about 2 years now...


----------



## DYS (Dec 29, 2008)

Thanks guys.

This seems such a simple problem, but believe it or not, my University or textbooks did/do not mention anything about adding brackets if there is more than on statement - pretty lousy eh?

Thanks again, appreciate all the help I receive


----------



## JimmySeal (Sep 25, 2007)

It's actually considered a best practice to use braces with if-statements, even if the if-statement only executes one line:

http://java.dzone.com/news/omitting-braces-not-just-a-mat

There are those who disagree with this practice, but it's one that I use.


----------



## Greg8537481665 (Jan 31, 2009)

well I would throw your book in the trash and use this link:
http://java.sun.com/docs/books/tutorial/


----------



## mt2002 (Sep 14, 2005)

I actually prefer small if-statements without brackets. Even the ?: operator is great for small one-liner if statements. Of course, this is just my personal style.

From your link, JimmySeal, that problem can be easily fixed by proper formatting of the code.


----------



## DYS (Dec 29, 2008)

Ha ha, yeah my books are rubbish.

The first book I got is the one that my module leaders recommend, which sucks. I went and got two more that looked alright. Upon further examination, they also sucked.

Thanks for the links people


----------



## mt2002 (Sep 14, 2005)

You can always read thinking in C++ two full e-books online and quite good.


----------

