英文:
Unable to get method to access characters in a String
问题
我正在尝试在Android Studio上制作一个凯撒密码应用程序。我已经准备好了XML文件和大多数其他的代码,但是在代码的某个点上,我无法继续访问字符串的每个元素。我尝试过使用charAt()
函数。
另外,我只想要在数组中的特定位置增加指定数量的字符,我这么说是因为我注意到在'z'
之后会出现特殊字符如'|'
,而这对我来说是可以接受的。这是我的第一个应用程序,我真的需要一些帮助。错误出现在第47行和第61行。
以下是我的代码:
package com.example.encrypt;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity
{
EditText input;
EditText output;
EditText num;
String inp;
String out;
int choice;
Character c;
Button enc, dec;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
output= (EditText) findViewById(R.id.output);
num = (EditText) findViewById(R.id.num);
enc = (Button) findViewById(R.id.enc);
dec = (Button) findViewById(R.id.dec);
enc.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
inp=input.getText().toString();
choice= Integer.parseInt(num.getText().toString());
for(int i=0;i<input.length();i++)
{
inp.charAt(i)= (char) (inp.charAt(i)+choice);
}
}
});
dec.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v1)
{
inp=input.getText().toString();
choice= Integer.parseInt(num.getText().toString());
for(int i=0;i<input.length();i++)
{
inp.charAt(i)= (char) (inp.charAt(i)-choice);
}
}
});
}
}
英文:
I am trying to make a Caesar Cipher app on Android Studio. I have the XML file and most of the other code ready but was unable to continue past the point in the code where I must access each element of the string. I have tried using the charAt()
function.
Also I just want the character at the point in the array to increase by the number specified, I say this because I saw that after 'z' special characters like '|' appear and that is fine by me. This is my first app and could really use some help. The error is at line 47 and 61.
Here is my code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity
{
EditText input;
EditText output;
EditText num;
String inp;
String out;
int choice;
Character c;
Button enc, dec;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
output= (EditText) findViewById(R.id.output);
num = (EditText) findViewById(R.id.num);
enc = (Button) findViewById(R.id.enc);
dec = (Button) findViewById(R.id.dec);
enc.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
inp=input.getText().toString();
choice= Integer.parseInt(num.getText().toString());
for(int i=0;i<input.length();i++)
{
inp.charAt(i)= (char) (inp.charAt(i)+choice);
}
}
});
dec.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v1)
{
inp=input.getText().toString();
choice= Integer.parseInt(num.getText().toString());
for(int i=0;i<input.length();i++)
{
inp.charAt(i)= (char) (inp.charAt(i)-choice);
}
}
});
}
}
</details>
# 答案1
**得分**: 1
我相信你的第一个问题出在这里:
```java
inp.charAt(i)= (char) (inp.charAt(i)+choice);
你试图将一个值赋给一个方法的返回值。在Java中是不可能的。
在Java中,字符串是不可变的 - 如果你想要替换字符串中的一个值,你必须构建一个新的字符串,并将原来指向旧字符串的引用改为指向新字符串。
可能还有其他问题我还没有来得及解决,但我认为这是真正的问题。我不太理解你的尝试,所以很抱歉我无法提供你应该使用什么的建议。
英文:
I believe your first problem is here:
inp.charAt(i)= (char) (inp.charAt(i)+choice);
You are trying to assign a value to a method return. This is not possible in Java.
Strings are immutable in Java - if you'd like to replace a value in a String, you must build a new String and resassign the reference that points to the old String to instead point to the new String.
There may be other issues that I have not had a chance to get to, but I believe this is the real issue. I don't fully understand what you're trying to do, so unfortunately I can't make a suggestion for what you should use instead.
答案2
得分: 1
基于 @corsiKa 的回答,我建议如下。首先,将你的输入字符串转换为 char[]
:
char[] array = inp.toCharArray();
现在你可以遍历这个数组并修改它的内容:
array[i] = array[i] + choice;
最后,你可以从该数组构建输出字符串:
output.setText(new String(array));
英文:
Building on @corsiKa's answer, I suggest the following. First, convert your input string into a char[]
:
char[] array = inp.toCharArray();
Now you can iterate over the array and modify its contents:
array[i] = array[i] + choice;
And, at the end, you can build your output string from that array:
output.setText(new String(array));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论