java将字母转换为数字,然后将所有值放入数组中
我在想办法把字母转换成数字,然后按n位数分割 例如,我有一句话:明天开始 然后,我将其转换为ASCII格式的数字。因此,转换编号必须为:java将字母转换为数字,然后将所有值放入数组中,java,arrays,Java,Arrays,我在想办法把字母转换成数字,然后按n位数分割 例如,我有一句话:明天开始 然后,我将其转换为ASCII格式的数字。因此,转换编号必须为:84797779827987326669717378 之后,所有数字将转换为n位,并将其放入一维数组中 假设每个区块的数字被分成3位数。结果必须是:847 977 798 282 798 732 666 971 737 8 它们都在数组中:arr[0]=847,arr[1]=977,arr[2]=798,….arr[n] 我已经试着用java解决它了。但到目前为
84797779827987326669717378
之后,所有数字将转换为n位,并将其放入一维数组中
假设每个区块的数字被分成3位数。结果必须是:847 977 798 282 798 732 666 971 737 8
它们都在数组中:arr[0]=847,arr[1]=977,arr[2]=798,….arr[n]
我已经试着用java解决它了。但到目前为止,我只能将字符串转换为数字
以下是我尝试过的部分代码:
String words = "TOMORROW BEGIN";
int sa;
char c;
for(int i = 0; i < words.length(); i++){
c = words.charAt(i);
sa = (int) c;
String kt = Integer.toString(sa);
System.out.print(kt);
}
String words=“明天开始”;
int sa;
字符c;
对于(inti=0;i
嗯,它成功地转换成了号码:847977798279873266669717378
但是,我搞不清楚如何将数字分成n位数的块(假设每个块3位数),然后将所有块的值放入数组中
要解决这个问题,我必须做些什么?一个解决方案是每三个字符取一个子字符串
,然后使用Integer.parseInt()
将每个子字符串转换回来
公共类主{
公共静态void main(字符串[]args){
字符串输入=“84797779827987326669717378”;
int n=3;
int[]输出=新的int[(input.length()+n-1)/n];
for(int i=0;i
一种解决方案是每三个字符取一个子字符串,然后使用Integer.parseInt()
将每个子字符串转换回来
公共类主{
公共静态void main(字符串[]args){
字符串输入=“84797779827987326669717378”;
int n=3;
int[]输出=新的int[(input.length()+n-1)/n];
for(int i=0;i
您可以使用substring方法拆分字符串
例如:
for(int start=0;start
您可以使用substring方法拆分字符串
例如:
for(int start=0;start
您可以这样尝试:
public static void main(String[] args) {
String words = "TOMORROW BEGIN";
StringBuilder builder = new StringBuilder();
for (int idx = 0; idx < words.length(); idx++) {
builder.append((int) words.charAt(idx));
}
int[] ints = new int[(builder.length() + 2) / 3];
for (int idx = 0; idx < ints.length; idx++) {
int from = idx * 3;
int to = Math.min(from + 3, builder.length());
ints[idx] = Integer.parseInt(builder.substring(from, to));
}
System.out.println(builder);
System.out.println(Arrays.toString(ints));
}
您可以这样尝试:
public static void main(String[] args) {
String words = "TOMORROW BEGIN";
StringBuilder builder = new StringBuilder();
for (int idx = 0; idx < words.length(); idx++) {
builder.append((int) words.charAt(idx));
}
int[] ints = new int[(builder.length() + 2) / 3];
for (int idx = 0; idx < ints.length; idx++) {
int from = idx * 3;
int to = Math.min(from + 3, builder.length());
ints[idx] = Integer.parseInt(builder.substring(from, to));
}
System.out.println(builder);
System.out.println(Arrays.toString(ints));
}
将ASCII连接到字符串或StringBuffer,然后在其中循环
根据您的代码:
String words = "TOMORROW BEGIN";
int sa;
char c;
StringBuffer asciiBuffer = new StringBuffer();
for(int i = 0; i < words.length(); i++){
c = words.charAt(i);
sa = (int) c;
asciiBuffer.append(Integer.toString(sa));
}
System.out.println("\nBuffer: " + asciiBuffer);
int from = 0;
int arrayStringLength = 3;
int bufferLength = asciiBuffer.length();
int arraySize = bufferLength / arrayStringLength + 1;
CharSequence[] result = new CharSequence[arraySize];
int i=0;
while(from < bufferLength){
result[i] = asciiBuffer.subSequence(from, Math.min(from+arrayStringLength, bufferLength));
from = from+arrayStringLength;
i++;
}
System.out.println(Arrays.toString(result));
String words=“明天开始”;
int sa;
字符c;
StringBuffer asciiBuffer=新的StringBuffer();
对于(inti=0;i
输出:
[847, 977, 798, 282, 798, 732, 666, 971, 737, 8]
[84, 79, 77, 79, 82, 82, 79, 87, 32, 66, 69, 71, 73, 78]
缓冲区:8479777982798732669717378
[847,977,798,282,798,732,666,971,737,8]
看
编辑:与整数数组中的其他答案存储结果相比,0在CharSequence数组中是守恒的。以“TPMORROW BFGIN”作为条目的整数解决方案的潜在问题示例:
缓冲区:84807779827987326670717378
[8480779828279873276771737,8]
将ASCII连接到字符串或StringBuffer,然后在其中循环
根据您的代码:
String words = "TOMORROW BEGIN";
int sa;
char c;
StringBuffer asciiBuffer = new StringBuffer();
for(int i = 0; i < words.length(); i++){
c = words.charAt(i);
sa = (int) c;
asciiBuffer.append(Integer.toString(sa));
}
System.out.println("\nBuffer: " + asciiBuffer);
int from = 0;
int arrayStringLength = 3;
int bufferLength = asciiBuffer.length();
int arraySize = bufferLength / arrayStringLength + 1;
CharSequence[] result = new CharSequence[arraySize];
int i=0;
while(from < bufferLength){
result[i] = asciiBuffer.subSequence(from, Math.min(from+arrayStringLength, bufferLength));
from = from+arrayStringLength;
i++;
}
System.out.println(Arrays.toString(result));
String words=“明天开始”;
int sa;
字符c;
StringBuffer asciiBuffer=新的StringBuffer();
对于(inti=0;i
输出:
[847, 977, 798, 282, 798, 732, 666, 971, 737, 8]
[84, 79, 77, 79, 82, 82, 79, 87, 32, 66, 69, 71, 73, 78]
缓冲区:8479777982798732669717378
[847,977,798,282,798,732,666,971,737,8]
看
编辑:与整数数组中的其他答案存储结果相比,0在CharSequence数组中是守恒的。以“TPMORROW BFGIN”作为条目的整数解决方案的潜在问题示例:
缓冲区:84807779827987326670717378
[8480779828279873276771737,8]
我使用了您的代码并添加了几行。这应该起作用:
public static void main(String[] args) {
String words = "TOMORROW BEGIN";
int sa;
int n = 3; // put any value for n
char c;
int[] array;
String kt = "";
for(int i = 0; i < words.length(); i++){
c = words.charAt(i);
sa = (int) c;
kt += sa;
}
System.out.println(kt);
int nbPartitions = kt.length() / n + kt.length() % n;
array = new int[nbPartitions];
for(int i = 0; i < nbPartitions; i++){
int begin = i * n;
int end = (i + 1) * n;
if(end > kt.length()){
array[i] = Integer.parseInt(kt.substring(begin, kt.length()));
break;
}
array[i] = Integer.parseInt(kt.substring(begin, end));
}
System.out.println(Arrays.toString(array));
}
publicstaticvoidmain(字符串[]args){
String words=“明天开始”;
int sa;
int n=3;//为n输入任何值
字符c;
int[]数组;
字符串kt=“”;
对于(inti=0;iSystem.out.println(Arrays.toString(splitString(str, 2)));
[84, 79, 77, 79, 82, 82, 79, 87, 32, 66, 69, 71, 73, 78]