-
')' and '(' expected
Hi,
I am sorry to have to ask this question as it's asked so many times, but I continuously receive ';' and ')' expected errors.
I started using Java just yesterday so the program is really basic but still any help would be greatly appreciated.
Program
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner scans = new Scanner(System.in);
double kg;
double answer;
System.out.println("Kg to Lbs Converter");
System.out.println("Kg to Lbs or Lbs to Kg");
double kgtolbs = 0;
double lbstokg = 0;
if kgtolbs = scans.nextString("Kg to Lbs") {
System.out.println("Kg to Lbs is selected");
}
if lbstokg = scans.nextString("Lbs to Kg") {
System.out.println("Lbs to Kg is selected");
}
}
}
And here is the Error code:
Test1.java:12: '(' expected
if kgtolbs = scans.nextString("Kg to Lbs") {
^
Test1.java:18: illegal start of expression
}
^
Test1.java:17: ')' expected
}
^
3 errors
Thanks in advanced.
-
Check out the proper syntax for an if statement: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Hint: Your compiler is telling you exactly what the problem is.
-
Thanks,
I believe that I did it properly this time, but I know get ';' expected but only one error:
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner scans = new Scanner(System.in);
double kg;
kgtolbsselect = scans.nextString("Kg to Lbs");
lbstokgselect = scans.nextString("Lbs to Kg");
double lbs;
double answer;
System.out.println("Kg to Lbs Converter");
System.out.println("Kg to Lbs or Lbs to Kg");
double kgtolbs = 0;
double lbstokgequation = lbs * 0.4536;
double kgtolbsequation = kg * 2.205;
kgtolbsequation() {
if(kgtolbsselect) {
System.out.println("Kg to Lbs is selected");
}
}
lbstokgequation() {
if(lbstokgselect) {
System.out.println("Lbs to Kg is selected");
}
}
}
}
Here is the error know:
Test2.java:17: ';' expected
kgtolbsequation() {
^
-
It appears you are defining methods inside of main, these methods also aren't complete.
This line
Code:
kgtolbsequation() {
is one problem.
-
You can't have methods within methods. And put the code in [code] tags, right now it's very hard to read. Like this:
Code:
method() {
// code goes here
}