Java例外机制的有关问题

Java例外机制的问题

1.你知道怎么判断一个字符串是不是整数?

有三种方法解决以上问题

1.使用Character.isDigit(char)判断

String str = "123sad" ;

if(!"".equals(str)){ //字符串不为空

 char num[] = str.toCharArray();//把字符串转换为字符数组

 StringBuffer a = new StringBuffer();//使用StringBuffer类,把非数字放到a中

 StringBuffer b = new StringBuffer();//把数字放到b中

 for(int i=0;i<num.length;i++)

{

 //判断输入的数字是否为数字还是字符

 if(Character.isDigit(num[i])){

//调用Character.isDigit(char)方法判断是否是数字,是返回True,否则False

b.append(num[i]) ;//如果是数字,就把它赋给b.

}else {

  a.append(num[i]) ;//如果是字符,就把它赋给a.

}

}

}

 

 

2.使用类型转换判断

try{

String str ="12312sadfdf" ;

int num = Integer.valueOf(str);//把字符串强制转换为数字

return  true ;

}catch(Exception e)

{

 return false ;//如果抛出异常

}

 

 

3.用正则表达式判断

 String str = "" ;

 boolean isNum = str.matches("[0-9]+") ;

//表示1个或多个(如"3"或"225"),*表示0个或多个([0-9]*)(如""或"1"或"22"),?表示0个或1个([0-9]?)(如""或"7")

 

ps:上述方法只能判断是否是正整数

 

 

 

2.你知道怎么判断一个字符串是不是负整数?

看一个题,就会明白:Java例外机制的问题

 

总时间限制:
1000ms
内存限制:
65536kB
描述
现有多组输入数据,每组数据占两行。
如果第一行是一个整数(假设值为n),则创建一个大小为n的整数数组。
其第二行数据是个整数(假设值为index),输出数组中下标为index的数值。
如果第一行不是一个整数,则把第一行数据整个作为一个字符串;
其第二行数据也是整数(假设值为index),输出字符串中下标为index处的字符。
请通过Java的Exception机制去判断各种例外情况,
在例外发生时(如n为负数,index越界,第一行字符串不是整数),
输出相应例外类的类名。对应每组数据,有一行输出。
输入
输入为多行,每两行数据为一组。
对每组数据,第一行可能是一个整数,也可能是一个字符串,第二行数据一定是一个整数。整数的取值范围任意
输出
输出也为多行,对应一组数据有一行输出。
或者输出正确的结果,或者输出被抛出的例外的类名。
样例输入
123
256
Hello, how are you?
65
-1
45
23
21
Haha, you're wrong!
3
样例输出
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
NegativeArraySizeException
0
h//其实这个是a,不是h,希望大家不会犯错
解决方案一: Java
import java.util.Scanner;

public class Main{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
   new Main().test() ;
	}
    public void test()
    {
    	Scanner sc = new Scanner(System.in) ;
    	String str;
    	int index;
    	while(sc.hasNext()){
    		str = sc.nextLine() ;
       		index = sc.nextInt()  ; 
       		boolean isNum =  change(str) ;
    	 if(isNum)
    	{
    		int n = Integer.parseInt(str) ;
    		if(n<0){
    		System.out.println("NegativeArraySizeException") ;
    		}else{
    	    int h[] = new int[n] ;
       		if(index<0||index>h.length-1)
    		{
    			System.out.println("ArrayIndexOutOfBoundsException") ;	
    		}else
    		{
    			System.out.println(h[index]) ;
    		} 
       		}
    	}else
    	{   
       		String n = str ;
    		if(index<0||index>n.length()-1)
    		{
    			System.out.println("StringIndexOutOfBoundsException") ;
    		}else
    		{
    			System.out.println(n.charAt(index)) ;
    		}
    	}
    	 if(sc.hasNext())
       	  {sc.nextLine();}
    	 else{
    		 break ;
    	 }
        }
    	
    }
/**
*判断字符串是否是整数
*/
    public boolean change(String str)
    {   int k=0;
    if(str.equals("")) return false ;
    	 char num[] = str.toCharArray();//把字符串转换为字符数组
    	   for (int i = 0; i < num.length; i++) {
          	// 判断输入的数字是否为数字还是字符
    		   if((i==0&&num[i]=='0'&&num.length>1)||(num[0]=='-'&&num[1]=='0'))
    		   {	    
    	    	    return false ;
    	   }else if (Character.isDigit(num[i])) {
    	        k++ ;
    	    }
    	    if(i==0&&num[i]=='-'&&num.length>1)
    	    {
    	    	k++ ;
    	    }
    	   }    
        if(k==num.length) {return true ;}
        else {return false;}
      }
}
解决方案二:   Java 
  
package dsa;

import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cinScanner = new Scanner(System.in);
		PrintWriter coutPrintWriter = new PrintWriter(System.out);
		
		while(cinScanner.hasNext()) {
			String string = cinScanner.nextLine();
			String string1 = cinScanner.nextLine();
			try {
				int n = Integer.parseInt(string);
				try {
					int a[] = new int[n];
					try {
						int m = Integer.parseInt(string1);
						coutPrintWriter.println(a[m]);
					} catch (Exception ex) {
						coutPrintWriter.println(ex.getClass().getSimpleName());
					}
				} catch (Exception e) {
					coutPrintWriter.println(e.getClass().getSimpleName());
				}
			} catch (Exception e) {
				try {
					int m = Integer.parseInt(string1);
					coutPrintWriter.println(string.charAt(m));
				} catch (Exception ex) {
					coutPrintWriter.println(ex.getClass().getSimpleName());
				}
			}
		}
		
		cinScanner.close();
		coutPrintWriter.close();
	}
}