英文:
Why am I getting variable not initialized error?
问题
我试图将一个字符串中的所有字符放入一个二维字符数组中。以下是详细信息:
*我的代码:*
import java.util.*;
public class Main
{
public static void main(String[] args) {
//System.out.println("Hello World");
Scanner sc=new Scanner(System.in);
int n,l=0; //正如您所看到的,n被初始化了
String x="";
char[][] arr1=new char[10][10];
if(sc.hasNextInt())
n=sc.nextInt();
for(int i=0;i<n;i++){
if(sc.hasNextLine())
x=sc.nextLine();
//char[] arr=x.toCharArray();
if(x.length()==n){
for(int j=l;j<arr1.length;j++){
for(int k=0;k<arr1.length;k++){
arr1[j][k]=x.charAt(i);
}
x="";
++l;
break;
}
}
}
System.out.println(arr1);
}
}
*错误:*
错误:可能未初始化变量n
for(int i=0;i<n;i++){
这是什么?变量n已经被初始化了。如何修复这个问题?
英文:
I am trying to get all the characters from a string to a 2D character array. The details are given below:
My code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
//System.out.println("Hello World");
Scanner sc=new Scanner(System.in);
int n,l=0; //as you can see, n is initialized
String x="";
char[][] arr1=new char[10][10];
if(sc.hasNextInt())
n=sc.nextInt();
for(int i=0;i<n;i++){
if(sc.hasNextLine())
x=sc.nextLine();
//char[] arr=x.toCharArray();
if(x.length()==n){
for(int j=l;j<arr1.length;j++){
for(int k=0;k<arr1.length;k++){
arr1[j][k]=x.charAt(i);
}
x="";
++l;
break;
}
}
}
System.out.println(arr1);
}
}
Error:
error: variable n might not have been initialized
for(int i=0;i<n;i++){
What's this? Variable n is already initialized. How do I fix this?
答案1
得分: 4
你只能将一个 char
替换为另一个 char
,或者将一个 CharSequence
替换为另一个 CharSequence
(因为这些是 String#replace
定义的唯一重载),不能将一个 char
替换为一个 String
。通过使用 String.valueOf
将第一个参数转换为 String
(即一个 CharSequence
),可以解决这个问题。
ar[j] = ar[j].replace(String.valueOf(ar[i].charAt(k)), "");
英文:
You can only replace a char
with a char
or a CharSequence
with a CharSequence
(as those are the only overloads defined for String#replace
), not a char
with a String
. Converting the first argument to a String
(which is a CharSequence
) with String.valueOf
will solve the issue.
ar[j] = ar[j].replace(String.valueOf(ar[i].charAt(k)), "");
答案2
得分: 0
如果允许使用Java 8的流API,simplify
方法可以如下所示:
使用流的reduce
操作来连接输入数组中的字符串
使用String的replaceAll
方法与正则表达式,将之前检测到的所有字符替换为空字符串:
public static String simplify(String... arr) {
return Arrays
.stream(arr)
.reduce("", // 空累加器
(s1, s2) -> s1 + (s1.isEmpty() ? s2 : s2.replaceAll("[" + s1 + "]", ""))
);
}
测试:
System.out.println(simplify("abcdef", "fghij"));
System.out.println(simplify("abcdef", "fghij", " jklmn"));
输出
abcdefghij
abcdefghij klmn
英文:
If you are allowed to use Java 8 stream API, method simplify
may be presented as follows:
- using Stream
reduce
operation to join strings in the input array - using String
replaceAll
method with a regexp replacing all previously detected characters with empty literal:
public static String simplify(String... arr) {
return Arrays
.stream(arr)
.reduce("", // empty accumulator
(s1, s2) -> s1 + (s1.isEmpty() ? s2 : s2.replaceAll("[" + s1 + "]", ""))
);
}
Tests:
System.out.println(simplify("abcdef", "fghij"));
System.out.println(simplify("abcdef", "fghij", " jklmn"));
Output
abcdefghij
abcdefghij klmn
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论