Results 1 to 9 of 9

Thread: Newbie question

  1. #1
    MrNiceGuy is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Newbie question

    Hi Im new to the forums but I have a quick question.

    I am trying to compile this code.



    import java.util.Scanner;

    class TicketPrice {

    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("Have a coupon? (Y/N) ");
    reply = myScanner.findInLine(".").charAt(0);

    if (age >= 12 && age < 65) {
    price = 9.25;
    }
    if (age < 12 || age >= 65) {
    price = 5.25;
    }
    if (reply == 'Y' || reply == 'y') {
    price -= 2.00;
    }
    if (reply != 'Y' && reply != 'y' &&
    reply!= 'N' && reply!= 'n') {
    System.out.println("Huh?");

    }
    System.out.print("Please pay $");
    System.out.print(price);
    System.out.print(". ");
    System.out.println("Enjoy the show!");
    }
    }


    ...and it compiles just fine. When I execute it it asks for the age and the prompt waits for you to put in the integer. Then it asks whether you want a coupon or not but rushes through without giving the option to Type Y or N and it gives me this error.

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

    Process completed.

    Any help would be greatly appreciated! :)
    Last edited by MrNiceGuy; 11-12-2010 at 02:03 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem may be coming from your current code not allowing the Scanner object to properly handle the end of line (EOL) token. You may wish to place a call to
    Java Code:
    myScanner.nextLine();
    after your call to nextInt(). Give it a try and let us know if it helps.

    Luck!

  3. #3
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Hi,

    When you use nextInt you only read the integer not the linefeed. So you have to add a nextLine after nextInt to read that linefeed.

    To read the Y or N you can use scanner.next (that reads just one character).

    Hope this helps,
    Erik

    BTW Please put your code between [code][/code] tags. That leaves the indenting in tact.

    Follow nextInt with a scanner.nextLine to read the linefeed and continue with the next
    I'm new to Java but I like to help where ever I can. :)

  4. #4
    MrNiceGuy is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks so much this fixed it! :)

    Will I always need to follow a scanner with this myScanner.nextLine(); if I have multiple scanners and prompts in the same body?

    I'm still new to this and I appreciate all help! :)

  5. #5
    MrNiceGuy is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by venerik View Post
    Hi,

    When you use nextInt you only read the integer not the linefeed. So you have to add a nextLine after nextInt to read that linefeed.

    To read the Y or N you can use scanner.next (that reads just one character).

    Hope this helps,
    Erik

    BTW Please put your code between [code][/code] tags. That leaves the indenting in tact.

    Follow nextInt with a scanner.nextLine to read the linefeed and continue with the next
    I will put my code in the code /code tags form now on thanks for the tip. :)

  6. #6
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by MrNiceGuy View Post
    }
    if (reply == 'Y' || reply == 'y') {
    price -= 2.00;
    }

    if (reply != 'Y' && reply != 'y' &&
    reply!= 'N' && reply!= 'n') {
    System.out.println("Huh?");
    }
    Are you aware of the java.lang package?

    if not it contains the functionality to convert to Lowercase or UpperCase

    import java.lang.*;

    Java Code:
    char lowerReply = Character.toLowerCase(reply);
    this will turn your above if statement to...

    Java Code:
      if (lowerReply == 'y') {
         price -= 2.00;
      }
    
      if (lowerReply != 'y' || lowerReply!= 'n') {
        System.out.println("Huh?");
      }

    I think i read somewhere that double is not the best for working with $ values either (but not sure if i read this right)

  7. #7
    MrNiceGuy is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by venerik View Post
    Hi,

    When you use nextInt you only read the integer not the linefeed. So you have to add a nextLine after nextInt to read that linefeed.

    To read the Y or N you can use scanner.next (that reads just one character).

    Hope this helps,
    Erik

    BTW Please put your code between [code][/code] tags. That leaves the indenting in tact.

    Follow nextInt with a scanner.nextLine to read the linefeed and continue with the next
    Quote Originally Posted by maknib View Post
    Are you aware of the java.lang package?

    if not it contains the functionality to convert to Lowercase or UpperCase

    import java.lang.*;

    Java Code:
    char lowerReply = Character.toLowerCase(reply);
    this will turn your above if statement to...

    Java Code:
      if (lowerReply == 'y') {
         price -= 2.00;
      }
    
      if (lowerReply != 'y' || lowerReply!= 'n') {
        System.out.println("Huh?");
      }

    I think i read somewhere that double is not the best for working with $ values either (but not sure if i read this right)
    Hah! That simplifies all that text great tip! Thanks :)

    I read somewhere that there is a NumberFormat.getCurrencyInstance that works better for money values but im not that advanced yet. I'm doing self-learning with textbooks at home and taking it in steps so I don't confuse myself.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Will I always need to follow a scanner with this myScanner.nextLine();

    Looking at the API documentation it appears that the nextXXX() methods leave the newline character where it is, so you have to nextLine() to advance past it. The API documentation is the final arbiter in such matters, and the first place to look if in doubt.

    As far as money is concerned, it's best to work in cents (int or, if you're rich, long), or construct you're own class if there are complex particular rules that you must follow for rounding.

    NumberFormat.getCurrencyInstance() is for formatting numeric quantities for display as human readable (currency) strings while not making those quantities increasingly inaccurate because of rounding.

    Practice exercises often take money to be doubles: which is OK although you should recognise that floating point arithmetic might result in answers that banks, lawyers and their ilk may not like. Even in that case you should use NumberFormat (or the printf() family) for displaying the numbers.

  9. #9
    MrNiceGuy is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Looking at the API documentation it appears that the nextXXX() methods leave the newline character where it is, so you have to nextLine() to advance past it. The API documentation is the final arbiter in such matters, and the first place to look if in doubt.

    As far as money is concerned, it's best to work in cents (int or, if you're rich, long), or construct you're own class if there are complex particular rules that you must follow for rounding.

    NumberFormat.getCurrencyInstance() is for formatting numeric quantities for display as human readable (currency) strings while not making those quantities increasingly inaccurate because of rounding.

    Practice exercises often take money to be doubles: which is OK although you should recognise that floating point arithmetic might result in answers that banks, lawyers and their ilk may not like. Even in that case you should use NumberFormat (or the printf() family) for displaying the numbers.
    Good stuff Bookmarked the API Documentation for future reference :)

Similar Threads

  1. First Assignment and already stuck....Newbie Question
    By Danieldcc in forum New To Java
    Replies: 5
    Last Post: 09-24-2010, 11:44 PM
  2. Newbie design question
    By PrinceSendai in forum JDBC
    Replies: 1
    Last Post: 08-17-2010, 06:42 AM
  3. newbie question
    By ronguilmet in forum New To Java
    Replies: 2
    Last Post: 11-16-2009, 02:37 AM
  4. Newbie search array question
    By CirKuT in forum New To Java
    Replies: 19
    Last Post: 09-14-2008, 06:26 AM
  5. Newbie question; Vectors
    By Kern in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 06:59 AM

Posting Permissions

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