aminkeith
asked on
else without if error
I can't figure out why I am getting an else without if error on line 28 of this program. I am using jgrasp ide. I've copied the program below :
//converts temperatures from centigrade to farenheit and vice versa using manuel input from the user
import java.util.Scanner;
public class tempconversion
{
public static void main (String[] args)
{
Scanner stdin = new Scanner (System.in) ;
Scanner F2C = new Scanner (System.in) ;
System.out.println ( "If you are converting F ==> C press 0 " ) ;
System.out.println ( "If you are converting C ==> F press 1 " ) ;
F2C.nextDouble() ;
if( F2C == 0){
while (stdin.hasnext () ) {
System.out.println ( "Enter next input : " );
stdin.nextDouble () ;
tC= ((stdin * 9.0)/ 5.0)+ 32.0 ;
System.out.println( " Input : " + (stdin.nextDouble));
System.out.println ( "Converted : " + ( tC )) ;
}
else
while (stdin.hasnext () ) {
System.out.println ( "Enter next input : " );
stdin.nextDouble () ;
tF= ((stdin - 32.0) * 5.0) / 9.0 ;
System.out.println( " Input : " + (stdin.nextDouble));
System.out.println ( "Converted : " + ( tF ));
}
}
System.out.println ( "Good Bye " ) ;
}
}
//converts temperatures from centigrade to farenheit and vice versa using manuel input from the user
import java.util.Scanner;
public class tempconversion
{
public static void main (String[] args)
{
Scanner stdin = new Scanner (System.in) ;
Scanner F2C = new Scanner (System.in) ;
System.out.println ( "If you are converting F ==> C press 0 " ) ;
System.out.println ( "If you are converting C ==> F press 1 " ) ;
F2C.nextDouble() ;
if( F2C == 0){
while (stdin.hasnext () ) {
System.out.println ( "Enter next input : " );
stdin.nextDouble () ;
tC= ((stdin * 9.0)/ 5.0)+ 32.0 ;
System.out.println( " Input : " + (stdin.nextDouble));
System.out.println ( "Converted : " + ( tC )) ;
}
else
while (stdin.hasnext () ) {
System.out.println ( "Enter next input : " );
stdin.nextDouble () ;
tF= ((stdin - 32.0) * 5.0) / 9.0 ;
System.out.println( " Input : " + (stdin.nextDouble));
System.out.println ( "Converted : " + ( tF ));
}
}
System.out.println ( "Good Bye " ) ;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the closing bracket above the else is closing the open bracket for the while statement.
The opening bracket after "if( F2C == 0)" has not yet been closed before the else...you need another closing bracket before the else...
The opening bracket after "if( F2C == 0)" has not yet been closed before the else...you need another closing bracket before the else...
I know this bracing style is now the "in" way...but I learned an older way where the brace gets its OWN line, and indents are always 3 or more characters.
This way you can always see the opening and closing braces "line up"!
This way you can always see the opening and closing braces "line up"!
ASKER
That eliminated the else without if error, but now I'm getting these 3 errors....
tempconversion.java:41: error: <identifier> expected
System.out.println ( "Good Bye " ) ;
^
tempconversion.java:41: error: illegal start of type
System.out.println ( "Good Bye " ) ;
^
tempconversion.java:45: error: class, interface, or enum expected
}
^
3 errors
tempconversion.java:41: error: <identifier> expected
System.out.println ( "Good Bye " ) ;
^
tempconversion.java:41: error: illegal start of type
System.out.println ( "Good Bye " ) ;
^
tempconversion.java:45: error: class, interface, or enum expected
}
^
3 errors
You need an opening brace ( { ) following the else.
If your IDE doesn't support auto-completion of brackets, then it's a good practice to immediately close any brace that you open so that you can avoid omitting a brace as your code grows in size.