Two errors in my Java assignment

Here is my current code:

import java.lang.String;
import java.io.*;

class InvalidAgeException extends Exception
{
    public InvalidAgeException()
    {
        super("The age you entered is not between 0 and 125");
    }
}

public class questionOne
{
    public static void main(String args[])
    {
        System.out.println("What is your name?");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String name;

        try
        {
            name = br.readLine();
        }
        catch(IOException e)
        {
            System.out.println("Error: " + e);
            System.exit(1);
        }

        System.out.println("Hello " + name + ", how old are you?");

        String i;
        int age;

        try
        {
            i = br.readLine();
            age = Integer.valueOf(i);
        }
        catch(IOException e)
        {
            System.out.println("Error: " + e);
            System.exit(1);
        }
        catch(InvalidAgeException e)
        {
            System.out.println("Error: " + e);
            System.exit(1);
        }
        finally
        {
            System.out.println("No errors found.");
        }
    }
}

The purpose is to write a program that asks for a username and age, and if the age is not between 0 and 125, throw an exception. I get two errors in my code:

questionOne.java:31: variable name might not have been initialized
    System.out.println("Hello " + name + ", how old are you?");
                                  ^
questionOne.java:46: exception InvalidAgeException is never thrown in body of corresponding try statement
    catch(InvalidAgeException e)
    ^
2 errors

I am not sure how to fix them.

+3
source share
9 answers
questionOne.java:31: variable name might not have been initialized
    System.out.println("Hello " + name + ", how old are you?");  

Precisely because the variables inside the function must be initialized before using it. When you try to use uninitialized variables inside a function, the compiler throws an exception.

So try using this

String name = " ";

questionOne.java:46: exception InvalidAgeException is never thrown in body of corresponding try statement
    catch(InvalidAgeException e)  

You haven't thrown away InvalidAgeExceptionanywhere (and therefore the compiler complains).

Try it,

  if (age < 0 || age > 125){
    throw new InvalidAgeException("This is an invalid age :" + age);
}
+2
source

. , Try Catch, , . , catch try

String name = "";

.

Edit:

, catch try.

if (age < 0 || age > 125){
    throw new InvalidAgeException();
}
+3

String name="";

.

, , , , , .

0

. -:

String name = null;

. readline, 0 125, :

throw new InvalidAgeException();
0

. , , , System.exit(1);

InvalidAgeException exetch- try, , , , .

.

0

: name null empty.

, ;) : ?

0

, :

String name = new String();

, , . (, readLine() - IOException, ).

- , InvalidAgeException , , catch.

0

. , , ...

public static void main(String... args) throws IOException, InvalidAgeException {
    System.out.println("What is your name?");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String name = br.readLine();
    System.out.println("Hello " + name + ", how old are you?");
    int age = Integer.parseInt(br.readLine());
    if (age < 0 || age > 125)
        throw new InvalidAgeException();
    System.out.println("No errors found, you are " + age + " years old");
}
0

NullPointerException.

...
String name; //Name is basically null and compiler does not want to throw a NullPointerException.
...
System.out.println("Hello " + name + ", how old are you?");

:

String name = "";
...

Second error: In principle, an InvalidAgeException is never thrown, so catch it. This is what the compiler complains about. What you need to do is throw an InvalidAgeException somewhere.

0
source

All Articles