Results 1 to 13 of 13
  1. #1
    scotts is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default Code does not work

    Hi,
    tried running the following code but after entering integer input,the code gives an errror message..
    pls enlighten me why..
    thkx
    Here is the code:

    import java.util.Scanner;
    import static java.lang.System.in;
    import static java.lang.System.out;
    class Guyton
    {
    public static void main(String args[])
    {
    Scanner myScanner = new Scanner(in);
    double price = 0.00;
    int age;
    char reply;
    out.println("Welcome to the McTwisp Movie Theatre.What is your age?");

    age = myScanner.nextInt();
    if (age >= 13 && age < 65)
    {
    price = 5.38;
    }
    if (age < 13 || age >= 65)
    {
    price = 3.00;
    }
    out.println("Do you have a coupon? Y/N");
    reply = myScanner.findInLine(".").charAt(0);
    if ( reply == 'Y' && reply == 'y')
    {
    price -= 2.00;
    }
    if (reply != 'Y' || reply != 'y' || reply != 'N' || reply != 'n')
    {
    out.println("Huh?");
    }
    out.println("Please pay $ ");
    out.println("Have a great time");
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by scotts View Post
    Java Code:
      if (reply != 'Y' || reply != 'y' || reply != 'N' || reply != 'n')
      {
       out.println("Huh?");
      }
    Here your logic is incorrect: suppose reply == 'Y; then most certainly it isn't equal to a 'y' or an 'N' or an 'n'. Change the || operator to the && operator.

    kind regards,

    Jos

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    "the code gives an errror message"

    Why do people not actually give us the error message?
    It's my wife that's the mindreader, not me...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    "the code gives an errror message"

    Why do people not actually give us the error message?
    It's my wife that's the mindreader, not me...
    Your wife too? My conjecture is that all women are psychic in a scary way; us poor men just have to work hard, put the trash out, keep our mouths shut and do the debugging ;-)

    kind regards,

    Jos

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    Apart form Jos comments,one more thing here I want to add is you are trying to get the int value,but you are trying to use this below String method which obviously throw NullPointerException.
    reply = myScanner.findInLine(".").charAt(0);

    Another thing is put try catch block with printStackTrace() to debug..
    Ramya:cool:

  6. #6
    scotts is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default user input error

    Hi,
    thks for all the solutions ..
    after tweaking a part of the code it reads as:

    import java.util.Scanner;
    import static java.lang.System.in;
    import static java.lang.System.out;
    class Guyton
    {
    public static void main(String args[])
    {
    Scanner myScanner = new Scanner(in);
    double price = 0.00;
    int age;
    char reply;
    out.println("Welcome to the McTwisp Movie Theatre.What is your age?");

    age = myScanner.nextInt();
    if (age >= 13 && age < 65)
    {
    price = 5.38;
    }
    if (age < 13 || age >= 65)
    {
    price = 3.00;
    }
    out.println("Do you have a coupon? Y/N");
    reply = myScanner.findInLine(".").charAt(0);
    if ( reply == 'Y' && reply == 'y')
    {
    price -= 2.00;
    }
    if (reply != 'Y' && reply != 'y' && reply != 'N' && reply != 'n')
    {
    out.println("Huh");
    }
    out.println("Please pay $ ");
    out.println("Have a great time");
    }
    }

    and i don't quite understand the part about the int value being below the String value..pls give a clear explanation cos i'm still a noob in java
    or help me recorrect the code
    thx

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    myScanner.findInLine(".").charAt(0);
    Break this statement up into two parts and test the results.
    Test what findInLine() returns before using charAt().

  8. #8
    scotts is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default error msg continues

    Hi Norm,
    when i seperate the code as you mentioned it gives this error msg when compiled:
    reply = myScanner.findInLine();
    ^
    any solutions?
    thkx

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Well, you didn't give us the error, but I'm guessing it's because you are assigning a String (which is what is returned by findInLine()) to a char (which is what reply is).

    You should assign the result of findInLine() to a String, and then assign the result of charAt() on that String to "reply".

  10. #10
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    you have to be strong in writing the comments as well as clear in ur logic.
    below are my highlighted change.

    two changes I made and also u should include jos changes.

    1.after reciving an int using scanner u should move the ciursor to next line using nextLine() method.

    2.I have taken the input string and extracted the char instead of findInline()...


    if (age < 13 || age >= 65)
    {
    price = 3.00;
    }
    myScanner.nextLine();//moving the cursor to next lineout.println("Do you have a coupon? Y/N");

    reply = myScanner.nextLine().trim().charAt(0);
    Ramya:cool:

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    reply = myScanner.nextLine().trim().charAt(0);
    I think this is poor technique to chain one methods results to another method call when its possible for an earlier method to fail.
    Especially when debugging you need to see the results of all the methods and not assume that the data is how you expect it to be.

  12. #12
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    Hi Norm,
    As per yours myScanner.nextLine() is having the possiblity of throwing nullpointer exception.But,it will wait for the user to input y/n.


    I identified the error and given the above solution.
    Ramya:cool:

  13. #13
    scotts is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Smile Solved

    Thanks Troll, Ramya and Norm for helping out..
    i changed two of the lines to:

    myScanner.nextLine();
    reply = myScanner.findInLine(".").charAt(0);

    Ramya's way worked too..
    glad its finally solved..

Similar Threads

  1. Why this code don't work?
    By artemff in forum CLDC and MIDP
    Replies: 6
    Last Post: 04-16-2010, 02:57 AM
  2. How does the '*' work in the code?
    By DrMath in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 09:26 AM
  3. Replies: 2
    Last Post: 07-29-2009, 12:23 PM
  4. how does this code work...explain me the execution please...
    By vital parsley in forum New To Java
    Replies: 3
    Last Post: 07-25-2008, 04:50 AM
  5. the code dosent work
    By pcman in forum Java 2D
    Replies: 1
    Last Post: 03-20-2008, 08:20 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
  •