Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34855 > unrolled thread
| Started by | kalisha malama <malamakalisha@gmail.com> |
|---|---|
| First post | 2016-07-11 12:00 -0700 |
| Last post | 2016-07-11 13:58 -0700 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
Scanner error kalisha malama <malamakalisha@gmail.com> - 2016-07-11 12:00 -0700
Re: Scanner error Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-11 15:22 -0400
Re: Scanner error Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-11 15:26 -0400
Re: Scanner error Lew <lewbloch@gmail.com> - 2016-07-11 13:12 -0700
Re: Scanner error Eric Douglas <e.d.programmer@gmail.com> - 2016-07-11 13:58 -0700
| From | kalisha malama <malamakalisha@gmail.com> |
|---|---|
| Date | 2016-07-11 12:00 -0700 |
| Subject | Scanner error |
| Message-ID | <87b76adc-9800-410f-ac5a-3cd25588bb3a@googlegroups.com> |
Hello All,
Am so new to programming. I need your help. am getting the following error
import java.util.Scanner;
class Apples {
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?(Y/N)");
reply = myScanner.findInLine(".").charAt(0);
if (age >= 13 && age <60){
price = 9.30;
}
if (age < 13 && age >= 60){
price = 5.90;
}
if (reply == 'Y' || reply == 'y'){
price -=2.00;
}
if (reply !='Y' && reply != 'y' &&
reply !='N' && reply !='n'){
System.out.println("Next Serious Customer");
}
System.out.print ("Please Pay $");
System.out.print ("price");
System.out.print (".");
System.out.println("Thank You. Enjoy Your Diner");
}
}
error message
How old are you ?21
Do you have a Coupon?(Y/N)Exception in thread "main" java.lang.NullPointerException
at Apples.main(Apples.java:15)
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2016-07-11 15:22 -0400 |
| Message-ID | <nm0rle$8am$1@dont-email.me> |
| In reply to | #34855 |
On 7/11/2016 3:00 PM, kalisha malama wrote:
> Hello All,
>
> Am so new to programming. I need your help. am getting the following error
>
> import java.util.Scanner;
>
> class Apples {
>
> 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?(Y/N)");
> reply = myScanner.findInLine(".").charAt(0);
>
> if (age >= 13 && age <60){
> price = 9.30;
> }
>
> if (age < 13 && age >= 60){
> price = 5.90;
> }
> if (reply == 'Y' || reply == 'y'){
> price -=2.00;
> }
>
> if (reply !='Y' && reply != 'y' &&
> reply !='N' && reply !='n'){
> System.out.println("Next Serious Customer");
> }
>
>
> System.out.print ("Please Pay $");
> System.out.print ("price");
> System.out.print (".");
> System.out.println("Thank You. Enjoy Your Diner");
> }
>
> }
>
> error message
>
> How old are you ?21
> Do you have a Coupon?(Y/N)Exception in thread "main" java.lang.NullPointerException
> at Apples.main(Apples.java:15)
The nextInt() call consumed the "21" and returned the value 21, but
it stopped consuming input right there, without swallowing the rest of
the line (that is, without swallowing the end-of-line "sentinel," the
marker produced by ENTER or RETURN or whatever).
Then the findInLine() call looked for a single character *in the
same line*, and didn't find one. Failing to find one, it returned null.
As the Javadoc says,
"If no such pattern is detected in the input up to the next
line separator, then null is returned and the scanner's
position is unchanged."
Finally, the program attempts `null.charAt(0)' -- with unfortunate
but predictable results ...
Consider using next() instead of findInLine(). (Also, consider that
"stream-oriented" Scanner may not be the best tool for "line-oriented"
input -- but maybe that's a topic for another day.)
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2016-07-11 15:26 -0400 |
| Message-ID | <nm0rsq$8am$2@dont-email.me> |
| In reply to | #34855 |
On 7/11/2016 3:23 PM, Stefan Ram wrote:
> kalisha malama <malamakalisha@gmail.com> writes:
>> How old are you ?21
>> Do you have a Coupon?(Y/N)Exception in thread "main" java.lang.NullPointerException
>> at Apples.main(Apples.java:15)
>
> The method declaration
>
> void f( final java.lang.CharSequence s ){ s.charAt( 0 ); }
>
> assumes that »s« is not null, otherwise there will be an exception,
> which is a kind of runtime error.
>
> The code could be replaced by, for example,
>
> void f( final java.lang.CharSequence s ){ if( s != null )s.charAt( 0 ); }
>
> .
I, too, sometimes type faster than I think. Will you post a
correction?
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2016-07-11 13:12 -0700 |
| Message-ID | <2fe59664-573d-4ad4-9496-99a7dc969947@googlegroups.com> |
| In reply to | #34859 |
> if (age < 13 && age >= 60){
This will never evaluate to 'true'.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2016-07-11 13:58 -0700 |
| Message-ID | <294eee0c-7d05-4158-b7b9-6db229b41897@googlegroups.com> |
| In reply to | #34855 |
On Monday, July 11, 2016 at 3:00:36 PM UTC-4, kalisha malama wrote:
> Hello All,
>
> Am so new to programming. I need your help. am getting the following error
>
> import java.util.Scanner;
>
> class Apples {
>
> 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?(Y/N)");
> reply = myScanner.findInLine(".").charAt(0);
>
> if (age >= 13 && age <60){
> price = 9.30;
> }
>
> if (age < 13 && age >= 60){
> price = 5.90;
> }
> if (reply == 'Y' || reply == 'y'){
> price -=2.00;
> }
>
> if (reply !='Y' && reply != 'y' &&
> reply !='N' && reply !='n'){
> System.out.println("Next Serious Customer");
> }
>
>
> System.out.print ("Please Pay $");
> System.out.print ("price");
> System.out.print (".");
> System.out.println("Thank You. Enjoy Your Diner");
> }
>
> }
>
> error message
>
> How old are you ?21
> Do you have a Coupon?(Y/N)Exception in thread "main" java.lang.NullPointerException
> at Apples.main(Apples.java:15)
1. Not sure what you're trying to do with the findInLine. Change that line to reply = myScanner.next().charAt(0); will work
2. Logic errors, as Lew points out.
3. Cannot use double for monetary. I replaced that line and added the price variable to the print statement and got output:
Please Pay $7.300000000000001price.Thank You. Enjoy Your Diner
4. I pasted this in Eclipse and got a warning, don't forget to call myScanner.close()
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web