Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Very basic code problem

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Very basic code problem

    Hi guys, I've just started trying to teach myself Java less than two weeks ago. I've been reading the "Beginning Programming Java for Dummies" and I've writing small things to practice as I go that review what I've learned so far. I tried to make a small program that checked your age to see if you could buy cigarettes, asked what kind you'd like, ask how many, and then give you a total price. It compiles fine but after the cigarette selection there's an error and it terminates itself and I don't know why.
    This is the error that shows up

    Exception in thread "main" java.lang.NullPointerException
    at CigarettesOrNo.main(CigarettesOrNo.java:56)


    and this is the code

    import java.util.Scanner;
    import static java.lang.System.out;
     
     
    public class CigarettesOrNo {
     
    	public static void main(String args[]) {
    		Scanner myScanner = new Scanner(System.in);
    		int month, day, year;
    		char reply,sure;
    		double marlboro = 5.95;
    		double newport = 6.49;
    		double generic = 4.39;
    		int whichCigarette;
    		int howMany;
     
    		out.println("What is your birthday? Please type as follows MM DD YYYY");
    		month = myScanner.nextInt();
    		day = myScanner.nextInt();
    		year = myScanner.nextInt();
     
    		if (year < 1993) {
    			reply = 'y';
    		}
     
    		else if (year == 1993 && month < 4) {
    			reply = 'y';
    		}
     
    		else if (year == 1993 && month == 4 && day < 27) {
    			reply = 'y';
     
    		}	else {
    			reply = 'n';
    		}
     
    		if (reply == 'y') {
    			out.println("Which cigarettes would you like? 1. Marlboro, 2. Newport, or 3. Generics? Please select a number.");
    			whichCigarette = myScanner.nextInt();
     
    			if (whichCigarette == 1) {
    				out.println("Marlboros are $5.99 per pack, is that alright? (Y/N)");
    				sure = myScanner.findInLine(".").charAt(0);
     
    				if (sure == 'y') {
    					out.println("How many packs would you like?");
    					howMany = myScanner.nextInt();
    					out.print("Your total will be $" + marlboro * howMany + ". Thank you for your purchase!");
    				} 	else {
    					out.println("Thanks for looking.");
    				}	
    		    }
     
    			if (whichCigarette == 2) {
    				out.println("Newports are $6.49 per pack, is that alright? (Y/N)");
    				sure = myScanner.findInLine(".").charAt(0);
     
    				if (sure == 'y') {
    					out.println("How many packs would you like?");
    					howMany = myScanner.nextInt();
    					out.print("Your total will be $" + newport * howMany + ". Thank you for your purchase!");
    				} 	else {
    					out.println("Thanks for looking.");
    				}	
    		    }
     
    			if (whichCigarette == 3) {
    				out.println("Generics are $4.39 per pack, is that alright? (Y/N)");
    				sure = myScanner.findInLine(".").charAt(0);
     
    				if (sure == 'y') {
    					out.println("How many packs would you like?");
    					howMany = myScanner.nextInt();
    					out.print("Your total will be $" + generic * howMany + ". Thank you for your purchase!");
    				} 	else {
    					out.println("Thanks for looking.");
    				}	
    		    }
     
    		} 	else {
    			out.println("I'm sorry, you are too young to buy cigarettes!");
    		}
     
    	}
     
     
    }

    The error points to this line of code "sure = myScanner.findInLine(".").charAt(0);" but I don't know what's wrong with it. Maybe I'm too much of a beginner to see it haha. Anyway, thanks to anyone that can help me


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Very basic code problem

    I think it's System.out.println.

    Also, I think I found something odd.

    sure = myScanner.findInLine(".").charAt(0);

    What is the findInLine thing for?

    I'm going to check it quickly to be sure.....

    Yep, that's what's causing the problems.

    As you only scan in integers or yes or no, I don't see where you'd be entering a "." anyway.

    I must admit I don't really understand the findInLine method.
    Last edited by javapenguin; April 28th, 2011 at 03:26 AM. Reason: Found something else

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very basic code problem

    The thing is I only know so much right now and I'm trying to use what I know to get it done. The findInLine was a variable for the question above it which states the cigarette's price and asks (Y/N) if the price is okay with the customer.

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Very basic code problem

    The problem is this line:

    sure = myScanner.findInLine(".").charAt(0);

    The findInLine call is returning null, so you get an exception when you try to call charAt on the result.

    You've just read in the cigarette number, but you haven't skipped to a new line for the next entry. The nextXxx methods do that, but findInLine will only look at the current line. If you insert myScanner.nextLine(); immediately before your findInLine calls, it should work as expected.

  5. The Following User Says Thank You to dlorde For This Useful Post:

    JavaPF (April 28th, 2011)

  6. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very basic code problem

    I see, I can understand it now. Thanks a lot dlorde!

Similar Threads

  1. Basic Math Expression Java Problem
    By andyluvskrissy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 15th, 2011, 03:22 AM
  2. Basic Math Expression Java Problem
    By andyluvskrissy in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 02:46 PM
  3. Basic Java File I/O with Scanner Class Problem
    By miss confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2010, 08:04 AM
  4. JDBCrealm FORM based problem. But BASIC working perfectly alright!
    By salman4u in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 27th, 2010, 09:06 PM
  5. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM