Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By Solarsonic

Thread: Comparing a String to a string

  1. #1
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Comparing a String to a string

    Hello,
    My question/issue is how to compare a string to a string from a getmethod, which is initially pulling its info from a fileparser before being called on by another class.

    For example:

    public class Order extends OrderItem {

    public String itemAnswer;

    ***
    //constructor here
    ***
    public String addOrderItem()
    {
    if(itemAnswer.equals(getName()))
    {
    return this.getName();
    }
    }

    }

    in another class, getName is defined as:
    public String getName() {
    return name;
    }

    When I see this it just looks wrong, but not sure what to do. itemAnswer should match an itemName that the getName method I was thinking would return. Can anyone help or am I not being clear?

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Comparing a String to a string

    When you're comparing the values of Strings you use the .equals() method.

  3. #3
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Comparing a String to a string

    So where I used it in the if statement isn't correct?

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Comparing a String to a string

    Quote Originally Posted by clj89 View Post
    So where I used it in the if statement isn't correct?
    It looks right to me. What exactly is wrong? Did you get any errors?

  5. #5
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Comparing a String to a string

    I get an error saying addOrderItem: This method must return a result of type String.
    I tried adding toString to getName but that didnt help

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

    Default Re: Comparing a String to a string

    Your problem appears that you are returning a result from within an if statement. What happens if the if condition is false? What is returned then? You need to have a default return statement, perhaps return null, or throw an error, but something needs to occur that makes sense.

  7. #7
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Comparing a String to a string

    I have the else going to a different method.
    else
    {
    this.orderTotal();
    }

  8. #8
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Comparing a String to a string

    Quote Originally Posted by clj89 View Post
    I have the else going to a different method.
    else
    {
    this.orderTotal();
    }
    When you have a method you need to return something no matter the condition.

    So for your else condition you need to return a String as well.
    Last edited by Solarsonic; 10-02-2011 at 05:31 AM.

  9. #9
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Comparing a String to a string

    okay, I can fix that. But now I need to back track a little.
    Now, after I return:
    this.getName();

    I want to use that in another if statement. But when I try to, I get an unreachable code error. This is what I've got:

    if(this.getName().equals("smallPizza")|this.getNam e().equals("Small Pizza"))
    {
    return this.addLeftToppings();
    return this.addRightToppings();
    }

    Should I use itemAnswer for this if statement instead? Also, this may not be the best way to do what I'm trying to do, so I apologize if it seems messed up to you.

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

    Default Re: Comparing a String to a string

    That statement doesn't make sense. How can you have two return statements in a row? As soon as one is reached, it returns what it's supposed to return and the method ends. As the compiler states, the next statement is never reached. I think you need to think through your logic on paper before trying to type in code.

  11. #11
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Comparing a String to a string

    thanks, I've been trying and everything I come up with in writing doesn't work so trying everything I can

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

    Default Re: Comparing a String to a string

    Quote Originally Posted by clj89 View Post
    thanks, I've been trying and everything I come up with in writing doesn't work so trying everything I can
    The heuristic of throwing everything at the wall and seeing what sticks isn't a good one for programming. Better to think the issue through before writing, creating a meaningful algorithm, and then coding it.

  13. #13
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Comparing a String to a string

    Quote Originally Posted by Fubarable View Post
    The heuristic of throwing everything at the wall and seeing what sticks isn't a good one for programming. Better to think the issue through before writing, creating a meaningful algorithm, and then coding it.
    I'm def going to redo that now. Quick question though. When I first thought my part of this project out, I figured if else statements would work, since I would need the users input and their input determines everything. Its a pizza delivery system and whatever the user enters really runs the whole thing. Would that not work the best or would you use a different approach?

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

    Default Re: Comparing a String to a string

    If I were doing something like this, I'd create model type classes (the Pizza class, Toppings class, Customer class, Order class), and then a separate user interface class or classes. I've seen the decorator design pattern used for this, but this may be overkill for you.

  15. #15
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Comparing a String to a string

    Quote Originally Posted by clj89 View Post
    I'm def going to redo that now. Quick question though. When I first thought my part of this project out, I figured if else statements would work, since I would need the users input and their input determines everything. Its a pizza delivery system and whatever the user enters really runs the whole thing. Would that not work the best or would you use a different approach?
    You could also use switch statements like so:

    Java Code:
    String food = "pizza";
    
    switch (food) {
    
    case "buffalo wings":
    System.out.println("How many pieces of buffalo wings do you want?");
    break;
    
    case "pizza":
    System.out.println("What size pie do you want?");
    break;
    
    }
    EDIT: If you've learned about objects then Fubarable's way would be the best.

  16. #16
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Comparing a String to a string

    my group decided to use a file parser to hold all the information;pizza names, the prices, the toppings, and 3 other pieces of info. Then there was a menuitem class that was made that received the info from the file parser. Now from that menuitem class, I'm working on the orderitem class which extends menuitem and order class which extends orderitem. And the if else statements I mentioned I figured I could use in the order class in the additem method.
    But things you guys mentioned, I def thought about but its a little to late for me try the switch or separate classes for the pizza, toppings, etc.

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

    Default Re: Comparing a String to a string

    Quote Originally Posted by Solarsonic View Post
    You could also use switch statements like so:

    Java Code:
    String food = "pizza";
    
    switch (food) {
    
    case "buffalo wings":
    System.out.println("How many pieces of buffalo wings do you want?");
    break;
    
    case "pizza":
    System.out.println("What size pie do you want?");
    break;
    
    }
    EDIT: If you've learned about objects then Fubarable's way would be the best.
    Is Switch with Strings now allowed in Java 7?

  18. #18
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Comparing a String to a string

    Quote Originally Posted by Fubarable View Post
    Is Switch with Strings now allowed in Java 7?
    Yeah it's a new feature of Java 7.
    Fubarable likes this.

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2014, 09:06 AM
  2. Replies: 0
    Last Post: 04-17-2011, 05:57 PM
  3. Replies: 1
    Last Post: 10-21-2010, 08:06 PM
  4. Comparing JComboBox with string?
    By vahshir in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 08:39 AM
  5. Comparing contents of a file with String[]
    By eponcedeleon in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2010, 10:10 PM

Posting Permissions

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