Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    11
    ReputationRep:
    Hi, can you tell me what's wrong with my program? The problem seems to lay in the if statement.


    package tutorial14;
    import java.util.Scanner;
    class Robot {
    public String name;


    public void speak(String text) {
    System.out.println(name + ": " + text);
    }
    public void move(String direction, double distance) {
    System.out.println(name + " is moving " + distance + "metres in direction: " + direction);

    }
    }


    public class Application {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("What's your robot's name?");
    String robotName = input.nextLine();
    Robot userRobot = new Robot();
    userRobot.name = robotName;
    System.out.println(robotName + " wants to greet you!");
    String greeting = "It's so nice to meet you, creator!";
    userRobot.speak(greeting);
    System.out.println("Will you answer him?");
    String userReply = input.nextLine();
    System.out.println(userReply.len gth());
    if (userReply == "yes") {
    System.out.println("That's great!");
    System.out.println("What do you want to say?");
    String userGreeting = input.nextLine();
    System.out.println("You: " + userGreeting);
    userRobot.speak("So glad you love me!");
    } else {
    System.out.println("You're a meanie! Your robot's gone...");
    }
    }
    }




    This is my output:
    What's your robot's name?
    Joe
    Joe wants to greet you!
    Joe: It's so nice to meet you, creator!
    Will you answer him?
    yes
    3
    You're a meanie! Your robot's gone...


    While after saying yes the program should have executed the commands in the brackets after If because the condition was met?
    • Thread Starter
    Offline

    11
    ReputationRep:
    Ok, should have waited, someone else told me it should be " if(userReply.equals("yes"))"...
    Can some explain what the difference is? :/
    Offline

    18
    ReputationRep:
    You cannot perform a direct comparison with a String object and char literals (at least I don't think you can - it would explain the problem and the solution you've been given). To perform this comparison you should use the equals method of the String object you have created that will return a nice Boolean result for your if-block to work with.
    Offline

    21
    ReputationRep:
    If your input only needs to take one-word answers use input.next() instead of input.nextLine()
    Offline

    15
    ReputationRep:
    (Original post by Viridiana)
    Ok, should have waited, someone else told me it should be " if(userReply.equals("yes"))"...
    Can some explain what the difference is? :/
    You can only use == to test equality for primitive types. The primitive types are the ones which start with a lower case letter, including but not limited to: int, char, long

    String is a reference type. The simple answer is that == doesn't do what you think it does on reference types, so you should use .equals (if the type has an equals method, which String does)
    The slightly longer explaination is that String (and all other Object) variables are just references to a piece of memory somewhere where the data can be found. So if I do:
    Code:
    String a = "abc";
    String b = "abc";
    a == b;
    The == statement does not attempt to determine whether a and b contain the same human-readable value, but simply whether a and b point to the same physical location of memory. In general, they don't
    • Thread Starter
    Offline

    11
    ReputationRep:
    Thanks everyone!

    I have another doubt, I'd be glad if someone could help me because my friend who told me about the .equals method doesn't know how to help me this time.

    This is a part of my program:



    public void placeOrder() {

    Scanner input = new Scanner(System.in);
    System.out.println("Do you want to place an order?");
    String clientResponse = input.nextLine();
    if (clientResponse.equals("yes")) {
    System.out.println("You have " + this.Wallet + " in your wallet.");
    System.out.println("Which product do you want to buy?");
    String clientResponse2 = input.nextLine();
    System.out.println("How many?");
    int howMany = input.nextInt();
    if (clientResponse2.equals("apple") ) {
    this.Wallet = this.Wallet - (apple.price * howMany);
    this.numbOfPurchases++;
    System.out.println("Thank you for your purchase of the " + apple.name + " !");
    System.out.println("You've purchased " + howMany + " items.");
    System.out.println("You've already used our store " + this.numbOfPurchases + " times.");
    System.out.println("Please come back!");
    } else if (clientResponse2.equals("banana" )) {
    this.Wallet = this.Wallet - banana.price * howMany;
    this.numbOfPurchases++;
    System.out.println("Thank you for your purchase of the " + banana.name + " !");
    System.out.println("You've purchased " + howMany + " items.");
    System.out.println("You've already used our store " + this.numbOfPurchases + " times.");
    System.out.println("Please come back!");


    } else {
    System.out
    .println("Sorry, it seems we don't sell this product.");
    }
    } else {
    System.out.println("Hope you buy something next time!");
    }
    }


    Something's wrong in the bold lines.

    This method is inside this class:
    class Client {
    String name;
    int numbOfPurchases;
    double Wallet;
    int accountID;
    private Product apple;
    private Product banana;
    (...)
    }

    I've tried to make objects banana and apple of class Product accessible for the method in class Client. Did I do it wrong?
    Offline

    21
    ReputationRep:
    Code:
    price has private access in Product
    Is this the error you're getting? If so, add a "getter" method to Product
    • Thread Starter
    Offline

    11
    ReputationRep:
    (Original post by shawn_o1)
    Code:
    price in class Product has private access
    Is this the error you're getting? If so, add a "getter" method to Product
    Do you want to place an order?
    yes
    You have 19.3 in your wallet.
    Which product do you want to buy?
    banana
    How many?
    5
    Exception in thread "main" java.lang.NullPointerException
    at Client.placeOrder(Application.ja va:42)
    at Application.main(Application.jav a:89)


    This is the output I get.
    Offline

    21
    ReputationRep:
    (Original post by Viridiana)
    Do you want to place an order?
    yes
    You have 19.3 in your wallet.
    Which product do you want to buy?
    banana
    How many?
    5
    Exception in thread "main" java.lang.NullPointerException
    at Client.placeOrder(Application.ja va:42)
    at Application.main(Application.jav a:89)


    This is the output I get.
    You haven't instantiated a Product, that's why. Make sure the Product apple refers to an instance. e.g.
    Code:
    new Product("apple").price - howMany;
    depending on you how defined the constructor for Product.
    • Thread Starter
    Offline

    11
    ReputationRep:
    (Original post by shawn_o1)
    You haven't instantiated a Product, that's why. Make sure the Product apple refers to an instance. e.g.
    Code:
    new Product("apple").price - howMany;
    depending on you how defined the constructor for Product.

    Your version gives me an error :/

    This is how my products are defined but it's in the main class. Do I define those things somewhere else?
    Thanks for all your help

    Product banana = new Product();
    banana.name = "Californian Banana";
    banana.price = 1.20;
    banana.quantity = 29;
    banana.ID = 1111;
    banana.countValue(banana.quantit y, banana.price);


    Product apple = new Product();
    apple.name = "Polish Apple";
    apple.price = 0.80;
    apple.quantity = 190;
    apple.ID = 2222;
    apple.countValue(apple.quantity, apple.price);
    Offline

    21
    ReputationRep:
    Ah yes, you don't have an instance of apple or banana within Client. Also, it appears all the fields of Product are public - make them private, then define a constructor for Product - something like:
    Code:
    public Product(String input)
    {
      if (input.equals("apple"))
      { // set private fields }
      else
      { // do something else } 
    }
    Then the code in Client can have this statement
    Code:
    this.Wallet = this.Wallet - (new Product("apple").getPrice() * howMany);
    where getPrice() is a "getter method" that retrieves the price of the Product.
    • Thread Starter
    Offline

    11
    ReputationRep:
    (Original post by shawn_o1)
    Ah yes, you don't have an instance of apple or banana within Client. Also, it appears all the fields of Product are public - make them private, then define a constructor for Product - something like:
    Code:
    public Product(String input)
    {
      if (input.equals("apple"))
      { // set private fields }
      else
      { // do something else } 
    }
    Then the code in Client can have this statement
    Code:
    this.Wallet = this.Wallet - (new Product("apple").getPrice() * howMany);
    where getPrice() is a "getter method" that retrieves the price of the Product.

    Thanks! It seems I haven't studied all these concept in depth enough yet and I don't get it all, but you've helped me a lot either way.

    What's your background in Java? You sound like you know a lot.
    Offline

    21
    ReputationRep:
    (Original post by Viridiana)
    Thanks! It seems I haven't studied all these concept in depth enough yet and I don't get it all, but you've helped me a lot either way.

    What's your background in Java? You sound like you know a lot.
    I've programmed for 2 and a half years You're welcome
 
 
 
Poll
Do you agree with the PM's proposal to cut tuition fees for some courses?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.