Results 1 to 9 of 9

Thread: Kind of stuck

  1. #1
    Nicky Swans is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default Kind of stuck

    Hey guys I have been doing an assignment first time I have used Java and I am not sure why my while statements dont seem to work? What I am trying to do is to re-ask a user a question if they enter a false answer. There are only two correct answers blue or white and if they enter purple I want it to be re-asked. Could anyone give me some help with this?

    Java Code:
        System.out.println("Please choose a colour, blue or white?");
        colour = scanner.nextLine();
        colour = colour.toLowerCase(); // Changes the case of the String colour to lower case rather than ignoring it
    
    if (colour.equals("white"))
          {
             System.out.println("Hello " + forename + " " + surname + ", Swansea City play in white.");
          }
          else if (colour.equals("blue"))
          {
             System.out.println("Hello " + forename + " " + surname + ", Cardiff City play in blue.");
          }
        else 
          {
             System.out.println("Sorry," + forename +", I did not understand you");
        }
      }
    }
    Thanks, complete newb to java I rarely ask for help when it comes to things but this is something that confuses me.

  2. #2
    Beginner's Avatar
    Beginner is offline Member
    Join Date
    Apr 2010
    Posts
    25
    Rep Power
    0

    Default

    If you could, please post the whole thing

  3. #3
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default

    Hi,

    Instead of
    Java Code:
    colour.equals("white")
    try
    Java Code:
    colour.compareTo("white") == 0
    Greetings

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Dipke View Post
    Hi,

    Instead of
    Java Code:
    colour.equals("white")
    try
    Java Code:
    colour.compareTo("white") == 0
    Greetings
    What is the benefit of doing this?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nicky Swans View Post
    I am not sure why my while statements dont seem to work?
    I don't see any while statements in your code ...

    kind regards,

    Jos

  6. #6
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default

    Hi,
    Java Code:
    What is the benefit of doing this?

    Because compareTo compares the text.
    I am using this and it works

    greetz

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Dipke View Post
    Hi,
    Java Code:
    What is the benefit of doing this?

    Because compareTo compares the text.
    I am using this and it works
    The compareTo( ... ) methods compares two ordered objects o1 and o2 where o1 < o2 or o1 == o2 or o1 > o2. The equals( ... ) method just checks whether o1 == o2 or o1 != o2. The second approach should be enough for the OP's problem.

    kind regards,

    Jos

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Quote Originally Posted by Dipke View Post
    Hi,
    Java Code:
    What is the benefit of doing this?

    Because compareTo compares the text.
    I am using this and it works

    greetz
    It may work, but how does using a method intended for ordering (compareTo) make more sense to use when looking at equality than a method (equals) that simply handles equality?

  9. #9
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default

    Java Code:
    It may work, but how does using a method intended for ordering (compareTo) make more sense to use when looking at equality than a method (equals) that simply handles equality?
    There is not.
    I thought that that was the question.
    I have tested the code and the code is working fine.
    So JosAh is correct, where is the while statement because the rest is working.

    Greetz

Similar Threads

  1. kind of Component being return? and Conversion
    By mine0926 in forum NetBeans
    Replies: 11
    Last Post: 05-17-2010, 10:11 AM
  2. ';' expected, not of the common kind..
    By Addez in forum New To Java
    Replies: 6
    Last Post: 09-02-2009, 04:37 AM
  3. how client know what kind of server
    By lemur in forum Networking
    Replies: 3
    Last Post: 05-31-2008, 07:11 AM
  4. Total Newbie, Be Kind :)
    By dazza-s in forum New To Java
    Replies: 11
    Last Post: 04-26-2008, 10:54 PM
  5. Will make a pyramid of some kind out of a number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:20 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
  •