Define a new exception called “NotMemberException”. The exception must be thrown if the registration number entered by the user does not exist in the list of StudInfo. Your program should catch the exception as follows. It should first try to see if the number before and after the one entered exists. If so, it informs the user that they probably made a typo and provides them the name of the found student. If neither is found it must simply inform the user that such registration number does not exist.
1
Expert's answer
2012-05-18T07:35:33-0400
public class NotMemberException extends Exception{ public NotMemberException(){ super("Number not in list!"); } @Override public String toString(){ return "Number not in list!"; } } //----------Main class import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; public class TestExec { public static String promt() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Student ID"); String str = br.readLine(); return str; } public static void main(String[] args) throws IOException, NotMemberException{ String studId; ArrayList<String> listStudId = new ArrayList<String>(); listStudId.add("1234"); listStudId.add("7777"); listStudId.add("1245"); listStudId.add("3421"); listStudId.add("1035"); listStudId.add("5784"); listStudId.add("2222"); listStudId.add("1101"); studId = promt(); Iterator it = listStudId.iterator(); while(it.hasNext()){ String str = (String)it.next(); if(str.equals(studId)){ System.out.println("Student find"); break; }else if(str.startsWith(studId.substring(0, 1))){ System.out.println("You probably made typo. Try again!"); break; } throw new NotMemberException(); } } }
Comments
You're welcome. We are glad to be helpful. If you really liked our service please press like-button beside answer field. Thank you!
THANK YOU VERY MUCH
Leave a comment