Results 1 to 9 of 9
  1. #1
    Iliyas is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Question Simple code. Could you help pls..?

    Hi, Could you pls. look at the code. As I run the code the message:

    "Do you have a coupon? Exception in thread "main" java.lang.NullPointerException
    at TicketPricewithDiscount.main(TicketPricewithDiscou nt.java:15)"

    is showing off :confused: . I have copied the code from the book and there it works fine(at least the author writes it works:) . Would appreciate your help. Thanks. (See the full code below).

    import java.util.Scanner;

    public 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("How old are you?");
    age = myScanner.nextInt();

    System.out.print("Do you have a coupon? ");
    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 != 'N' && reply!='n'&& reply!= 'Y' && reply!='y'){
    System.out.print ("Huh?");
    }

    System.out.print ("Please pay $ ");
    System.out.print(price);
    System.out.print(". ");
    System.out.println("Enjoyr the show!");
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You should probably call myScanner.nextLine(); immediately after getting the age via myScanner.nextInt(). The reason for this is that nextInt doesn't process the end of line character, and so without nextLine() the scanner object will be immediately called after the nextInt() call and your NPE will be thrown.

    For example:
    Java Code:
        System.out.print("How old are you?");
        age = myScanner.nextInt();
        myScanner.nextLine();  // add this to swallow the end of line character
    Question, why the call to findInLine(".")? What is the purpose of using this and not nextLine()?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What you are going to do here?

    Java Code:
    reply = myScanner.findInLine(".").charAt(0);

  4. #4
    Iliyas is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Smile Thanks

    Amazing. Thanks dude, it worked. the purpose of findInLine(".") call was to get a single character which had to be "Y" or "N" or "y" "n".

    "System.out.print("Do you have a coupon? Y/N");
    reply = myScanner.findInLine(".").charAt(0);"

    Option between Y or N inputs into compiler.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, did you read more about findInLine() method on the Java doc? You have a patter like ".", and what happen if your input doesn't have that.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, what's wrong with just using nextLine() there? Why use findInLine(...)?
    Java Code:
    System.out.print("Do you have a coupon? Y/N ");
    reply = myScanner.nextLine().charAt(0);"

  7. #7
    Iliyas is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    Yeah. I see "nextLine()" resolves another problem. Had some discrepancy using "findInLine(".")" call.
    thanks,

  8. #8
    tghn2b is offline Member
    Join Date
    Jan 2008
    Posts
    31
    Rep Power
    0

    Default

    Just a minor point:

    I think maybe when someone is at the age of 65 they will get in for free.

    if they have a coupon, might even collect 2.00 for saying 'Y'

    8-)

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Iliyas View Post
    Yeah. I see "nextLine()" resolves another problem. Had some discrepancy using "findInLine(".")" call.
    thanks,
    Yes, as I said in the lase post what happen if your input doesn't contain that pattern you are looking. Null...

Similar Threads

  1. Simple code help
    By rednose in forum New To Java
    Replies: 11
    Last Post: 11-30-2008, 07:02 AM
  2. Please help me this is simple bit of code
    By BlitzAcez in forum New To Java
    Replies: 4
    Last Post: 11-27-2008, 06:52 AM
  3. Peculiarty in code of simple program...
    By Kreuz14 in forum New To Java
    Replies: 4
    Last Post: 01-23-2008, 04:27 AM
  4. simple code
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:49 PM
  5. problem with a simple java code
    By boy22 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 03:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •