Results 1 to 5 of 5
  1. #1
    Bindlz is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default ')' 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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default

    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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Bindlz is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    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() {
    ^

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It appears you are defining methods inside of main, these methods also aren't complete.

    This line
    Java Code:
    kgtolbsequation() {
    is one problem.

  5. #5
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    You can't have methods within methods. And put the code in [code] tags, right now it's very hard to read. Like this:
    Java Code:
    method() {
      // code goes here
    }

Similar Threads

  1. '{' expected
    By elliojonhy in forum New To Java
    Replies: 3
    Last Post: 12-01-2010, 09:26 PM
  2. ';' expected
    By lala in forum New To Java
    Replies: 2
    Last Post: 11-24-2010, 08:02 PM
  3. .class expected
    By dre in forum New To Java
    Replies: 6
    Last Post: 06-27-2009, 06:01 PM
  4. identifier expected
    By tlouvierre in forum New To Java
    Replies: 4
    Last Post: 05-28-2009, 12:11 AM
  5. MSG ERROR: : expected
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 02:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •