Exception try and catch on split name into first and last
Solution 1
I would suggest to catch the Exception in the Name
class and then throw
the InvalidNameException
like this:
public void printName(String name) throws InvalidNameException {
try {
String[] nameSplit = name.split(" ");
String first = nameSplit[0];
String last = nameSplit[1];
System.out.println("First name: " + first);
System.out.println("Last name: " + last);
} catch (ArrayIndexOutOfBoundsException e) {
throw new InvalidNameException("Missing space in: " + name);
}
}
UPDATE
Running this code:
import javax.naming.InvalidNameException;
public class NameApp{
public static void main(String[] args) {
Name aa = new Name();
try {
System.out.print("Enter your name: ");
String in = "George Den";
aa.printName(in);
in = "George";
aa.printName(in);
}
catch(InvalidNameException e){
System.out.println(e.toString());
}
}
}
import javax.naming.InvalidNameException;
public class Name {
public void printName(String name) throws InvalidNameException {
try {
String[] nameSplit = name.split(" ");
String first = nameSplit[0];
String last = nameSplit[1];
System.out.println("First name: " + first);
System.out.println("Last name: " + last);
} catch (ArrayIndexOutOfBoundsException e) {
throw new InvalidNameException("Missing space in: " + name);
}
}
}
Will print this:
Enter your name: First name: George Last name: Den
javax.naming.InvalidNameException: Missing space in: George
UPDATE 2
Take into account @robjohncox advice. This code shouldn't be used as is because the javax.naming.InvalidNameException
should be thrown only at specific cases such as LDAP lookups and generally directory specific operations.
What should be best for you is to sub-class Exception Object and create your own kind of exception (see also this How can I write custom Exceptions? ):
public class MyCustomNamingException extends Exception {
public MyCustomNamingException(){
}
public MyCustomNamingException(String message){
super(message);
}
}
And then use this MyCustomNamingException
in your code as was previously illustrated using the InvalidNameException
Solution 2
Simply do something like this:
if (name.contains(" ")) {
} else {
throw new InvalidNameException ("Name does not contain the space");
}
Solution 3
The behavior is as expected, the exception is triggered by the line
String last = nameSplit[1]
because the result of "RuthKnight".split(" ") results in an array with a single element, and therefore it has no element at position 1 (causing the ArrayIndexOutOfBoundsException
to be thrown).
The exception type javax.naming.InvalidNameException
isn't thrown anywhere in your code, and you shouldn't really use it as this exception is designed for use by the Java naming services code in javax.naming
.
You should modify your code something like this:
String in = console.nextLine();
if (in.contains(" ") {
aa.printName(in);
}
else {
System.out.println("invalid name");
}
Evan S
Updated on June 04, 2022Comments
-
Evan S almost 2 years
I am asked to write a piece of program to handle exception using try and catch. Although, when I ran it, it would not have reflected my exception codes as "invalid name" should have printed. Can anybody point the reason out? If someone corrects my code would be big welcome as well! Thanks.
I need write a program under the following conditions :
import javax.naming.InvalidNameException. Write a method public void printName(String name) throws InvalidNameException If name has no white space the method should throw an InvalidNameException. Write a driver to test.
===============Code result============= Enter your name: James Dean First name: James Last name: Dean Enter your name: Brian Smith First name: Brian Last name: Smith Enter your name: RuthKnight Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Name.printName(Name.java:33) at NameApp.main(NameApp.java:17)
==================Method============================ import javax.naming.InvalidNameException; public class Name{ public void printName(String name) throws InvalidNameException{ String [] nameSplit = name.split(" "); String first = nameSplit[0]; String last = nameSplit[1]; System.out.println("First name: "+first); System.out.println("Last name: "+last); } }
======================Driver======================== import java.util.Scanner; import javax.naming.InvalidNameException; public class NameApp{ public static void main(String[] args) { Name aa = new Name(); Scanner console = new Scanner(System.in); try { boolean keepRunning= true; while(keepRunning){ System.out.print("Enter your name: "); String in = console.nextLine(); aa.printName(in); } } catch(InvalidNameException e){ System.out.println("invalid name"); } } }
-
Evan S almost 11 yearsHi Robjohncox! I would be happy as well if I could write codes like you wrote :-) But it is given task so no choice but to use javax.naming.InvalidNameException. Thanks for the explanation on ArrayIndexOutOfBoundsException.
-
robjohncox almost 11 yearsNo choice but to use InvalidNameException? That is unusual. Perhaps you should rebel against the task and explain why it shouldn't be used :)
-
Evan S almost 11 yearsRebel? HAHAHA That's cool though! Since I have to deal with it, no hesitate to share your input if you know about it. It would be helpful. Thanks :-)
-
Evan S almost 11 yearsIt led this result.....error: unreported exception InvalidNameException; must be caught or declared to be thrown aa.printName(in);
-
Evan S almost 11 yearsThank you so much for your update! I ran them and they worked the way I wanted. Thanks for your effort. I am novice to Java. So I need full codes to understand sometimes. I will take some time to memorize them tonight :-)