Been messing with Java for the last couple of hours...
Here's what I have:
which outputs correctly like this:
If I reverse the order of these lines
I get this error:
telling me this line is in correct:
And I can't figure out why this is happening... If it means anything, it happens in both Eclipse and in NetBeans both on Windows and Elementary OS (Ubuntu spinoff, Eclipse only)
Any help would be much appreciated
Here's what I have:
Code:
import java.util.Scanner;
import static java.lang.System.in;
import static java.lang.System.out;
public class TicketPriceWithDiscountBooleanOperators {
public static void main(String args[]) {
Scanner Keyboard = new Scanner(in);
int age; double price = 0.00; char reply;
boolean isKid, isSenior, hasCoupon, hasNoCoupon;
out.print("Have a coupon? (Y/N) ");
reply = Keyboard.findInLine(".").charAt(0);
out.print("How old are you? "); age = Keyboard.nextInt();
isKid = age < 12; isSenior = age >= 65;
hasCoupon = reply == 'Y' || reply == 'y';
hasNoCoupon = reply == 'N' || reply == 'n';
if (!isKid && !isSenior) {
price = 9.25;
}
if (isKid || isSenior) {
price = 5.25;
}
if (hasCoupon) {
price -= 2.00;
}
if (!hasCoupon && !hasNoCoupon) {
out.println("Huh?");
}
out.println("Please pay $" + price + ". Enjoy the show!");
}
}
Code:
run:
Have a coupon? (Y/N) Y
How old are you? 13
Please pay $7.25. Enjoy the show!
BUILD SUCCESSFUL (total time: 4 seconds)
If I reverse the order of these lines
Code:
out.print("Have a coupon? (Y/N) ");
reply = Keyboard.findInLine(".").charAt(0);
Code:
out.print("How old are you? "); age = Keyboard.nextInt();
I get this error:
Code:
How old are you? 13
Have a coupon? (Y/N) Exception in thread "main" java.lang.NullPointerException
at TicketPriceWithDiscountBooleanOperators.main(TicketPriceWithDiscountBooleanOperators.java:13)
telling me this line is in correct:
Code:
reply = Keyboard.findInLine(".").charAt(0);
And I can't figure out why this is happening... If it means anything, it happens in both Eclipse and in NetBeans both on Windows and Elementary OS (Ubuntu spinoff, Eclipse only)
Any help would be much appreciated