java中字符串到数组的转换问题
我正在编写一个函数,它接受整数字符串并将其转换为数组。然后,在数组中的每个元素上,更改值,将数组转换回字符串,然后返回字符串。此代码给出一个错误。这就是我到目前为止所做的:java中字符串到数组的转换问题,java,arrays,string,Java,Arrays,String,我正在编写一个函数,它接受整数字符串并将其转换为数组。然后,在数组中的每个元素上,更改值,将数组转换回字符串,然后返回字符串。此代码给出一个错误。这就是我到目前为止所做的: import java.util.*; public static void swap(int[] encrypted, int i, int j) { int temp = encrypted[i]; encrypted[i] = encrypted[j]; encrypted[j] = temp; } //
import java.util.*;
public static void swap(int[] encrypted, int i, int j)
{
int temp = encrypted[i]; encrypted[i] = encrypted[j]; encrypted[j] =
temp;
}
// Input string should only be 4 digits. Ex "1234"
public static String encrypt(String str)
{
int i;
int len = str.length(); // get length of string
int[] encrypted = new int[len]; // size of array
for (i = 0; i < len; i++)
{
// this should be placing integer at string position
// i, altering its value, and then placing it in
// encrypted[i]. This does not work
encrypted[i] = str.toIntArray(i) + 7 % 10;
}
// swaps elements in array
swap(encrypted, 0, 2);
swap(encrypted, 1, 3);
// should convert the array back into a string.
// Also might not work
str = String.valueOf(encrypted);
// return new string
return str;
}
import java.util.*;
公共静态无效交换(int[]加密,int i,int j)
{
int temp=加密的[i];加密的[i]=加密的[j];加密的[j]=
临时雇员
}
//输入字符串应仅为4位数字。前“1234”
公共静态字符串加密(字符串str)
{
int i;
int len=str.length();//获取字符串的长度
int[]encrypted=new int[len];//数组大小
对于(i=0;i
给定输入字符串“1234”,我希望输出为0189。
1 + 7 % 10 = 8... 等等
交换索引0和2、1和3。
我仍然不习惯所有的Java命令,因为我还在学习。如果有什么事情我做得不对,请告诉我。而不是
encrypted[i] = str.toIntArray(i) + 7 % 10;
试一试
1。
String
没有任何名为toIntArray
的方法
替换
for (i = 0; i < len; i++)
{
// this should be placing integer at string position
// i, altering its value, and then placing it in
// encrypted[i]. This does not work
encrypted[i] = str.toIntArray(i) + 7 % 10;
}
输出:
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(encrypt("12345678"));
}
public static String encrypt(String str) {
int i;
int len = str.length();
int[] encrypted = new int[len];
for (i = 0; i < len; i++) {
encrypted[i] = Character.getNumericValue(str.charAt(i)) + 7 % 10;
}
if (encrypted.length > 3) {
swap(encrypted, 0, 3);
}
if (encrypted.length > 4) {
swap(encrypted, 1, 4);
}
// Convert the array back into a string.
str = Arrays.stream(encrypted).boxed().map(String::valueOf).collect(Collectors.joining());
// Return new string
return str;
}
static void swap(int[] arr, int x, int y) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
11121089131415
String
上没有toIntArray
方法。您的swap
方法在哪里定义?最后,你有什么实际问题?请发布编译的代码,并询问有关其行为的特定问题,或者如果您无法使其编译,请发布编译器错误消息。请看。是否有可比较的整数数组的ToCharray?我刚刚发现了交换错误。长度始终为四,因此应为交换(0,2)和交换(1,3)。我尝试了您的编辑,输入字符串为1234。我得到的结果是[I@4a574795.我希望输出是0189.1+7%10=8…等等。然后在位置0和2,以及1和3交换数组。我编辑了我的代码以使其可编译。如果您能提供帮助,我将不胜感激。Thanks@idkusrname126-你确定你试过我更新的答案吗?我已经测试过了,还发布了一个测试样本。请随意如果有任何疑问/问题,请发表评论。刚刚看到了更新的答案。将数组转换回字符串时,我出现了一个错误“str=Arrays.stream(encrypted).boxed().map(string::valueOf).collect(Collectors.joining());^symbol:变量收集器位置:类问题1错误您正在使用的Java版本是什么?您可以使用终端窗口或命令提示符上的命令,Java-version
获得它。版本13.0.2上的Im我用输入字符串b尝试了您的答案我得到的结果是[I@4a574795.我希望输出为0189。1+7%10=8…等等。然后在位置0和2,以及1和3交换数组。我编辑了我的代码以使其可编译。如果您能提供帮助,我将不胜感激。谢谢
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(encrypt("12345678"));
}
public static String encrypt(String str) {
int i;
int len = str.length();
int[] encrypted = new int[len];
for (i = 0; i < len; i++) {
encrypted[i] = Character.getNumericValue(str.charAt(i)) + 7 % 10;
}
if (encrypted.length > 3) {
swap(encrypted, 0, 3);
}
if (encrypted.length > 4) {
swap(encrypted, 1, 4);
}
// Convert the array back into a string.
str = Arrays.stream(encrypted).boxed().map(String::valueOf).collect(Collectors.joining());
// Return new string
return str;
}
static void swap(int[] arr, int x, int y) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
11121089131415