Results 1 to 3 of 3
  1. #1
    xpd259 is offline Member
    Join Date
    Dec 2010
    Rep Power

    Default New to java and stuck

    Hiya peoples :)

    I'm very new to java (like days new) and i'm working my way through
    "beginning programming with java for dummies" and using Eclipse as my IDE
    and it's been very helpful and a good start but i've hit an example that doesn't work and I'd like to know why

    it errors on this line 20
    Java Code:
     reply = myScanner.findInLine(“.”).charAt(0);
    and I dont' understand why it errors and I'd like to understand if its a change in Java since the book was written or a co** up on my part

    many thanks in advance

    full code
    Java Code:
    import java.util.Scanner;
         class NicePrice {
         public static void main(String args[]) {
         Scanner myScanner = new Scanner(;
        int age;
       double price = 0.00;
       char reply;
        boolean isKid, isSenior, hasCoupon, hasNoCoupon;
       System.out.print(“How old are you? “);
       age = myScanner.nextInt();
       System.out.print(“Have a coupon? (Y/N) “);
      **/ ERRORS HERE 
      reply = myScanner.findInLine(“.”).charAt(0);
      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) {
               System.out.print(“Please pay $”);
               System.out.print(“. “);
               System.out.println(“Enjoy the show!”);


    Java Code:
    how old are you 5
    Exception in thread "main" Have a coupon? (Y/N) java.lang.NullPointerException
    	at nicePrice.main(

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    This is from the API documentation for the finInLine( ... ) method:

    Quote Originally Posted by API
    Attempts to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged.
    Probably no chars where found before an end of line character was read so the method returned null. On the previous line you were trying to read an int; the nextInt() method doesn't consume new line characters so that explains it. Put another dummy 'myScanner.nextLine()' call after you have read the int so the new line will be swallowed and you are allowed to type Y or N.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    xpd259 is offline Member
    Join Date
    Dec 2010
    Rep Power

    Smile (solved)

    Thank you so much this was really confusing me as I would expect a book to be correct but looks like i was wrong

    Now i understand I can move on

Similar Threads

  1. Replies: 2
    Last Post: 06-01-2010, 02:58 AM
  2. Stuck new to Java
    By Jaguar1998 in forum New To Java
    Replies: 3
    Last Post: 04-09-2010, 12:02 PM
  3. Replies: 5
    Last Post: 09-16-2009, 01:36 PM
  4. Kinda stuck of learning Java
    By jurka in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 04:00 PM
  5. Replies: 2
    Last Post: 12-10-2008, 04:40 PM

Posting Permissions

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