T
the_transcriber
Hello again. This time i have another code that isn't working. It is
like a ticket program... It should ask the user "how old are you?",
following that answer it should say "Have a coupon? (Y/N)"
and then it will give the ticket price based on the answers. When i
run it, here is the output that i recieve...
How old are you? 51
Have a coupon? (Y/N) Exception in thread "main"
java.lang.NullPointerException
at Discount.main(Discount.java:15)
Process completed.
____________________________________________________________________________
So here is the code... any ideas? Im completely new to this, and this
program should work without using more methods that i haven't learned
yet, it should work the way it is i thought! So if anyone can see what
i did wrong with it using the stuff i have already used, let me know,
thanks! This is from the Beginning Programming with Java for Dummies -
2nd Edition book
import java.util.Scanner;
class Discount {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int age;
double price = 0.00;
char reply;
System.out.print("How old are you? ");
age = myScanner.nextInt();
System.out.print("Have a coupon? (Y/N) ");
reply = myScanner.findInLine(".").charAt(0);
if (age >= 12 && age < 65) {
price = 9.25;
}
if (age < 12 || age >= 65) {
price = 5.25;
}
if (reply == 'Y' || reply == 'y') {
price -= 2.00;
}
if (reply != 'Y' && reply != 'y' &&
reply!='N' && reply!='n') {
System.out.println("Huh?");
}
System.out.print("Please pay $");
System.out.print(price);
System.out.print(". ");
System.out.println("Enjoy the show!");
}
}
like a ticket program... It should ask the user "how old are you?",
following that answer it should say "Have a coupon? (Y/N)"
and then it will give the ticket price based on the answers. When i
run it, here is the output that i recieve...
How old are you? 51
Have a coupon? (Y/N) Exception in thread "main"
java.lang.NullPointerException
at Discount.main(Discount.java:15)
Process completed.
____________________________________________________________________________
So here is the code... any ideas? Im completely new to this, and this
program should work without using more methods that i haven't learned
yet, it should work the way it is i thought! So if anyone can see what
i did wrong with it using the stuff i have already used, let me know,
thanks! This is from the Beginning Programming with Java for Dummies -
2nd Edition book
import java.util.Scanner;
class Discount {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int age;
double price = 0.00;
char reply;
System.out.print("How old are you? ");
age = myScanner.nextInt();
System.out.print("Have a coupon? (Y/N) ");
reply = myScanner.findInLine(".").charAt(0);
if (age >= 12 && age < 65) {
price = 9.25;
}
if (age < 12 || age >= 65) {
price = 5.25;
}
if (reply == 'Y' || reply == 'y') {
price -= 2.00;
}
if (reply != 'Y' && reply != 'y' &&
reply!='N' && reply!='n') {
System.out.println("Huh?");
}
System.out.print("Please pay $");
System.out.print(price);
System.out.print(". ");
System.out.println("Enjoy the show!");
}
}