mercredi 14 septembre 2016

java.util.Scanner is blocking me in an if statement

I do think the problem I am having is that Scanner is blocking me in an if statement but I am not sure.

What I am trying to achieve is a program that will allow me to associate a price with a name, although that's not really relevant here. Here is the code:

import java.util.Scanner;
public class Program03 {
    public static void main(String[] args) {

        Scanner stdIn = new Scanner(System.in);



        //Candies

        String redHots = ("red-hots");
        String mikeNikes = ("mikenikes");
        String name;
        String name2;

        //Prices

        double rH;
        double mNI;
        double enterP;

        //Other

        String dollar = ("$");


        //Actual Output

        System.out.println("Welcome to the store");
        System.out.println("---------------------------------");

        System.out.println('\t');
        System.out.print("Please enter the name of the first candy: ");
        name = stdIn.nextLine();



        if(name.equalsIgnoreCase ("red-hots") || name.equalsIgnoreCase ("redhots")){

            System.out.println("Please enter price (per unit) of Red-Hots:");

        }


        enterP = stdIn.nextDouble();

        if(enterP >= 0){

            System.out.println("Please enter the name of the second Candy:");

    }


        if(name.equalsIgnoreCase("mike-n-ikes") || name.equalsIgnoreCase("mikesnikes") || name.equalsIgnoreCase("mikes-nikes") || name.equalsIgnoreCase("mikesn-ikes")){

            System.out.println("Enter the number of Mikes-N-Ikes to purchase:");

    }

        else if(enterP <= 0){
            System.out.println("Error: Please re-enter input");

        }
        stdIn.close();

    }
}

The output is kinda weird here:

Works fine

Works fine

Stuck!

I tried to close the scanner with Scanner.close(); at the end, I tried to close just the variable of the scanner with stdIn.close but nothing worked, I pretty sure I making a stupid mistake.

Please help, and please don't down vote this noob!

Aucun commentaire:

Enregistrer un commentaire