0

I am writing a Java program to decide how much a user has to pay for a delivery charge for a school project. I have been advised the method that works out the cost has to be static. When i de-bug my code the value of the price is never changing and i don't know why, please see my code below.

main method

package questionOne;

import java.util.Scanner;
import questionOne.DeliveryMenu;

public class DeliveryMenuDemo {

public static void main(String[] args)
    throws java.io.IOException {

    String choice;

    DeliveryMenu d = new DeliveryMenu();

    System.out.println("Enter the type of Delivery service needed (Standard/Super/WowWee)?");

    Scanner scanner = new Scanner(System.in);
    choice = scanner.next();
    d.setDeliveryCost(choice);
    d.getPrice();


}

}

and class with other methods

package questionOne;

public class DeliveryMenu {

private static String delivery;
private static int price=0;

public static int setDeliveryCost(String choice) {
    if(delivery == "Standard") {
        price = price + 22;
    } else if (delivery == "Super") {
        price = price + 44;
    } else if (delivery == "wow-wee") {
        price = price + 60;
    }   
    return price;
}

public String setDelivery(String s) {
    return delivery=s;
}

public void getPrice() {
    System.out.println(price);
}

}

price is never changing to the desired amount.

Stephen Worrall
  • 47
  • 2
  • 10
  • https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java you're not comparing strings properly – Alex Apr 10 '17 at 18:47
  • Do you ever call the setDelivery function? Even if you change what other people are commenting about comparing strings properly, you face another problem. You are never initializing the delivery variable in the DeliveryMenu class, therefor you will always be comparing an empty string. – Jack Gore Apr 10 '17 at 18:54
  • setDelivery was not needed anyway, that was hangover from a previous attempt that snuck in. – Stephen Worrall Apr 10 '17 at 19:37

1 Answers1

-1

For comparing Strings you need to use String method equals.

(delivery.equals("Standard")) or (delivery.equalsIgnoreCase("Standard")) instead of (delivery == "Standard")

Omore
  • 614
  • 6
  • 18