Results 1 to 8 of 8
  1. #1
    jmcquaid1987 is offline Member
    Join Date
    Mar 2014
    Posts
    12
    Rep Power
    0

    Default A chockful of errors..please help

    For starters, I am new to this forum, and Java. I'm taking a Java course in college, and this week's assignment really has me stumped. I have to produce output that looks like this:

    Brand: Nikon; Resolution: 10.0 mp; Price: $129.0
    Brand: Sony; Resolution: 3.0 mp; Price: $99.0
    Brand: Canon; Resolution: 8.0 mp; Price: $129.0
    Brand: Dell; Resolution: 2.0 mp; Price: $99.0

    The user has to enter the Brand Name, and then the Megapixels, for 4 different cameras. Here's my code for the DigitalCameraTest class:

    public class DigitalCameraTest {
    String Brand;
    private double megaPixels;
    private double Price;

    //returns a text value of each variable
    public String toString() {
    return "Brand: " + Brand + "; Resolution: " + megaPixels + "; Price: $" + Price ;
    }

    public DigitalCameraTest(String newBrand, double newMegaPixels) {
    Brand=newBrand;
    megaPixels=newMegaPixels;
    If (newMegaPixels > 10) {
    megaPixels=10;
    }
    else {
    megaPixels=newMegaPixels;
    }

    If (newMegaPixels > 6) {
    Price=129.00;
    }
    else {
    Price=99.00;
    }
    }


    Here are the errors detected by the compiler:

    C:\Users\Justin\Java\Week05>javac DigitalCamera.java
    .\DigitalCameraTest.java:14: error: ';' expected
    If (newMegaPixels > 10) {
    ^
    .\DigitalCameraTest.java:17: error: 'else' without 'if'
    else {
    ^
    .\DigitalCameraTest.java:21: error: ';' expected
    If (newMegaPixels > 6) {
    ^
    .\DigitalCameraTest.java:24: error: 'else' without 'if'
    else {
    ^
    .\DigitalCameraTest.java:27: error: reached end of file while parsing
    }


    Why would it want a semicolon after newMegaPixels? Also, why would it tell me to make an else if statement rather than just an else? Finally, what is wrong with my brackets? I'm grateful for any and all advice you may have! Thanks!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,029
    Rep Power
    6

    Default Re: A chockful of errors..please help

    It should be if, not If (case matters).
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jmcquaid1987 is offline Member
    Join Date
    Mar 2014
    Posts
    12
    Rep Power
    0

    Default Re: A chockful of errors..please help

    I lower-cased the if's, and it still gives me this error:






    C:\Users\Justin\Java\Week05>javac DigitalCamera.java
    .\DigitalCameraTest.java:18: error: illegal start of type
    else {
    ^
    .\DigitalCameraTest.java:18: error: ';' expected
    else {
    ^
    .\DigitalCameraTest.java:19: error: <identifier> expected
    megaPixels=newMegaPixels;
    ^
    .\DigitalCameraTest.java:22: error: class, interface, or enum expected
    if (newMegaPixels > 6) {
    ^
    .\DigitalCameraTest.java:24: error: class, interface, or enum expected
    }
    ^
    .\DigitalCameraTest.java:27: error: class, interface, or enum expected
    }
    ^
    .\DigitalCameraTest.java:19: error: cannot find symbol
    megaPixels=newMegaPixels;
    ^
    Last edited by jmcquaid1987; 03-12-2014 at 04:40 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,029
    Rep Power
    6

    Default Re: A chockful of errors..please help

    Looks like you are also missing a closing brace on the class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jmcquaid1987 is offline Member
    Join Date
    Mar 2014
    Posts
    12
    Rep Power
    0

    Default Re: A chockful of errors..please help

    public class DigitalCameraTest {
    String Brand;
    private double megaPixels;
    private double Price;

    //returns a text value of each variable
    public String toString() {
    return "Brand: " + Brand + "; Resolution: " + megaPixels + "; Price: $" + Price ;
    }

    public DigitalCameraTest(String newBrand, double newMegaPixels) {
    Brand=newBrand;
    megaPixels=newMegaPixels;
    if (newMegaPixels > 10) {
    megaPixels=10;
    } else {
    megaPixels=newMegaPixels;
    }

    if (newMegaPixels > 6) {
    Price=129.00;
    } else {
    Price=99.00;
    }
    }
    }


    All fixed, thanks! Now to work on the main class!

  6. #6
    jmcquaid1987 is offline Member
    Join Date
    Mar 2014
    Posts
    12
    Rep Power
    0

    Default Re: A chockful of errors..please help

    Okay, I've now created the main class which will utilize the DigitalCameraTest Class. Here is the code:

    System.out.print("Brand Name: ");
    newBrand=keyboard.nextLine();

    System.out.print("Megapixels: ");
    newMegaPixels=keyboard.nextDouble();

    System.out.print("Brand Name: ");
    newBrand=keyboard.nextLine();

    System.out.print("Megapixels: ");
    newMegaPixels=keyboard.nextDouble();

    System.out.print("Brand Name: ");
    newBrand=keyboard.nextLine();

    System.out.print("Megapixels: ");
    newMegaPixels=keyboard.nextDouble();

    System.out.print("Brand Name: ");
    newBrand=keyboard.nextLine();

    System.out.print("Megapixels: ");
    newMegaPixels=keyboard.nextDouble();

    System.out.println(d1);
    System.out.println(d2);
    System.out.println(d3);
    System.out.println(d4);
    }
    }

    It surprisingly only gave me one error, but it's one I've never seen before:

    C:\Users\Justin\Java\Week05>javac DigitalCamera
    error: Class names, 'DigitalCamera', are only accepted if annotation processing
    is explicitly requested
    1 error

    What does that mean?

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: A chockful of errors..please help

    Quote Originally Posted by jmcquaid1987 View Post
    Okay, I've now created the main class which will utilize the DigitalCameraTest Class. Here is the code:

    System.out.print("Brand Name: ");
    newBrand=keyboard.nextLine();

    System.out.print("Megapixels: ");
    newMegaPixels=keyboard.nextDouble();

    System.out.print("Brand Name: ");
    newBrand=keyboard.nextLine();

    System.out.print("Megapixels: ");
    newMegaPixels=keyboard.nextDouble();

    System.out.print("Brand Name: ");
    newBrand=keyboard.nextLine();

    System.out.print("Megapixels: ");
    newMegaPixels=keyboard.nextDouble();

    System.out.print("Brand Name: ");
    newBrand=keyboard.nextLine();

    System.out.print("Megapixels: ");
    newMegaPixels=keyboard.nextDouble();

    System.out.println(d1);
    System.out.println(d2);
    System.out.println(d3);
    System.out.println(d4);
    }
    }

    It surprisingly only gave me one error, but it's one I've never seen before:

    C:\Users\Justin\Java\Week05>javac DigitalCamera
    error: Class names, 'DigitalCamera', are only accepted if annotation processing
    is explicitly requested
    1 error

    What does that mean?
    It is counterproductive, for both you and the people on the forums, to be asking what every compiler error you get is and how to fix it.
    A simple hit on the web will solve almost any similar issue you are having.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,029
    Rep Power
    6

    Default Re: A chockful of errors..please help

    Please check out the Oracle Java tutorials linked to in my signature.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Why do I keep getting these errors?
    By psx2514 in forum New To Java
    Replies: 9
    Last Post: 11-07-2012, 06:03 PM
  2. Errors
    By Johanis in forum New To Java
    Replies: 10
    Last Post: 07-26-2012, 06:19 PM
  3. Gui with errors
    By scooter24 in forum New To Java
    Replies: 2
    Last Post: 07-04-2011, 06:29 PM
  4. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  5. Replies: 3
    Last Post: 03-08-2009, 02:51 AM

Posting Permissions

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