Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java (for beginners) problem

  • 17-02-2009 3: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: 843 ✭✭✭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