Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Scanner error Date: Mon, 11 Jul 2016 15:22:18 -0400 Organization: A noiseless patient Spider Lines: 75 Message-ID: References: <87b76adc-9800-410f-ac5a-3cd25588bb3a@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 11 Jul 2016 19:22:22 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="e905b254ff7ab0d0dda2c4195925adb8"; logging-data="8534"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8vgTwX5yJAaWow2QCXmcE" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: <87b76adc-9800-410f-ac5a-3cd25588bb3a@googlegroups.com> Cancel-Lock: sha1:Wzx2EFAYgf03QTa84EXeLp3jfUk= Xref: csiph.com comp.lang.java.programmer:34857 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