Results 1 to 12 of 12
Thread: Help with a question
- 05-02-2012, 04:21 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Help with a question
Write the program where a user enters their name and age and the program checks to see the age is between 0 and 125. If not, the program shows an error code (use Exception Class).
Here is the code that I have at the moment:
The problem is that I don't have an exception thrown because I don't know how to specify the age 0-125. Right now I can input -2 for an age, but nothing happens. How can I specify if age is this, then throw this...?Java Code:import java.util.Scanner; public class EHU1 extends Exception { public static void main(String [] args) throws InvalidAgeException{ Scanner input = new Scanner(System.in); System.out.println("Input your age:"); try { int age = input.nextInt(); System.out.println("Your age is: " + age); }catch(Exception e){ System.out.println("Invalid Age"); } System.out.println("Input your name:"); String name = input.next(); System.out.println("Your name is: " + name); } }
- 05-02-2012, 04:25 PM #2
Re: Help with a question
You have to check the value of age, then do something if it's in a certain range.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-02-2012, 04:38 PM #3
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Help with a question
And then read the following tutorial How to Throw Exceptions (The Java™ Tutorials > Essential Classes > Exceptions)
Website: Learn Java by Examples
- 05-02-2012, 04:39 PM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Help with a question
This is the code I have right now. All I get it this...Java Code:import java.util.Scanner; public class EHU1 extends Exception { public static void main(String [] args){ Scanner input = new Scanner(System.in); System.out.println("Input your age:"); int age = input.nextInt(); if(age >= 0 && age <=125){ System.out.println("Your age is: " + age); if(age < 0 || age > 125){ try { }catch(Exception e){ System.out.println(e); } } } System.out.println("Input your name:"); String name = input.next(); System.out.println("Your name is: " + name); } }
Input your age:
135
Input your name:
Army
Your name is: Army
- 05-02-2012, 04:46 PM #5
Re: Help with a question
That's pretty much what I would expect. Did you read the tutorial on throwing exceptions?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-02-2012, 05:54 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Help with a question
Also check your indentation; as it is now it is extremely misleading.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-02-2012, 06:02 PM #7
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Help with a question
this is the new code, but it's not working...Java Code:import java.util.Scanner; public class EHU1 extends Exception { Scanner input = new Scanner(System.in); private int age = input.nextInt(); public void getAge(age){ this.age = input.nextInt(); //this.age = input.nextInt(); if(age <0 || age > 125){ try { throw new Exception(); } catch (Exception e) { System.out.println("Invalid age" + e); e.printStackTrace(); } } System.out.println("You are " + age + " year(s) old."); } public static void main(String [] args){ System.out.println("Input your age"); EHU1 obj = new EHU1(); obj.getAge(); } }
The age inside the parameter can't be resolved to a variable
- 05-02-2012, 06:10 PM #8
Re: Help with a question
That's not proper syntax for using a parameter. Why are you trying to take a parameter into that method at all?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-02-2012, 06:27 PM #9
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Help with a question
fixed the syntax on it. i still get this though even if the exception is thrown.Java Code:import java.util.Scanner; public class EHU1 extends Exception { Scanner input = new Scanner(System.in); public void getAge(){ int age = input.nextInt(); //this.age = input.nextInt(); if(age <0 || age > 125){ try { throw new Exception(); } catch (Exception e) { System.out.println("Invalid age "); e.printStackTrace(); } } System.out.println("You are " + age + " year(s) old."); } public static void main(String [] args){ System.out.println("Input your age"); EHU1 obj = new EHU1(); obj.getAge(); } }
Input your age
135
Invalid age
You are 135 year(s) old.
java.lang.Exception
at OOP.EHU1.getAge(EHU1.java:13)
at OOP.EHU1.main(EHU1.java:24)
- 05-02-2012, 06:38 PM #10
Re: Help with a question
What do you expect to happen instead?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-02-2012, 07:30 PM #11
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Help with a question
I just don't want it to say, "you are (x) year(s) old", if it goes through the exception.
- 05-02-2012, 07:48 PM #12
Re: Help with a question
Well, that's not what your code is telling the computer to do. Your code gets the age, does something if the age is not valid, and then no matter what, it prints out that sentence.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Java Question [Beginner Question]
By joker760 in forum New To JavaReplies: 3Last Post: 12-13-2011, 04:01 PM -
question posted by indissa: library question.
By Fubarable in forum New To JavaReplies: 2Last Post: 11-18-2011, 01:14 AM -
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01-14-2011, 12:05 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks