Java Forum / General / October 2007
Same problem...
the_transcriber@yahoo.com - 10 Oct 2007 06:38 GMT Ok, i've taken everyones suggestions and am going to give more detail as to what line my error is on.
the line...
reply = myScanner.findInLine(".").charAt(0);
is causing the error...
How old are you? 26 Have a coupon? (Y/N) Exception in thread "main" java.lang.NullPointerException at TicketPriceWithDiscount.main(TicketPriceWithDiscount.java:15)
Process completed.
the output let the user anser Y or N for the coupon question! Im very very new, so speak in the dumbest terms you have! Thanks for everyone's help.
here is the full code, it is straight out of the Beginning Java for Dummies book!
import java.util.Scanner;
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("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!"); } }
Lew - 10 Oct 2007 07:06 GMT > Ok, i've [sic] taken everyones [sic] suggestions and am going to give more detail > as to what line my error is on. Not all of everyone's suggestions.
> double price = 0.00; But more importantly, you're still using
> reply = myScanner.findInLine(".").charAt(0); Please go back and re-read, say, AndrewTK's post, or kcwong's. Brian even gave you an alternative that you apparently ignored.
And since you're asking the same question, you should stay in the same thread rather than start a new one.
 Signature Lew
the_transcriber@yahoo.com - 10 Oct 2007 07:53 GMT > the_transcri...@yahoo.com wrote: > > Ok, i've [sic] taken everyones [sic] suggestions and am going to give more detail [quoted text clipped - 16 lines] > -- > Lew Thanks for your help. Please don't assume that i ignored anything. I just don't understand a lot of the terms that were used in the suggestions that Andrew or kcwong posted. I appreciate everyones help, im just trying to explain that I am very beginner, and that Im not just automatically gonna understand everyones suggestions. I have gone back and re-read, so feel free not to help me if I am frustrating you, or just bare with me, its your choice.
Lew - 10 Oct 2007 08:09 GMT > Thanks for your help. Please don't assume that i ignored anything. I > just don't understand a lot of the terms that were used in the [quoted text clipped - 3 lines] > back and re-read, so feel free not to help me if I am frustrating you, > or just bare with me, its your choice. Frustration is not the operative principle. I am calling your attention to salient points in response to your request for help. I have not expressed frustration. I have repeated points that needed to be highlighted because they answered your question, before you repeated it. This is to help you, and has nothing to do with my feelings.
My style is to state information, baldly and bluntly. It is the points that matter. You may assume that I have no feelings whatsoever for purposes of discussion.
If I were to express feelings (were I to have any), I'd say something like, "I feel ...", or I'd make a blatantly /ad hominem/ remark. There will be no doubt as to whether any feelings are expressed, nor inferences required.
Moreover, do not feel that you have to justify your actions or your questions in any way, other than the technical basis thereof, at least not for my sake. State your actions and your questions; that'll suffice.
When I pointed out that "you apparently ignored" an alternative, that was a statement of a conclusion based on objective evidence. Had you responded to that advice, for example asked for clarification on parts you did not understand, that would have provided counter-evidence. The evidence for my assertion was that you made no modifications whatsoever to the code that was giving you trouble, despite several answers to your specific question, nor asked any questions about that advice, then repeated the same question about the same part of the code.
If you have trouble with specific terms, it's always useful to go back to the basics.
<http://java.sun.com/docs/books/tutorial/index.html> <http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html> <http://java.sun.com/javase/6/docs/api/java/util/Scanner.html>
RTFM. GIYF.
 Signature Lew
the_transcriber@yahoo.com - 12 Oct 2007 17:25 GMT > the_transcri...@yahoo.com wrote: > > Thanks for your help. Please don't assume that i ignored anything. I [quoted text clipped - 43 lines] > -- > Lew I can't go back to the basics if i haven't left them. The help that i received fixed my code of course, but how does that help me? No explanation of why i had to change the code to get it to work was given when i requested. Don't worry about it. And please, no more english analysis, im not even in high school. For Example, "Don't worry about it." I'm quite sure you are not actually worrying about it, but i still said it, knowing that you aren't worried about my problem. "I feel" that you are simply trying to jam all the big words you know into one reply, perhaps to just get me to leave .!
Hunter Gratzner - 12 Oct 2007 18:16 GMT On Oct 12, 6:25 pm, the_transcri...@yahoo.com wrote:
> I can't go back to the basics if i haven't left them. The help that i > received fixed my code of course, but how does that help me? This is a discussion group, not a helpdesk.
> No > explanation of why i had to change the code to get it to work was > given when i requested. Read your textbook.
>"I feel" that you are simply trying to jam all the big words > you know into one reply, perhaps to just get me to leave .! Grow up.
the_transcriber@yahoo.com - 13 Oct 2007 07:51 GMT > On Oct 12, 6:25 pm, the_transcri...@yahoo.com wrote: > [quoted text clipped - 13 lines] > > Grow up. Asking for an explanation of something isn't a discussion? My text book is incorrect, which is what led me here, as I mentioned in the previous parts of my posts. I don't know anybody in person who can discuss with me why the book I am reading from is incorrect! definition of grow up? If i grow up now, then 9 years from now when im 20, i'll be old. No thanks!
Patricia Shanahan - 13 Oct 2007 16:11 GMT >> On Oct 12, 6:25 pm, the_transcri...@yahoo.com wrote: >> [quoted text clipped - 17 lines] > definition of grow up? If i grow up now, then 9 years from now when im > 20, i'll be old. No thanks! People in this newsgroup are supposed to function as adults, regardless of actual age. We don't always manage that, but we are supposed to try.
A lot of the time, we are trying to enable people finding their own answers. That often means pointing to the right place to find the answer, rather than directly answering questions.
By the time you are writing your own non-trivial Java programs, you will have hundreds of cases of needing to know what some method does, so it is worth learning how to find out, and think through the implications, for yourself.
Have you read the API documentation for Scanner (findInLine and next) and String (charAt)?
Patricia
Lew - 13 Oct 2007 02:17 GMT > problem. "I feel" that you are simply trying to jam all the big words > you know into one reply, perhaps to just get me to leave .! Please do not be angry. I apologize for having upset you. It was not my intention. I will try to explain better in the future.
 Signature Lew
the_transcriber@yahoo.com - 13 Oct 2007 07:38 GMT > the_transcri...@yahoo.com wrote: > > problem. "I feel" that you are simply trying to jam all the big words [quoted text clipped - 5 lines] > -- > Lew No problem, im not angry. I just don't speak as accurately as anyone else in here. I've been working with java for about 5 days, with no prior programming experience, so im just taking things one step at a time, very small steps
the_transcriber@yahoo.com - 12 Oct 2007 17:27 GMT > the_transcri...@yahoo.com wrote: > > Thanks for your help. Please don't assume that i ignored anything. I [quoted text clipped - 43 lines] > -- > Lew Also, i'll get myself into more trouble if i make a new post about this new topic... What does [sic] mean?
Gordon Beaton - 12 Oct 2007 17:40 GMT > Also, i'll get myself into more trouble if i make a new post about > this new topic... What does [sic] mean? http://en.wikipedia.org/wiki/Sic
Please trim your posts better!
/gordon
--
John W. Kennedy - 12 Oct 2007 17:49 GMT > Also, i'll get myself into more trouble if i make a new post about > this new topic... What does [sic] mean? "Sic" is Latin for "thus". When enclosed in brackets and inserted into or after a quotation (usually in italics), it means, "Yes, I know that's misspelled, or badly punctuated, or grammatically incorrect, or wrong in some other way, but that's the way I found it in the passage I'm quoting."
 Signature John W. Kennedy "Sweet, was Christ crucified to create this chat?" -- Charles Williams. "Judgement at Chelmsford"
Gordon Beaton - 10 Oct 2007 07:16 GMT > the line... > [quoted text clipped - 12 lines] > very very new, so speak in the dumbest terms you have! Thanks for > everyone's help. The problem is that the input stream contains other characters than just the two responses. More specifically, it contains one or two characters at the end of the line (when you pressed "enter") before the second response.
Try this to see what I mean: at the first prompt, answer *both questions* with a space in between before pressing enter, or follow your first response with a space before pressing enter.
So myScanner.findInLine() returns null when it doesn't find what it's looking for, and an exception is raised when you attempt to call charAt() on null.
My thought here is that you shouldn't be using Scanner directly on stdin (at least not this way), rather you should be preprocessing the input, for example separating it into lines, before looking for the user's answers in it.
/gordon
--
Roedy Green - 13 Oct 2007 09:45 GMT >reply = myScanner.findInLine(".").charAt(0); You pretty well always need a line of code in front of a charAt to make sure the line is 1+ chars long. Sometimes you KNOW it is. It that case you can:
assert line.length>0 : "oops line had no chars after all";
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|