Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java (for beginners) problem

  • 17-02-2009 04:15PM
    #1
    Closed Accounts Posts: 1,709 ✭✭✭


    Getting the following error
    "Exception in thread "main" java lang.NullPointerException at TicketPriceWithDiscount.main(TicketPriceWithDiscount.java:17)

    after the second question re the coupon.
    Heres the code:


    import java.util.Scanner;

    class TicketPriceWithDiscount {
    public static void main(String args[]) {
    Scanner myScanner = new Scanner(System.in);
    int age;
    double price = 0.00 ;
    char reply;



    System.out.print("What is your age?: ");
    age = myScanner.nextInt();


    System.out.print("Have you 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!");

    }
    }


    Anybody help ?


Comments

  • Registered Users, Registered Users 2 Posts: 3,548 ✭✭✭Draupnir


    Your line doesnt contain '.' and therefore, you cannot do charAt(0) on nothing. i.e. Null pointer exception.


  • Closed Accounts Posts: 1,709 ✭✭✭BolBill


    Draupnir wrote: »
    Your line doesnt contain '.' and therefore, you cannot do charAt(0) on nothing. i.e. Null pointer exception.

    Ok thanks. So what do I do ?


  • Closed Accounts Posts: 1,015 ✭✭✭Epic Tissue


    System.out.println("Have you got a coupon?: ");
    reply = scanner.nextLine();

    Does that work?


  • Closed Accounts Posts: 1,709 ✭✭✭BolBill


    No, it looks for a char


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    Strings are enclosed in "S"
    Chars are 'C'.
    I think....
    It's been a while but I think this happened to me....


  • Advertisement
  • Closed Accounts Posts: 1,015 ✭✭✭Epic Tissue


    BolBill wrote: »
    No, it looks for a char

    you can get a char from a string with charAt like you did.

    so..

    System.out.print("Have you got a coupon?: Y/N");
    reply = myScanner.nextLine().charAt(0);

    edit:

    seems to only work when you ask for Y/N before you ask for age. I don't know why this is... can anyone explain that?


  • Registered Users, Registered Users 2 Posts: 1,119 ✭✭✭Donald-Duck


    Been awhile since I used scanner but I assume its because nextInt hasn't terminated the line, so the "nextLine" is more than likely an empty string.

    You can just use ".next()"(Doesn't care about newlines ) to get the next string, and .charAt(0) to get the Y/N part of it


  • Registered Users, Registered Users 2 Posts: 887 ✭✭✭eoinbn


    Your code isn't very robost. For example you will get an error if you put in a letter instead of a number for the age question.

    static Scanner myScanner = new Scanner(System.in);

    public static boolean question() {
    while (true) {
    String answer;
    System.out.print("Have you a coupon? (Y/N) ");
    answer = myScanner.next();
    if (answer.equalsIgnoreCase("Y"))
    return true;
    else if (answer.equalsIgnoreCase("N"))
    return false;
    }

    }

    I would use something like that. It just loops until it gets a Y or N(and ignores caps).


  • Closed Accounts Posts: 1,709 ✭✭✭BolBill


    I used myScanner.next().charAt(0); and it works.

    Thanks for all your help folks, much appreciated.


Advertisement