Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Scanner error

9 views
Skip to first unread message

kalisha malama

unread,
Jul 11, 2016, 3:00:36 PM7/11/16
to
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)

Eric Sosman

unread,
Jul 11, 2016, 3:22:33 PM7/11/16
to
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.)

--
eso...@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Eric Sosman

unread,
Jul 11, 2016, 3:26:28 PM7/11/16
to
On 7/11/2016 3:23 PM, Stefan Ram wrote:
> kalisha malama <malama...@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?

Lew

unread,
Jul 11, 2016, 4:13:04 PM7/11/16
to
> if (age < 13 && age >= 60){

This will never evaluate to 'true'.

--
Lew

Eric Douglas

unread,
Jul 11, 2016, 4:58:12 PM7/11/16
to
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()
0 new messages