Results 1 to 12 of 12
Thread: Point my mistake
- 12-14-2010, 05:50 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 31
- Rep Power
- 0
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
- 12-14-2010, 05:55 PM #2
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?
- 12-14-2010, 05:57 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 31
- Rep Power
- 0
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?
- 12-14-2010, 05:58 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 31
- Rep Power
- 0
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?
- 12-14-2010, 06:12 PM #5
......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)
- 12-15-2010, 09:41 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
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");
}
}
RegardsLast edited by SmilingKey; 12-15-2010 at 10:59 AM.
- 12-15-2010, 02:28 PM #7
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.
- 12-15-2010, 09:29 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
also there will be a fault if someone enters 65
- 12-16-2010, 12:48 AM #9
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
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.
RegardsLast edited by SmilingKey; 12-16-2010 at 01:59 AM.
- 12-16-2010, 12:50 AM #10
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
- 12-16-2010, 01:31 AM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 10
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-16-2010, 01:31 PM #12
Spoonfeeding is not helping. Spoonfeeding incorrect answers is definitely not helping.
No. False. That code has no main method- no methods of any kind, or even a class definition. Please read this: SSCCE.
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?
Huh? You have no idea what mood I'm in. I'm pointing out flaws in your unhelpful post, nothing more. Relax.
Similar Threads
-
Help me ! identifying the mistake
By o0oNorao0o in forum New To JavaReplies: 6Last Post: 11-06-2010, 11:03 PM -
i can't see the mistake
By PVL268 in forum New To JavaReplies: 3Last Post: 04-29-2009, 06:26 AM -
i can't see the mistake
By PVL268 in forum New To JavaReplies: 2Last Post: 04-28-2009, 07:30 AM -
PLEASE!!!help me to find mistake
By sasha20 in forum New To JavaReplies: 1Last Post: 01-11-2008, 11:50 AM
Bookmarks