Results 1 to 12 of 12
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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);
    		
    	}
    }
    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...?

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

    Default 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!

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Help with a question


  4. #4
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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);
    		
    	}
    }
    This is the code I have right now. All I get it this...

    Input your age:
    135
    Input your name:
    Army
    Your name is: Army

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

    Default 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!

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

    Default Re: Help with a question

    Also check your indentation; as it is now it is extremely misleading.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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();
    			
    		
    		}
    	}
    this is the new code, but it's not working...

    The age inside the parameter can't be resolved to a variable

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

    Default 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!

  9. #9
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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();
    			
    		
    		}
    	}
    fixed the syntax on it. i still get this though even if the exception is thrown.

    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)

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

    Default 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!

  11. #11
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default 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.

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

    Default 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

  1. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 05:01 PM
  2. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 02:14 AM
  3. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 01:05 AM
  4. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 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
  •