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

标签 java android exception encryption casting

我试图构建一个简单的应用程序,它将纯文本和行数作为输入,并使用栅栏加密技术将纯文本转换为密文。我没有获取用户输入的任何行,并通过转换将该字符串输入转换为整数。当我这样做时,它显示了 NumberFormatException。我在 try block 内编写了转换行,之后该变量的范围受到限制,因此我的 encryption() 方法无法访问它。由于我的 onClick 函数未生成正确的所需密文,我该怎么办? 该按钮的行为就像从未被单击过一样。

我尝试在 try block 外部创建该变量,然后在 block 内对其进行类型转换,我还将该 lines 变量 final 设为它是在类(class)内访问的。然后它要求我初始化变量,我也这样做了,但它似乎对我没有帮助。

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);
}

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

最佳答案

I was taking no of lines input from user and convert that string input into integer by casting. As i was doing it ,it shows 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){

    }

    ...
}

I have tried creating that variable outside the try block and then typecasting it inside the block, i also made that "lines" variable final as it was accessed within the class. then it ask me to initialize the variable, i have done that also but it does not seems helping me.

你所做的会对你的代码造成更大的损害,因为你使变量值保持不变。 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);
    }
});

可以通过将所有文本 getter 移动到 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);
    }
});

关于java - “我无法正确捕获 NumberFormatException,因此我的 onClick 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56819817/

相关文章:

java - 在Linux字体问题上使用PDFBox 2.0.14将PDF转换为Tiff

java regex 如何匹配某个不是某个子字符串的字符串

JavaFX 如何通过按钮从 TableView 中获取特定列的所有值和总和?

java - 不连接数据库的Spring boot测试

flash - 如何捕获AS3程序未捕获的异常?

objective-c - 非 Mac OS X 平台是否支持 Objective-C 2.0 异常处理?

java - 如何在android中进行NFC点对点连接?

Android 选项卡布局不占用自定义 View 的全宽

android自定义 ListView 排序

c# - 使用带有异常的字典<TKey, TValue>