Results 1 to 12 of 12
  1. #1
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default Point my mistake

    public static void main(String[] args)throws Exception{

    BufferedReader input = new BufferedReader (new InputStreamReader(System.in));

    String name;
    System.out.println("ENTER NAME:");
    name=input.readLine();
    System.out.println("Name is:" + name);


    char answer;
    System.out.println("ENTER YOUR AGE:");
    answer=Character.toUpperCase((char)System.in.read( ));

    if (answer <='7')
    System.out.println("Still playing");
    if (answer > '7')
    System.out.println("Is studying");
    if (answer < '21')
    System.out.println("Is studying");
    if (answer > '21')
    System.out.println("Is working");
    if (answer < '65')
    System.out.println("Is working");
    if (answer > '65')
    System.out.println("Now retired");

    whats my mistake?can anyone help me make this program run?
    just the point on how to make it work

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    That wouldn't compile (at the very least, you're missing some closing }s).

    What doesn't work about it? Why are you using chars when you want to compare ints?

  3. #3
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    yea...

    char answer;
    System.out.println("ENTER YOUR AGE:");
    answer=Character.toUpperCase((char)System.in.read( ));

    somehow that is the problem...what code should i put to make it work?

  4. #4
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    int answer;
    System.out.println("ENTER YOUR AGE:");
    answer=Integer.toUpperCase((int)System.in.read());

    when i change it to this one it still doesnt work...any corrections on my code?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    ......what? Integer.toUpperCase()? That isn't a method.

    Why are you trying to convert to upper case if the user is going to be entering numbers?

    Look at the Integer API for useful methods: Integer (Java Platform SE 6)

  6. #6
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    Hi, this should run. Your mistake is that you try to compare String with a String. Answer is a String. '7' (age) is a String. Answer > '7' you cant do that. You have to translate to integer values in order to compare it.

    ParseInt method convert String to Integer.

    Integer answer;
    System.out.println("ENTER YOUR AGE:");
    answer=Integer.parseInt(input.readLine());

    if (answer <=7)
    System.out.println("Still playing");
    else if (answer > 7 && answer < 22)
    System.out.println("Is studying");
    else if (answer > 21 && answer < 65)
    System.out.println("Is working");
    else
    System.out.println("Now retired");
    }
    }

    Regards
    Last edited by SmilingKey; 12-15-2010 at 10:59 AM.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by SmilingKey View Post
    Hi, this should run.
    No, it doesn't. And even if it did, spoonfeeding is not helping.

    Quote Originally Posted by SmilingKey View Post
    Your mistake is that you try to compare String with a String. Answer is a String.
    What does that even mean?

    Quote Originally Posted by SmilingKey View Post
    '7' (age) is a String.
    False. '7' would be a char.

    Quote Originally Posted by SmilingKey View Post
    Answer > '7' you cant do that. You have to translate to integer values in order to compare it.
    False again. You certainly can compare chars to ints. You just might not get the answer you were expecting. Did you try any of this out before posting?

    Spoonfeeding is not helping. Spoonfeeding incorrect answers is really not helping.

  8. #8
    JavaTheHut is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    also there will be a fault if someone enters 65

  9. #9
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    No, it doesn't. And even if it did, spoonfeeding is not helping.


    What does that even mean?


    False. '7' would be a char.


    False again. You certainly can compare chars to ints. You just might not get the answer you were expecting. Did you try any of this out before posting?

    Spoonfeeding is not helping. Spoonfeeding incorrect answers is really not helping.
    Well I think you were a little hard.
    I just try to help and I posted part of questioner code with little changes and my explanation (not absolute truth).

    This should run? Yes, this run cuz I tested myself... dont say No or False to say something, please.

    You were right in this sentence: False. '7' would be a char.

    Also, you said I'm not helping but I think the best way to help is not asking a beginner why he used instead of explain why he shouldn't use: Integer.toUpperCase().

    Try to help and let others to help too. Be happy.
    Thanks for your correction not as much as forms.

    Regards
    Last edited by SmilingKey; 12-16-2010 at 01:59 AM.

  10. #10
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by JavaTheHut View Post
    also there will be a fault if someone enters 65
    If someone enters 65 it should enter into last else:

    else
    System.out.println("Now retired");
    }

    Regards

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by SmilingKey View Post
    Well I think you were a little hard.
    I just try to help and I posted part of questioner code with little changes and my explanation (not absolute truth).

    This should run? Yes, this run cuz I tested myself... dont say No or False to say something, please.

    You were right in this sentence: False. '7' would be a char.

    Also, you said I'm not helping but I think the best method to help is not asking a beginner why he used instead of explain why he shouldn't use: Integer.toUpperCase().

    Try to help and let others to help too. Be happy.
    Thanks for your correction not as much as forms.

    Regards
    poster criticising certainly was not hard, you tried to provide a solution and it was not great, your intentions were good though, posting that a String is a char is not helping. Either review your answers before you post or be brave enough to admit you got it wrong this time. Both will help the OP

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by SmilingKey View Post
    Well I think you were a little hard.
    I just try to help and I posted part of questioner code with little changes and my explanation (not absolute truth).
    Spoonfeeding is not helping. Spoonfeeding incorrect answers is definitely not helping.

    Quote Originally Posted by SmilingKey View Post
    This should run? Yes, this run cuz I tested myself... dont say No or False to say something, please.
    No. False. That code has no main method- no methods of any kind, or even a class definition. Please read this: SSCCE.



    Quote Originally Posted by SmilingKey View Post
    Also, you said I'm not helping but I think the best way to help is not asking a beginner why he used instead of explain why he shouldn't use: Integer.toUpperCase().
    Posting a poorly coded full answer is not helpful- it's actually hurting the OP. Pointing him towards the API and giving him a hint about what's wrong allows him to do the research himself. Now he'll probably use your "solution", without really understanding it, or the problems with it. What do you think will happen when he has to take a test on this stuff? What would have happened had you let him look through the APIs himself?

    Quote Originally Posted by SmilingKey View Post
    Try to help and let others to help too. Be happy.
    Huh? You have no idea what mood I'm in. I'm pointing out flaws in your unhelpful post, nothing more. Relax.

Similar Threads

  1. Help me ! identifying the mistake
    By o0oNorao0o in forum New To Java
    Replies: 6
    Last Post: 11-06-2010, 11:03 PM
  2. i can't see the mistake
    By PVL268 in forum New To Java
    Replies: 3
    Last Post: 04-29-2009, 06:26 AM
  3. i can't see the mistake
    By PVL268 in forum New To Java
    Replies: 2
    Last Post: 04-28-2009, 07:30 AM
  4. PLEASE!!!help me to find mistake
    By sasha20 in forum New To Java
    Replies: 1
    Last Post: 01-11-2008, 11:50 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
  •