Same problem...

T

the_transcriber

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!");
}
}
 
L

Lew

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.
 
G

Gordon Beaton

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.

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

--
 
T

the_transcriber

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.

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.
 
L

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.

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.
 
T

the_transcriber

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.

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 .!
 
T

the_transcriber

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.

Also, i'll get myself into more trouble if i make a new post about
this new topic... What does [sic] mean?
 
J

John W. Kennedy

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."
 
H

Hunter Gratzner

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.
 
L

Lew

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.
 
T

the_transcriber

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.

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
 
T

the_transcriber

This is a discussion group, not a helpdesk.


Read your textbook.


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!
 
R

Roedy Green

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";
 
P

Patricia Shanahan

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!

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top