Two errors in my Java assignment
Here's 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)
{
开发者_Python百科 System.out.println("Error: " + e);
System.exit(1);
}
finally
{
System.out.println("No errors found.");
}
}
}
The assignment is to write a program that asks for the user's name and age, and if the age is not between 0 and 125 throw an exception. I'm getting 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'm not sure how to fix them.
Your first error is cause by never initializing name. Keep in mind that its only initialized within the Try Catch block, which causes the compiler to throw an error since it may never be initialized within the program. So altering your line above the try catch block to
String name = "";
Will resolve that error.
Edit:
This might be what you need to do outside of the try catch block.
if (age < 0 || age > 125){
throw new InvalidAgeException();
}
questionOne.java:31: variable name might not have been initialized
System.out.println("Hello " + name + ", how old are you?");
It is because variables inside the function needs to be initialized before using it. When you try to use uninitialized variables inside a function, 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 have not thrown InvalidAgeException
anywhere ( and so the compiler is complaining).
Try this,
if (age < 0 || age > 125){
throw new InvalidAgeException("This is an invalid age :" + age);
}
You could do String name="";
Also age could do with initialising too.
When you get the age, you need to do your check, then if out of range, throw your exception, it won't come out for you automatically.
They both give you the solution. Just initialize the variable to something:
String name = null;
While for the second, no one is throwing the exception. Do the checks after you do the readline
, and if the parsed value is smaller than 0 or bigger than 125, do this:
throw new InvalidAgeException();
Initilize name. The compiler isn't intelligent enough to know that the program will terminate when you hit System.exit(1);
InvalidAgeException isn't never thrown by any exeucted code in your try block, thus it is not necessary, at least not yet with the code you have.
The compiler is has very descriptive messages.
The first error is easy: initialize name
as null or empty.
The second one is actually caused because you have not solved your homework yet ;) Hint: where is the validation?
You can get rid of the first error by changing your declaration to:
String name = new String();
It's complaiing becuase it sees a path through the code where name doesn't get assigned to. (Like if readLine() threw something other than an IOException, for example).
The second error is cause becuase you do not throw the InvalidAgeException anywhere, so it doesn't think you need that catch block.
I suggest you try to simplify your code. The more code you write the harder it is to see what your program actually does...
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");
}
The problem with the first error is like a compile 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?");
Instead try this:
String name = "";
...
Second error: Basically InvalidAgeException is never thrown so why catch it. That is what the compiler is complaining. What you should do is throw your InvalidAgeException somewhere.
精彩评论