Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Someone help me with my code it has some errors? Thanks!

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Someone help me with my code it has some errors? Thanks!

    Screen Shot 2012-11-01 at 4.45.22 PM.png
    Errors in order:
    Syntax error on tokens, delete these tokens
    Syntax error on tokens, Expression expected instead
    Syntax error on tokens, Expression expected instead
    Syntax error on tokens, delete these tokens

    This is my worker class:
    package HW07;
    //Bulb.java
    public class Bulb
    {
    //Instance Data
    private boolean bulbLit;
    //Constructor
    public Bulb (boolean bulbLitIn)
    {
    bulbLit = bulbLitIn;
    }
    //bulb setter off
    public void turnOff()
    {
    bulbLit = false;
    }
    //bulb setter on
    public void turnOn ()
    {
    bulbLit = true;
    }
    public String toString()
    {
    String result = “ ”;
    result += “This bulb is currently “;
    if(bulbLit)
    result += “on”;
    else
    result += “off”
    return result;
    }
    }

    This is my driver class:
    package HW07;
    public class HW07
    {
    public static void main (String[] args)
    {
    //Instantiate 2 bulb objects.
    Bulb bulb1 = new Bulb (false);
    Bulb bulb2 = new Bulb (false);
    //Bulb1 is turned on
    bulb1.turnOn();
    //Print out each bulbs original information.
    System.out.println ("Bulb 1: " +bulb1);
    System.out.println ("Bulb 2: " +bulb2);
    //Turn off bulb1 turn on bulb2
    bulb1.turnOff();
    bulb2.turnOn();
    //Print out each bulbs original information.
    System.out.println ("Bulb 1: " +bulb1);
    System.out.println ("Bulb 2: " +bulb2);
    }
    }

    This is the output:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error on tokens, delete these tokens
    Syntax error on tokens, Expression expected instead
    Syntax error on tokens, Expression expected instead
    Syntax error on tokens, delete these tokens

    at HW07.Bulb.toString(Bulb.java:35)
    at java.lang.String.valueOf(String.java:2826)
    at java.lang.StringBuilder.append(StringBuilder.java: 115)
    at HW07.HW07.main(HW07.java:20)


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Someone help me with my code it has some errors? Thanks!

    Quote Originally Posted by skitheeast8 View Post
    String result = “ ”;
    On which line is the error occuring? Is it the above line? Those quotations look like special word processor characters and I think the compiler does not know what they are.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Someone help me with my code it has some errors? Thanks!

    Oh wow, I'm an idiot.
    Well, thankfully someone picked up on my little mistake! Thank you!

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Someone help me with my code it has some errors? Thanks!

    Quote Originally Posted by skitheeast8 View Post
    Screen Shot 2012-11-01 at 4.45.22 PM.png
    Errors in order:
    Syntax error on tokens, delete these tokens
    Syntax error on tokens, Expression expected instead
    Syntax error on tokens, Expression expected instead
    Syntax error on tokens, delete these tokens

    This is my worker class:
    I think it is rejecting the quote marks that you have in Bulb.java.


    package HW07;
    //Bulb.java   
    public class Bulb
    {
    //Instance Data
    private boolean bulbLit;
    //Constructor
    public Bulb (boolean bulbLitIn)
    {
    bulbLit = bulbLitIn;
    }
    //bulb setter off
    public  void turnOff()
    {
    bulbLit = false;
    }
    //bulb setter on
    public void turnOn ()
    {
    bulbLit = true;
    }
    public String toString()
    {
    String result = “  ”; //<---From Zaphod_b Bad quote marks.
    result += “This bulb is currently “;//<---From Zaphod_b Bad quote marks.
    if(bulbLit)
    result += “on”;//<---From Zaphod_b Bad quote marks.
    else
    result += “off”//<---From Zaphod_b Bad quote marks. (Also missing semicolon)
    return result;
    }
    }

    What happens if you try
    package HW07;
    //Bulb.java   
    public class Bulb
    {
        //Instance Data
        private boolean bulbLit;
     
        //Constructor
        public Bulb (boolean bulbLitIn)
        {
            bulbLit = bulbLitIn;
        }
     
        //bulb setter off
        public  void turnOff()
        {
            bulbLit = false;
        }
     
        //bulb setter on
        public void turnOn()
        {
            bulbLit = true;
        }
     
        public String toString()
        {
            String result = "  ";
            result += "This bulb is currently ";
            if(bulbLit)
                result += "on";
            else
                result += "off";
            return result;
        } // End toString
    } // End class definition


    Cheers!

    Z
    Last edited by Zaphod_b; November 1st, 2012 at 06:17 PM.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Someone help me with my code it has some errors? Thanks!

    Quote Originally Posted by Zaphod_b View Post
    I think it is rejecting the quote marks that you have in Bulb.java.
    I wish I had thought of that!
    Improving the world one idiot at a time!

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Someone help me with my code it has some errors? Thanks!

    Quote Originally Posted by Junky View Post
    I wish I had thought of that!
    I apologize for stepping on your toes with my redundant post.

    I am trying to break myself of the habit of spending a few minutes crafting a response and then forgetting to see if someone else has already answered before I post. I used to do an immediate delete of the post when I discovered that I had done that, but people complained about that too.


    Bottom line: I'll try to do better.


    Cheers!

    Z

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Someone help me with my code it has some errors? Thanks!

    People just love to whine.
    Improving the world one idiot at a time!

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Someone help me with my code it has some errors? Thanks!

    That reminds me of what I sometimes do. I reply to a post without realising there is a second page and people have already responded.
    Improving the world one idiot at a time!

Similar Threads

  1. Errors with beginning jOptionPane code
    By LoganC in forum What's Wrong With My Code?
    Replies: 12
    Last Post: September 29th, 2012, 12:23 PM
  2. weird errors in my code
    By jobje325 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 17th, 2012, 11:15 AM
  3. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  4. I still have errors. Can you PLEASE correct my code?!
    By sam30317 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2011, 06:10 AM
  5. Please check the code for the errors
    By nrao in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 16th, 2010, 05:37 PM