2013年12月13日星期五

java beginner , help , problem exception handling.

import java.io.IOException;
import java.util *.;
/ / 1. Write a program that allows the user to an array of size 10 input integer value . Procedures should be by index or by specifying a value greater than 0 to find the array elements ,
/ / to obtain values ​​in the array . Program should handle any exceptions that occur when entering a value or access to an array of array elements .
/ / In addition , the program should be used to customize a NumberNotFoundException exception handling classes.
/ / If you try to access the array elements beyond the border , the capture ArrayIndexOutOfBoundsException exception and display an appropriate error message.
public class Array {

public void test1 () throws NumberNotFoundException {
try {
int [] arr = new int [10];
for (int j = 0; j byte [] b = new byte [1024];
System.out.println (" Please enter a value stored in the array : " ) ;
System.in.read (b);
String str = new String (b) trim ().;
int num = Integer.parseInt (str);
arr [j] = num;
}
System.out.println (" Get ready! " ) ;
Scanner in = new Scanner (System.in);
System.out.println (" Find an array of type: " ) ;
int x = in.nextInt ();
for (int i = 0; i if (arr [x] == arr [i]) {
System.out.println (" You entered the array with the first " + i + " a value :" + arr [i] + " array of identical" ) ;
}
}
} catch (IOException io) {
io.printStackTrace ();
}
catch (ArrayIndexOutOfBoundsException a) {
a.printStackTrace ();
System.out.println (" array out of bounds exception ." ) ;
}
catch (Exception e) {
throw new NumberNotFoundException ("asd custom exception error" ) ;
} finally {
System.out.println ("finally");
}
}

public static void main (String [] args) {
Array aa = new Array ();
try {
aa.test1 ();
} catch (NumberNotFoundException n) {
System.err.println (n.getMessage ());
}
}
}


The above is the first question of the source , NumberNotFoundException this exception class has already been created ! ! Then the second question does not know how to do, can not think of any contact , beginners , fast insane . Here is the second question topic . .

/ / 2. modify the program 1 , creates an exception class named DuplicateValueException to detect whether the user has entered a duplicate number.
/ / If the number entered by the user already exists in the array, you should throw a DuplicateValueException exception. Also, an appropriate error message is displayed .
/ / After processing the exception , the program should be able to continue normal execution .


Which god of help , very grateful
------ Solution ---------------------------- ----------------
your data package into string type , use regular expressions to judge,

For example : String s = new Integer (1223213) toString ();.
s.replaceAll ("(.) \ \ 1 +", "#") ;/ / repeat all the figures replaced # next, determine whether they contain # , if any, reported abnormal , otherwise not reported.
boolean b = s.contains ('#') ;/ / b is true to contain , then throw an exception ,





------ Solution ------------------------------------ --------
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
class NumberNotFoundException extends Exception {
public NumberNotFoundException () {
super ();
}
public NumberNotFoundException (String s) {
super (s);
}
}
class DuplicateValueException extends Exception {
public DuplicateValueException () {
super ();
}
public DuplicateValueException (String s) {
super (s);
}
}
public class Array {

public void test1 () throws NumberNotFoundException, DuplicateValueException {
try {
int [] arr = new int [10];
for (int j = 0; j byte [] b = new byte [1024];
System.out.println (" Please enter a value stored in the array : " ) ;
System.in.read (b);
String str = new String (b) trim ().;
int num = Integer.parseInt (str);

arr [j] = num;
for (int i = 0; i if (arr [i] == arr [j]) {
throw new DuplicateValueException (" data duplication " ) ;


} else {
arr [j] = num;
}
}



}
System.out.println (" Get ready! " ) ;

Scanner in = new Scanner (System.in);
System.out.println (" Find an array of type: " ) ;
int x = in.nextInt ();
for (int i = 0; i if (arr [x] == arr [i]) {
System.out.println (" Your input array with the first " + i + " a value :" + arr [i] " the same array "
+);
}
}
} catch (IOException io) {
io.printStackTrace ();
} catch (ArrayIndexOutOfBoundsException a) {
a.printStackTrace ();
System.out.println (" array out of bounds exception ." ) ;
} catch (DuplicateValueException e) {
throw new DuplicateValueException (" data duplication " ) ;
}
catch (Exception e) {
throw new NumberNotFoundException ("asd custom exception error" ) ;
} finally {
System.out.println ("finally");
}
}

public static void main (String [] args) throws DuplicateValueException {
Array aa = new Array ();
try {
aa.test1 ();
} catch (NumberNotFoundException n) {
System.err.println (n.getMessage ());
}
}
}
------ For reference only --------------------------------- ------
help Yeah, great God , help me , remind .
------ For reference only -------------------------------------- -
you this is an array , did not pay attention to see , I'm sorry , a little abrupt . . It can be : the array into a string can be judged
------ For reference only ------------------------- --------------
although made ​​out, but still thank you ha.

没有评论:

发表评论