Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34857

Re: Scanner error

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Scanner error
Date 2016-07-11 15:22 -0400
Organization A noiseless patient Spider
Message-ID <nm0rle$8am$1@dont-email.me> (permalink)
References <87b76adc-9800-410f-ac5a-3cd25588bb3a@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web