在Java中,如何将字符串强制转换为字符以填充数组?
正如标题所述,我希望创建一个char数组,用户输入应存储为char。我这样做是为了创建错误代码,它可能会成为缓冲区溢出的牺牲品。完成此工作后,我将截断输入,而不会导致缓冲区溢出。但当我被要求这样做时,我很难弄清楚如何在Java中将字符串存储为char。以下是我目前掌握的代码:在Java中,如何将字符串强制转换为字符以填充数组?,java,arrays,security,Java,Arrays,Security,正如标题所述,我希望创建一个char数组,用户输入应存储为char。我这样做是为了创建错误代码,它可能会成为缓冲区溢出的牺牲品。完成此工作后,我将截断输入,而不会导致缓冲区溢出。但当我被要求这样做时,我很难弄清楚如何在Java中将字符串存储为char。以下是我目前掌握的代码: import java.util.Scanner; public class buggyCode { public buggyCode() { // TODO Auto-generated constr
import java.util.Scanner;
public class buggyCode {
public buggyCode() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner kb = new Scanner(System.in);
String input1, input2, input3, input4, input5, input6, input7, input8, input9, input10;
char[] thisArray = new char [150];
thisArray[0]=toChar(input1);
thisArray[1]=input2;
thisArray[3]=input3;
System.out.println("Enter some characters. We are going to do this for a while");
input1 = kb.nextLine();
System.out.println("Enter some characters. We are going to do this for a while");
input2 = kb.nextLine();
System.out.println("Enter some characters. We are going to do this for a while");
input3 = kb.nextLine();
System.out.println("Enter some characters. We are going to do this for a while");
input4 = kb.nextLine();
System.out.println("Enter some characters. We are going to do this for a while");
input5 = kb.nextLine();
System.out.println("Enter some characters. We are going to do this for a while");
input6 = kb.nextLine();
System.out.println("Enter some characters. We are going to do this for a while");
input7 = kb.nextLine();
System.out.println("Enter some characters. We are going to do this for a while");
input8 = kb.nextLine();
System.out.println("Enter some characters. We are going to do this for a while");
input9 = kb.nextLine();
System.out.println("Enter some characters. We are going to do this for a while");
input10 = kb.nextLine();
}
}
首先,启动程序时,这行代码将自动失败。请删除以下内容:
thisArray[0]=toChar(input1);
thisArray[1]=input2;
thisArray[3]=input3;
要获得干净的代码,可以使用string放弃,只需使用for loop
for(int i = 0; i < 10; i++)
{
System.out.println("Enter some characters. We are going to do this for a while");
thisArray[i] = kb.nextLine().toCharArray()[0]; //store the first index of string to the thisArray array
}
for(int i=0;i<10;i++)
{
System.out.println(“输入一些字符,我们将做一段时间”);
thisArray[i]=kb.nextLine().toCharArray()[0];//将字符串的第一个索引存储到thisArray数组
}
在上面,您可以看到nextLine()随后被转换为char数组,并获得第一个索引,然后存储在ur char数组中,每当您写入
inputN
超过两次,并且N
替换为一个数字时,您就知道您需要一个数组。大多数情况下,当您发现自己只需很少的更改就可以复制粘贴代码时,您知道您需要一个循环
我将以每10个索引填充数组,从150开始向下
以下是如何正确地执行此操作:
char[] input = new char[150];
for (int i = 0 ; i != 10 ; i++) {
System.out.println("Enter some characters. We are going to do this for a while.");
char[] buf = kb.nextLine().toCharArray();
// Copy the data into input
System.arraycopy(buf, 0, input, 140-(i*10), Math.min(buf.length, 10));
}
谢谢,我忘了提到的一点是,在这项任务中,我要从150开始,每隔10个索引填充数组。你知道我怎样才能把代码重新考虑进去吗?感谢you@StevenEck请看一下编辑。输入最终分散在
输入
数组中,第一个输入出现在140和150之间,第二个输入出现在130和140之间,第三个输入出现在120和130之间,依此类推。最多可复制输入中的十个字符;剩下的将被丢弃。如果我不想丢弃多余的字符并造成缓冲区溢出怎么办?我是否要删除char[]buf行?@StevenEck然后您可能需要对索引进行更多操作:不要对起始位置使用140-(I*10)
表达式,而是通过从目标结束位置减去实际长度来计算。例如,如果您的目标结束位置为150,并且用户输入了15个字符,则您将在位置135开始复制。为最后一个结束位置创建一个变量,并从中减去长度。为下一次迭代保留结束位置,以便字符串端到端进入数组。注意索引超出范围的异常!这些是我希望在buggyCode上得到的例外,然后我将使用您帮助我的fixedCode和buggyCode。再次感谢!