Results 1 to 9 of 9
Thread: Newbie question
- 11-12-2010, 01:59 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
Newbie question
Hi Im new to the forums but I have a quick question.
I am trying to compile this code.
import java.util.Scanner;
class TicketPrice {
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("Have a coupon? (Y/N) ");
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 != 'Y' && reply != 'y' &&
reply!= 'N' && reply!= 'n') {
System.out.println("Huh?");
}
System.out.print("Please pay $");
System.out.print(price);
System.out.print(". ");
System.out.println("Enjoy the show!");
}
}
...and it compiles just fine. When I execute it it asks for the age and the prompt waits for you to put in the integer. Then it asks whether you want a coupon or not but rushes through without giving the option to Type Y or N and it gives me this error.
How old are you? 4
Have a coupon? (Y/N) Exception in thread "main" java.lang.NullPointerException
at TicketPrice.main(TicketPrice.java:17)
Process completed.
Any help would be greatly appreciated! :)Last edited by MrNiceGuy; 11-12-2010 at 02:03 AM.
-
Your problem may be coming from your current code not allowing the Scanner object to properly handle the end of line (EOL) token. You may wish to place a call to
after your call to nextInt(). Give it a try and let us know if it helps.Java Code:myScanner.nextLine();
Luck!
- 11-12-2010, 02:09 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Hi,
When you use nextInt you only read the integer not the linefeed. So you have to add a nextLine after nextInt to read that linefeed.
To read the Y or N you can use scanner.next (that reads just one character).
Hope this helps,
Erik
BTW Please put your code between [code][/code] tags. That leaves the indenting in tact.
Follow nextInt with a scanner.nextLine to read the linefeed and continue with the nextI'm new to Java but I like to help where ever I can. :)
- 11-12-2010, 04:04 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
Thanks so much this fixed it! :)
Will I always need to follow a scanner with this myScanner.nextLine(); if I have multiple scanners and prompts in the same body?
I'm still new to this and I appreciate all help! :)
- 11-12-2010, 04:06 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
- 11-12-2010, 04:32 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
Are you aware of the java.lang package?
if not it contains the functionality to convert to Lowercase or UpperCase
import java.lang.*;
this will turn your above if statement to...Java Code:char lowerReply = Character.toLowerCase(reply);
Java Code:if (lowerReply == 'y') { price -= 2.00; } if (lowerReply != 'y' || lowerReply!= 'n') { System.out.println("Huh?"); }
I think i read somewhere that double is not the best for working with $ values either (but not sure if i read this right)
- 11-12-2010, 04:44 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
Hah! That simplifies all that text great tip! Thanks :)
I read somewhere that there is a NumberFormat.getCurrencyInstance that works better for money values but im not that advanced yet. I'm doing self-learning with textbooks at home and taking it in steps so I don't confuse myself.
- 11-12-2010, 05:21 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Will I always need to follow a scanner with this myScanner.nextLine();
Looking at the API documentation it appears that the nextXXX() methods leave the newline character where it is, so you have to nextLine() to advance past it. The API documentation is the final arbiter in such matters, and the first place to look if in doubt.
As far as money is concerned, it's best to work in cents (int or, if you're rich, long), or construct you're own class if there are complex particular rules that you must follow for rounding.
NumberFormat.getCurrencyInstance() is for formatting numeric quantities for display as human readable (currency) strings while not making those quantities increasingly inaccurate because of rounding.
Practice exercises often take money to be doubles: which is OK although you should recognise that floating point arithmetic might result in answers that banks, lawyers and their ilk may not like. Even in that case you should use NumberFormat (or the printf() family) for displaying the numbers.
- 11-12-2010, 05:29 AM #9
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
First Assignment and already stuck....Newbie Question
By Danieldcc in forum New To JavaReplies: 5Last Post: 09-24-2010, 11:44 PM -
Newbie design question
By PrinceSendai in forum JDBCReplies: 1Last Post: 08-17-2010, 06:42 AM -
newbie question
By ronguilmet in forum New To JavaReplies: 2Last Post: 11-16-2009, 02:37 AM -
Newbie search array question
By CirKuT in forum New To JavaReplies: 19Last Post: 09-14-2008, 06:26 AM -
Newbie question; Vectors
By Kern in forum New To JavaReplies: 7Last Post: 08-03-2008, 06:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks