Rickie的软件测试学习笔记-第四周

本周预期是分析字符串转换数值类型时候发生的异常。

在C#中:

int.Parse(String str): 这种方法是将数字内容的字符串转换为int类型。

如果字符串的内容为Null ,则抛出ArgumentNullException异常;

如果字符串内容不是数字,则抛出FormatException异常。

使用该方法只能处理字符串的内容,而且转换后的字符串内容要在int类型的可表示范围之内。

在Java中:

java.lang.Integer.parseInt(String s, int radix) 方法解析的字符串参数s作为一个有符号整数的基数指定的第二个参数基数

以下是java.lang.Integer.parseInt()方法的声明:

public static int parseInt(String s, int radix) throws NumberFormatException

参数

  • s -- This is a String containing the integer representation to be parsed.

  • radix -- This is the radix to be used while parsing s.

返回值

此方法返回指定基数中的字符串参数表示的整数.

异常

  • NumberFormatException --如果字符串不包含一个可分析的整数.

故,在C#中出现的两种异常,在Java中都归为了

   

  NumberFormatException

下面 是Java程序实例测试:

程序界面:

                        

                              

当输入为111q,即含有非法字符q时,抛出异常

                        

当输入超过了int能表示的范围时,抛出异常

                   

当输入为null(空)时,抛出的异常

                          

当输入全部为字母的时,抛出异常

                      

输入为正确的int字符时,输出正确的parse得到的值

                            

下面是该程序所使用的Java 代码:

 1     int count=0;
 2     public void start(Stage primaryStage) {
 3         primaryStage.setTitle("Form");
 4         AnchorPane root = new AnchorPane();
 5         HBox hbox1 = new HBox(8);
 6         final TextField textfield = new TextField();
 7         textfield.setPrefColumnCount(25);
 8         textfield.setPrefWidth(180);
 9         hbox1.setAlignment(Pos.CENTER_LEFT);
10         hbox1.getChildren().addAll(new Label("输入: "), textfield);
11         HBox hbox2 = new HBox(20);
12         Button btn = new Button();
13         btn.setText("确定");
14         final Text text1 = new Text();
15         final Text text2 = new Text();
16         btn.addEventHandler(MouseEvent.MOUSE_CLICKED,
17                 new EventHandler<MouseEvent>() {
18                     public void handle(MouseEvent event) {
19                         text1.setText(null);
20                         text2.setText(null);
21                         String textString = textfield.getText().toString();
22                         char[] textChar = textString.toCharArray();
23                         System.out.println(textString);
24                         
25                         try
26                         {
27                             int result = Integer.parseInt(textfield.getText());
28                             text2.setText("转化结果:"+ Integer.toString(result));
29                             
30                         }
31                         catch (Exception exception)
32                         {
33                             text1.setText("输入异常
"+exception.toString());
34                         }
35                     }
36                 });
37         hbox2.getChildren().addAll(btn, text1,text2);
38         AnchorPane.setTopAnchor(hbox1, 40.0);
39         AnchorPane.setLeftAnchor(hbox1, 10.0);
40         AnchorPane.setTopAnchor(hbox2, 90.0);
41         AnchorPane.setLeftAnchor(hbox2, 20.0);
42         root.getChildren().addAll(hbox1, hbox2);
43         primaryStage.setScene(new Scene(root, 250, 120));
44         primaryStage.show();
45     }
View Code


原文地址:https://www.cnblogs.com/rickierao/p/4396571.html