【问题标题】:"I am not able to catch NumberFormatException properly, because of this my onClick is not working“我无法正确捕获 NumberFormatException,因此我的 onClick 无法正常工作
【发布时间】:2019-06-29 18:13:03
【问题描述】:

我试图构建一个简单的应用程序,它将纯文本和无行作为输入,并使用围栏加密技术将纯文本转换为密文。我没有从用户输入任何行,并通过强制转换将该字符串输入转换为整数。当我这样做时,它显示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);
}

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

【问题讨论】:

  • 您在 onCreate() 方法中获取 plntxt 和 noOfLines 的值,甚至还没有显示组件,因此用户没有机会输入任何内容。这些值可能是空字符串,因此 parseInt() 将失败,因为空字符串不是有效的数字格式。
  • 请添加您的日志猫

标签: java android exception encryption casting


【解决方案1】:

我没有从用户输入任何行,并通过强制转换将该字符串输入转换为整数。当我这样做时,它显示 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);
    }
});

【讨论】:

  • 很高兴它能帮助你:D
猜你喜欢
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
相关资源
最近更新 更多