|
-
January 6th, 2012, 12:47 PM
#1
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!");
}
}
-
January 8th, 2012, 08:32 AM
#2
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
-
January 10th, 2012, 06:03 AM
#3
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.
-
January 10th, 2012, 06:27 AM
#4
Re: java.lang.NullPointerException :P
 Originally Posted by Londbrok
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).
 Originally Posted by Londbrok
"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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|