“我无法正确捕获NumberFormatException,因此我的onClick无法正常工作

时间:2019-06-29 18:13:03

标签: java android exception encryption casting

我正在尝试构建一个简单的应用程序,该应用程序将纯文本和无行作为输入,并使用Rail fence加密技术将纯文本转换为密文。我没有从用户输入任何行,而是通过强制转换将字符串输入转换为整数。在执行此操作时,它显示了NumberFormatException。我在try块内编写了强制转换行,之后限制了该变量的范围,使得我的encryption()方法无法访问它。我的onClick函数未生成正确的所需密文,该怎么办? 该按钮的行为就像从未单击过一样。

我尝试在try块外创建该变量,然后在该块内进行类型转换,我还使lines变量final在类中被访问。然后它要求我初始化变量,我也这样做了,但似乎对我没有帮助。

Button decryptBtn, encryptBtn;
TextView hlWrld, encryptedText;
EditText noOfLines, plainText;
int lines;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    decryptBtn = findViewById(R.id.decryptBtn);
    encryptBtn = findViewById(R.id.encrptBtn);
    hlWrld = findViewById(R.id.hlwWorld);
    encryptedText = findViewById(R.id.encryptedText);
    noOfLines = findViewById(R.id.lineNo);
    plainText = findViewById(R.id.plntxt);

    final String plntxt = plainText.getText().toString();
    final String noOflines = noOfLines.getText().toString();
    int lines = 0;
    try {
        lines = Integer.parseInt(noOflines);
    } catch (NumberFormatException e) {

    }

    final int finalLines = lines;

    encryptBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            encryption(plntxt, finalLines);
        }
    });
}

public void encryption(String plntxt, int lines) {
    boolean checkdown = false;  // check whether it is moving downward or upward
    int j = 0;
    int row = lines;                  // no of row is the no of rails entered by user
    int col = plntxt.length();             //column length is the size of string
    char[][] a = new char[row][col];
    // we create a matrix of a of row *col size

    for (int i = 0; i < col; i++) {  // matrix visiting in rails order and putting the character of plaintext
        if (j == 0 || j == row - 1)
            checkdown = !checkdown;

        a[j][i] = plntxt.charAt(i);

        if (checkdown) {
            j++;
        } else {
            j--;
        }
    }

    // visiting the matrix in usual order to get ciphertext
    for (int i = 0; i < row; i++) {
        for (int k = 0; k < col; k++) {
            System.out.print(a[i][k] + "  ");
        }
        System.out.println();
    }

    String en = "";

    System.out.println("----------------------");
    for (int i = 0; i < row; i++) {
        for (int k = 0; k < col; k++) {
            if (a[i][k] != 0)
                en = en + a[i][k];
        }
    }

    System.out.println(en); // printing the ciphertext
    encryptedText.setText(en);
}

我希望输出是密文,这是由于我在setText()上应用了textView方法的结果。但是,什么都没有发生。

1 个答案:

答案 0 :(得分:0)

  

我没有从用户那里输入任何行,而是通过强制转换将该字符串输入转换为整数。当我这样做时,它显示NumberFormatException。

这是因为您正尝试从EditText读取一个字符串,该字符串尚无输入,但整数不是有效数字,使用以下代码(请参见注释):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // your view binding code with findViewById
    // ...

    // here you're trying to read the EditText value,
    // but no user input yet because you've only inflate it before.
    final String plntxt= plainText.getText().toString();
    final String noOflines= noOfLines.getText().toString();
    // the noOfLines is "", an empty string.
    int lines = 0;
    try {
        // this raise an exception because empty string is not number.
        lines = Integer.parseInt(noOflines);
    }
    catch (NumberFormatException e){

    }

    ...
}
  

我尝试在try块外创建该变量,然后在该块内进行类型转换,我还将该“行”变量定为final,因为在类内对其进行了访问。然后它要求我初始化变量,我也这样做了,但似乎并没有帮助我。

您所做的是对代码的破坏更大,因为您使变量值保持不变。 plntxtnoOflines的值都将始终是一个""空字符串。因此,您的以下代码将无效:

final String plntxt= plainText.getText().toString();
final String noOflines= noOfLines.getText().toString();

...

final int finalLines = lines;
encryptBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // this won't work because plntxt is always empty string
        // and finalLines is always invalid number.
        encryption(plntxt, finalLines);
    }
});

可以通过将所有文本获取器移至onClick方法的内部来完成简单的修复:

encryptBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      String plntxt= plainText.getText().toString();
      String noOflines= noOfLines.getText().toString();
      int lines = 0;
      try {
        lines = Integer.parseInt(noOflines);
      }
       catch (NumberFormatException e){

      }

      encryption(plntxt, finalLines);
    }
});