Results 1 to 12 of 12
Thread: Help with a question
- 05-02-2012, 05:21 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 147
- 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:
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, 05: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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 05-02-2012, 05:38 PM #3
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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, 05:39 PM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 147
- Rep Power
- 0
Re: Help with a question
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, 05: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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 05-02-2012, 06:54 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Help with a question
Also check your indentation; as it is now it is extremely misleading.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 05-02-2012, 07:02 PM #7
Senior Member
- Join Date
- Jan 2012
- Posts
- 147
- Rep Power
- 0
Re: Help with a question
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, 07: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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 05-02-2012, 07:27 PM #9
Senior Member
- Join Date
- Jan 2012
- Posts
- 147
- Rep Power
- 0
Re: Help with a question
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, 07: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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 05-02-2012, 08:30 PM #11
Senior Member
- Join Date
- Jan 2012
- Posts
- 147
- 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, 08: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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
Similar Threads
-
Java Question [Beginner Question]
By joker760 in forum New To JavaReplies: 3Last Post: 12-13-2011, 05:01 PM -
question posted by indissa: library question.
By Fubarable in forum New To JavaReplies: 2Last Post: 11-18-2011, 02:14 AM -
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01-14-2011, 01:05 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 09:49 AM
Bookmarks