Results 1 to 13 of 13
  1. #1
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default I need help on a practice code for java

    Java Code:
    import java.util.Scanner;
    
    class Helpful {
    	
    	public static void main(String args[]) {
    		Scanner myScanner = new Scanner(System.in);
    		int age;
    		double price = 0.00;
    		char reply;
    		boolean isKid, isSenior, hasCoupon, hasNoCoupon;
    		
    		System.out.print("How old are you? ");
    		age = myScanner.nextInt();
    		
    		System.out.print("Have a coupon? (Y/N) ");
    		reply = myScanner.findInLine(".").charAt(0);
    
    		isKid = age < 12;
    		isSenior = age >= 65;
    		hasCoupon = reply == 'Y' || reply == 'y';
    		hasNoCoupon = reply == 'N' || reply == 'n';
    		
    		if (!isKid && !isSenior) {
    			price = 9.25;
    		}
    		if (isKid || isSenior) {
    			price = 5.25;
    		}
    		
    		if (hasCoupon) {
    			price -= 2.00;
    		}
    		if (!hasCoupon && !hasNoCoupon) {
    			System.out.println("Huh?");
    		}
    		
    		System.out.print("Please pay $");
    		System.out.print(price);
    		System.out.println(".");
    		System.out.println("Enjoy the movie!");
    	}
    }
    // below is wat comes up when i hit execute (i type the 34 and hit enter, and the next thing that comes up should make me chose between 'Y' and 'N' but it doesn't)


    How old are you? 34
    Have a coupon? (Y/N) Exception in thread "main" java.lang.NullPointerException
    at Helpful.main(Helpful.java:16)

    Process completed.
    Last edited by JavaBean; 07-16-2007 at 10:31 PM.

  2. #2
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default

    also, i didn't know how to put it on forums without the indents being taken out, so sorry about that.

  3. #3
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    also, i didn't know how to put it on forums without the indents being taken out, so sorry about that.
    You just put it inside [code] tag like this: [ code ] your code .. [ / code] (without spaces!)

  4. #4
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default

    how do i fix wat i have up there?

  5. #5
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    You can try:

    Java Code:
    String reply = myScanner.nextLine();
    if (reply.charAt(0) == 'Y') hasCoupon = true;
    else hasCoupon = false;
    instead of:

    Java Code:
    reply = myScanner.findInLine(".").charAt(0);
    
    hasCoupon = reply == 'Y' || reply == 'y';
    hasNoCoupon = reply == 'N' || reply == 'n';

  6. #6
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default

    didn't work, but ty anyway

  7. #7
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default

    is there any way to get rid of the nullpointerexception? i think that's wat it is

  8. #8
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    Java Code:
    reply = myScanner.findInLine(".").charAt(0);
    In this line, if findInLine method returns null then calling charAt method of a null object reference will return you a NullPointerException! This is why i suggested you to do that in another way.

  9. #9
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default

    ooo, i see

  10. #10
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default

    well, i just took out that line and everything that goes with it with /*-*/ and it still has the nullpointerexception, so im thinking that maybe it isn't the problem

  11. #11
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    Try this:

    Java Code:
    if (myScanner.findInLine(".") == null) System.out.println("This line is the problem");

  12. #12
    upendarreddy is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default replace beline insteadof myScanner.findInline(".").charAt(0);

    reply = myScanner.next().charAt(0);
    it workds.
    enjoy.

  13. #13
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Congrats! You missed being three years late by only a few days :p
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Building small web application in java for practice.
    By Saurabh321 in forum New To Java
    Replies: 1
    Last Post: 02-01-2008, 03:38 PM
  2. java code
    By sobhalr in forum New To Java
    Replies: 0
    Last Post: 12-28-2007, 10:25 AM
  3. Need a java code
    By vissu007 in forum New To Java
    Replies: 1
    Last Post: 07-11-2007, 10:06 PM
  4. Web Services - IBM Expands SOA Management Practice
    By Felissa in forum Web Frameworks
    Replies: 0
    Last Post: 06-25-2007, 04:08 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •