Why won't my Java program accept alphabetic input from the user?

Edit: my problem is partially fixed. Now I can enter text, however nothing reads after entering the value "shiphalf". Am I phrasing my if else statements?


I'm trying to let people enter a coupon code, but I can't figure out how to tell the console that the user is entering text. I feel like the error is here somewhere here:

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

      

But I'm not really sure. Below is the entire source 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.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");
        }


    }

}

      

+3


source to share


3 answers


You have 2 problems in your code:
1. Use input.next()

instead input.nextLine()

.
2. Change if conditional

to ship.equals("shiphalf")

. Remember you are using .equals()

2 strings to compare.

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");
        }
    }
}

      

+6


source


This is where the following happens: 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. It doesn't enter a new line. Looking at the Java documentation , we can see that the nextLine () method is partially described as follows:



Adapts this scanner to the current line and returns the missing input. This method returns the rest of the current line, excluding any line separator at the end

So, if your scanner is still on the line and you call nextLine (), it just returns the rest of the line. In your case, it will most likely be an empty String, even if you want to read the coupon code. After that, your scanner points to the actual line in the console that you wanted to read.

So what's the easiest way to get around this? You can simply call the nextLine () method one more time before getting the coupon code input. This will set the scanner to the beginning of the current line you want to read.

+2


source


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

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

      

Hope this helps?

+1


source







All Articles