Results 1 to 9 of 9
- 12-25-2008, 01:53 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
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!");
}
}
-
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:
Question, why the call to findInLine(".")? What is the purpose of using this and not nextLine()?Java Code:System.out.print("How old are you?"); age = myScanner.nextInt(); myScanner.nextLine(); // add this to swallow the end of line character
- 12-25-2008, 02:34 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you are going to do here?
Java Code:reply = myScanner.findInLine(".").charAt(0);
- 12-25-2008, 02:41 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
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.
- 12-25-2008, 02:45 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
-
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);"
- 12-25-2008, 04:04 PM #7
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
Yeah. I see "nextLine()" resolves another problem. Had some discrepancy using "findInLine(".")" call.
thanks,
- 12-26-2008, 02:31 AM #8
Member
- Join Date
- Jan 2008
- Posts
- 31
- Rep Power
- 0
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-)
- 12-26-2008, 03:17 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Simple code help
By rednose in forum New To JavaReplies: 11Last Post: 11-30-2008, 06:02 AM -
Please help me this is simple bit of code
By BlitzAcez in forum New To JavaReplies: 4Last Post: 11-27-2008, 05:52 AM -
Peculiarty in code of simple program...
By Kreuz14 in forum New To JavaReplies: 4Last Post: 01-23-2008, 03:27 AM -
simple code
By elizabeth in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:49 PM -
problem with a simple java code
By boy22 in forum New To JavaReplies: 2Last Post: 08-03-2007, 02:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks