0

I currently have the following code wrote:

import java.util.Scanner;

public class TicketPriceWithDiscount {

public static void main(String[] args) {

    TicketPriceWithDiscount obj = new TicketPriceWithDiscount();

}

  public TicketPriceWithDiscount(){

    double regPrice = 9.25;
    double otherPrice = 5.25; // ages under 12 or over 65
    double discount = 2.00;
    String person;
    int pInput;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("What is your age?");

    pInput = keyboard.nextInt();

    System.out.println("Do you have a coupon? Yes or no will do.");

    person = keyboard.nextLine();

    if(person == "yes" && pInput <= 12 || pInput >= 65){
        otherPrice = otherPrice - discount;
        System.out.println("Your ticket price is: " + otherPrice);

    } 

    else if(person == "yes" && pInput >= 12 && pInput <= 65){
        regPrice = regPrice - discount;
        System.out.println("Your ticket price is: " + regPrice);
    } 

    else if(pInput <= 12 || pInput >= 65){
        System.out.println("Your ticket price is: " + otherPrice);
    } 

    else if(pInput >= 12 || pInput <= 65) {
        System.out.println("Your ticket price is: " + regPrice);
    }

}

}

The program runs up to the point in which I ask for the person to input wither or not they have a coupon.

System.out.println("Do you have a coupon? Yes or no will do.");

    person = keyboard.nextLine();

At this point, the program does not wait for an input and completes the if statement based on age input which it does wait for a reply from the user.

The output intended is for the user to input age, say yes or no to the coupon question and for the program to continue its run depending on those answers.

What I have tried:

  1. closing the scanner - keyboard.close();
  2. create a new scanner after closing the first instance of scanner.

Guidance is highly appreciated.

Suraj Rao
  • 28,186
  • 10
  • 88
  • 94
James
  • 11
  • 7

3 Answers3

1

I think your nextLine reads the new line feed after your

keyboard.nextInt();

Try putting a

keyboard.nextLine();

just after

keyboard.nextInt();

to reset the Scanner.

Koray Tugay
  • 20,438
  • 37
  • 155
  • 276
  • I have just done so and no success. The program still runs through, only accounting for the first user int value. – James Apr 01 '17 at 13:22
0

You seem to be having the same problem as in this question with solution. Certainly an odd quirk!

Community
  • 1
  • 1
hughjdavey
  • 882
  • 2
  • 10
  • 13
0

change to :

import java.util.Scanner;

public class TicketPriceWithDiscount {

public static void main(String[] args) {

    TicketPriceWithDiscount obj = new TicketPriceWithDiscount();

}

  public TicketPriceWithDiscount(){

    double regPrice = 9.25;
    double otherPrice = 5.25; // ages under 12 or over 65
    double discount = 2.00;
    String person;
    int pInput;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("What is your age?");
    pInput = keyboard.nextInt();

    System.out.println("Do you have a coupon? Yes or no will do.");
    //change to next() not nextLine()
    //because nextLine is used when you read a file
    person = keyboard.next();

     //use equals  not ==  and don't forget the parentheses
    if(person.equals("yes") && (pInput <= 12 || pInput >= 65)){
        otherPrice = otherPrice - discount;
        System.out.println("rani hna");
        System.out.println("Your ticket price is: " + otherPrice);

    } 

    //use equal not ==
    else if(person.equals("yes")  && pInput >= 12 && pInput <= 65){
        regPrice = regPrice - discount;
        System.out.println("Your ticket price is: " + regPrice);
    } 

    else if(pInput <= 12 || pInput >= 65){
        System.out.println("Your ticket price is: " + otherPrice);
    } 

    else if(pInput >= 12 || pInput <= 65) {
        System.out.println("Your ticket price is: " + regPrice);
    }

}

}
Charif DZ
  • 13,500
  • 3
  • 13
  • 36
  • you forget the parentheses too on the first condition – Charif DZ Apr 01 '17 at 13:27
  • to use, keyboard.next() and not keyboard.nextLine(). I have tried this and it works perfectly. From another answer, which referred me to another similar question, i now understand that the nextLine was actually reading the "enter key" from the previous answer. I had not seen my == problem and knew to use .equals when comparing strings. Rushed mistake. Thank you very much. Off to read on the difference between .next and .nextLine. – James Apr 01 '17 at 13:32
  • I would upvote if I had that privilege but i don't. So, I'm sorry but all I can give is a "Thank You". – James Apr 01 '17 at 13:41
  • Well, it's recorded but not publicly. – James Apr 01 '17 at 13:42
  • LoL Thank you too – Charif DZ Apr 01 '17 at 13:49