'else' without 'if'
807600Sep 23 2007 — edited Sep 23 2007Hi There,
Just another lost student. I am taking an web-based java class through a local community college. I have not done any type of programming in over 20 years. I've read through the new to java forum and suspect that my problem is either (missing semi-colons and/or brackets) and/or (semi-colons and/or brackets in the wrong place). I've changed things around several times, but can't get passed the compiler errors.
***Errors:***
Lab4.java:47: 'else' without 'if'
else if (operator == '-')
^
Lab4.java:50: 'else' without 'if'
else if (operator == '*' | operator == 'x' | operator == 'X')
^
Lab4.java:53: 'else' without 'if'
else if (operator == '/')
^
Lab4.java:56: 'else' without 'if'
else
^
Lab4.java:58: 'else' without 'if'
else
^
5 errors
Program:
import java.util.*;
public class Lab4
{
static Scanner console = new Scanner(System.in);
public static void main (String[] args)
{
char operator;
double x;
double y;
double z;
x = 1.0;
y = 1.0;
z = x + y;
System.out.print("Enter the first number: ");
System.out.flush();
x = console.nextDouble();
System.out.println();
System.out.print("Enter the second number: ");
System.out.flush();
y = console.nextDouble();
System.out.println();
System.out.print("Enter the operator: ");
System.out.flush();
operator = console.next().charAt(0);
System.out.println();
if ((y != 0) && (operator =='/'))
{
if (operator == '+')
z = x + y;
System.out.println (x + (" + ") + y +(" = ") + z);
else if (operator == '-')
z = x - y;
System.out.println (x + (" - ") + y + (" = ") + z);
else if (operator == '*' | operator == 'x' | operator == 'X')
z = x * y;
System.out.println (x + (" * ") + y + (" = ") + z);
else if (operator == '/')
z = x / y;
System.out.println (x + (" / ") + y + (" = ") + z);
else
} System.out.println("Invalid operator entered, please start again.");
else
System.out.println("Invalid input, the denominator cannot be zero.");
}
}
Thank you in advance for any assistance offered!