CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2012
    Posts
    1

    Angry java.lang.NullPointerException :P

    Hi I have just started java lang for some while when i encountered this problem in a .pdf java for dummies . Here is a little programm in which i cant find an error it still compile without error i cant get it run properly .



    import java.util.Scanner;


    class NicePrice {


    public static void main(String args[]) {
    Scanner myScanner = new Scanner(System.in);
    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("Do you have a coupon ? (Y/N) ");
    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.println("Ti?");
    }

    System.out.print("Please pay $");
    System.out.print(price);
    System.out.print(". ");
    System.out.println("Geia Xara!");

    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java.lang.NullPointerException :P

    Which bit of it doesn't work? Please state what values you are entering, what should happen and what actually happens
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2007
    Posts
    442

    Re: java.lang.NullPointerException :P

    Why do you use findInLine method? Why not just check the input?

    Why do you expect that System.in would contain a "."? You do not place it there and should the user somehow know place it, he would place it last (as in Y.). However, the specification says that upon finding the pattern (here: ".") Scanner would advance past that. Hence, for Yes answers: "Y." would always fail, "Y" would always fail, only ".Y" would work.

    And you may have to use Scanner.nextLine() to advance the scanner initially to something readable. The API is a bit obscure, it also states that it may block untill pattern is matched, or line is ended. Either way you can get a null response, and in what you are doing, theres no need to have one of those. "Y" or "y" is a coupon, anything else is not.

    Do not use two booleans to describe same element (hasCoupon / hasNoCoupon). Booleans are dangerous, and this will only be super confusing and really error prone.

  4. #4
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: java.lang.NullPointerException :P

    Quote Originally Posted by Londbrok View Post
    Why do you expect that System.in would contain a "."? You do not place it there and should the user somehow know place it, he would place it last (as in Y.). However, the specification says that upon finding the pattern (here: ".") Scanner would advance past that. Hence, for Yes answers: "Y." would always fail, "Y" would always fail, only ".Y" would work.
    The string passed to the findInLine method is used to construct a pattern, so "." will capture a single character not just a '.' (ignoring delimiters).
    Quote Originally Posted by Londbrok View Post
    "Y" or "y" is a coupon, anything else is not.

    Do not use two booleans to describe same element (hasCoupon / hasNoCoupon). Booleans are dangerous, and this will only be super confusing and really error prone.
    That's right, both are false if the user enters anything not in the list of valid answers ('Y', 'y', 'N', 'n'), in which case it should keep asking until the answer is valid.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured