3

Edit: my problem has been partially fixed. Now I am able to enter text, however nothing reads after I enter the 'shiphalf' value. Did I construct my if else statements incorrectly?


I am attempting to let people input a coupon code but I am unable to figure out how to let the console know that the user is inputting text. I feel that the error is somewhere in here:

        System.out.println("Enter a Coupon Code: ");
        String ship = input.nextLine(); 

But I am not entirely sure. Here is the whole source code below:

package pseudoPackage;

import java.util.Scanner;

public class PseudoCode {

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);


        int ship1 = 0;
        int ship2 = 6;
        int ship3 = 2;
        String shiphalf = null;

        System.out.print("How much will shipping cost you?");
        System.out.println();
        System.out.print("Enter your textbook cost in dollars without the $: ");
        double cost = input.nextDouble();



        if ( cost >= 100 ) {
            System.out.println("Your shipping costs are $0");
        } else if ( cost < 100 && cost > 50 ) {
            System.out.println("Your shipping cost is $6");
        } else if ( cost < 50 ) {
            System.out.println("Your shipping cost is $2");
        }

        System.out.println("Enter a Coupon Code: ");
        String ship = input.nextLine(); 

        if ( ship == shiphalf && cost >= 100 ) {
            System.out.println("Your shipping costs are $0");
        } else if ( ship == shiphalf && cost < 100 && cost >50 ) {
            System.out.println("Your shipping costs are $3 ");
        } else if ( ship == shiphalf && cost < 50 ) {
            System.out.println("Your shipping costs are $1");
        }


    }

}
nbro
  • 12,226
  • 19
  • 85
  • 163
Nicholas Dry
  • 61
  • 1
  • 8
  • 1
    don't compare Strings via `==`. This operator compares object reference, not the string value. Use `equals()` method of String class. Like 'if (ship.equals(shiphalf)) { do something here }' –  May 30 '15 at 22:09
  • possible duplicate of [Skipping nextLine() after use next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-next-nextint-or-other-nextfoo-methods) – khelwood May 30 '15 at 22:11
  • See [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jesper May 30 '15 at 22:18
  • You should watch out for `` issues. If you type in `50` you won't get any reduction – Roman Vottner May 30 '15 at 22:28

3 Answers3

6

You have 2 problems in your code:
1. Use input.next() instead of input.nextLine().
2. Change your if conditional to ship.equals("shiphalf"). Remember that you use .equals() to compare 2 strings.

Here is your final code:

package pseudoPackage;
import java.util.Scanner;

public class PseudoCode {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int ship1 = 0;
        int ship2 = 6;
        int ship3 = 2;
        String shiphalf = null;

        System.out.print("How much will shipping cost you?");
        System.out.print("\nEnter your textbook cost in dollars without the $: ");
        double cost = input.nextDouble();

        if ( cost >= 100 ) {
            System.out.println("Your shipping costs are $0");
        } else if ( cost < 100 && cost > 50 ) {
            System.out.println("Your shipping cost is $6");
        } else if ( cost < 50 ) {
            System.out.println("Your shipping cost is $2");
        }

        System.out.println("Enter a Coupon Code: ");
        String ship = input.next(); 

        if ( ship.equals("shiphalf") && cost >= 100 ) {
            System.out.println("Your shipping costs are $0");
        } else if ( ship.equals("shiphalf") && cost < 100 && cost >50 ) {
            System.out.println("Your shipping costs are $3 ");
        } else if ( ship.equals("shiphalf") && cost < 50 ) {
            System.out.println("Your shipping costs are $1");
        }
    }
}
Henry Zhu
  • 1,826
  • 5
  • 30
  • 69
2

What happens here is the following: when you call

double cost = input.nextDouble();

Your Scanner reads the next Double in the Console and then "stops" at the end of that double value. It does not enter a new Line. Looking at the Java Documentation, we can see that the Method nextLine() is partly described the following way:

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end

So if your Scanner still is in a line, and you call nextLine() it will simply return the rest of the line. In your case this would most likely just be an empty String, even though you wish to read the Coupon Code. After that your Scanner points to the actual line in the console you wanted to read.

So what's the easiest way to work around this ? You can just invoke the Method nextLine() once more before you get the Input for the Coupon Code. This will set your Scanner to the beginning of the actual line you wish to read.

Benjamin
  • 99
  • 8
  • Thanks for the detail! – Nicholas Dry May 30 '15 at 22:24
  • Also, in regards to comparing Strings with .equals() and not ==, this might prove to be useful. http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java Remeber that Strings are Objects and not primitive datatypes like ints or doubles. This makes their behaviour a little different – Benjamin May 30 '15 at 22:28
1

Another way to get text from a user is to use JOptionPane. Also if you are comparing text, you want to code ' ship.equals(shiphalf) '

    String x = JOptionPane.showInputDialog(null, "Type something!");
    if(ship.equals(x)){
            System.out.println("Your code works"); 
    }

Hope this kind of helps?

Skaleb
  • 226
  • 1
  • 8