我试图构建一个简单的应用程序,它将纯文本和行数作为输入,并使用栅栏加密技术将纯文本转换为密文。我没有获取用户输入的任何行,并通过转换将该字符串输入转换为整数。当我这样做时,它显示了 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.
你所做的会对你的代码造成更大的损害,因为你使变量值保持不变。 plntxt
和 noOflines
的值始终为 ""
空字符串。因此,您的以下代码将不起作用:
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/