Question:
Else without If Java Error. Please help!!?
hpvs15c
2012-09-12 23:18:12 UTC
Please take a look at my java code below. Why am I getting the Else without If error on line 70 of my code? Thanks in advance. I've been up all night trying to figure this out.

import javax.swing.JOptionPane;

public class MyCalculatorTwo
{

public static void main(String [ ] args)
{
double first;
double second;
double power1;
double sum=0;
double difference=0;
String oneInputString;
String twoInputString;
String type;
String power=null;

String calctype = JOptionPane.showInputDialog("Will you be calculating one number or two numbers? (Please enter either \"one\" or \"two\")");

// Executes an operation with one number.

if(calctype.equals("one"))
{
oneInputString = JOptionPane.showInputDialog("Enter the first number.");
first = Double.parseDouble(oneInputString);

type = JOptionPane.showInputDialog("What type of operation would you like to execute? (Enter \"square root,\" \"cubic root,\" \"sine,\" \"power,\" or \"absolute value.\" ");

if(type.equals("square root"))
JOptionPane.showMessageDialog(null,"Answer: " + Math.sqrt(first));

else if (type.equals("cubic root"))
JOptionPane.showMessageDialog(null,"Answer: " + Math.cbrt(first));

else if (type.equals("sine"))
JOptionPane.showMessageDialog(null,"Answer: " + Math.sin(first));

else if (type.equals("power"))
{
power = JOptionPane.showInputDialog("To what power would you like to raise " +first+" to?");
power1 = Double.parseDouble(power);
JOptionPane.showMessageDialog(null,"Answer: " + Math.pow(first, power1));
}

else if (type.equals("absolute value"))
JOptionPane.showMessageDialog(null,"Answer: " + Math.abs(first));

else
JOptionPane.showMessageDialog(null,"Invalid entry. Please enter either \"square root,\" \"cubic root,\" \"sine,\" \"power,\" or \"absolute value.\"");
}

// Executes an operation with two numbers.

else if(calctype.equals("two"))
{
oneInputString = JOptionPane.showInputDialog("Enter the first number.");
first = Double.parseDouble(oneInputString);

twoInputString = JOptionPane.showInputDialog("Enter the second number.");
second = Double.parseDouble(twoInputString);

type = JOptionPane.showInputDialog("What type of operation would you like to execute? Enter either \"addition,\" \"subtraction,\" \"multiplication,\" or \"subtraction.\"");

if (type.equals("addition"))
sum = first + second;
JOptionPane.showMessageDialog(null, "Answer: " + sum);

else if (type.equals("subtraction"))
difference = first - second;
JOptionPane.showMessageDialog(null, "Answer: " + difference);




}

else
{
System.out.println("Invalid entry. Please enter either \"one\" or \"two.\"");
}
}

}
Five answers:
James
2012-09-12 23:27:38 UTC
in this part:



if (type.equals("addition"))

sum = first + second;

JOptionPane.showMessageDialog(null, "Answer: " + sum);



should be:



if (type.equals("addition")) {

sum = first + second;

JOptionPane.showMessageDialog(null, "Answer: " + sum);

}



because this if has two statements, they need to be enclosed in curlybraces



Edit: just ran this program. You should apply the curlybraces to the "else if" part as well, otherwise the "difference" dialog will pop up regardless of whether the user enters addition or subtraction.
cazeault
2016-08-02 08:34:39 UTC
Given the quantity of stipulations you might be checking for, you can also want to seem at using the Java "swap" instead than a series of if/else if. If you're set on making use of the if/else if: if () // do anything else if // do something else As a "swap" illustration: switch (option) case 1: // do choice 1 matters here spoil; case 2: // do option 2 things here spoil; case three: // do option 3 matters here break; default: // do a default set of matters if the above instances do not exist to your obstacle, your missing a bracket after your "else if (alternative==4)", except of path you most effective need the "procedure.Out.Println(=============");" line to be within the else. You need to bracket the team of strains after the condition if you need all of them to run. Given the error message (and your earlier questions on the chunk of code), you can also wish to appear on the formatting of your editor. Make sure your code is indented and that your brackets fit up. If it helps, add comment traces above/beneath each and every pair to identify them.
Techno
2012-09-12 23:28:20 UTC
You can Not use else, then else if...

the "else if" is a spefic condition you specify what you have.

but when you use "Else" here it should be your last condition, no specific reason, means everything else so you end it with else.

you cannot either use else multiple times, you just use it once in the end generalising everything else in one solution.

so as long as you have conditions you use "If" or "Else If".



gd luck.
Laurence I
2012-09-12 23:28:07 UTC
if you join up your braces, the last else does not belong to any IF

this means you have a brace in the wrong place.



print it out and join braces with a pencil, working inwards
green meklar
2012-09-13 11:15:39 UTC
Please, add curly brackets to your if and else statements. It is so, so, so much easier to read that way.



Incidentally, in this case, it will also fix your problem.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...