为什么我会得到“变量未初始化”的错误?

huangapple go评论78阅读模式
英文:

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(&quot;Hello World&quot;);
Scanner sc=new Scanner(System.in);
int n,l=0;       //as you can see, n is initialized
String x=&quot;&quot;;
char[][] arr1=new char[10][10];
if(sc.hasNextInt())
n=sc.nextInt();
for(int i=0;i&lt;n;i++){
if(sc.hasNextLine())
x=sc.nextLine();
//char[] arr=x.toCharArray();
if(x.length()==n){
for(int j=l;j&lt;arr1.length;j++){
for(int k=0;k&lt;arr1.length;k++){
arr1[j][k]=x.charAt(i);
}
x=&quot;&quot;;
++l;
break;
}
}
}
System.out.println(arr1);
}
}

Error:

error: variable n might not have been initialized
for(int i=0;i&lt;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)), &quot;&quot;);

答案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(&quot;&quot;, // empty accumulator
(s1, s2) -&gt; s1 + (s1.isEmpty() ? s2 : s2.replaceAll(&quot;[&quot; + s1 + &quot;]&quot;, &quot;&quot;))
);
} 

Tests:

System.out.println(simplify(&quot;abcdef&quot;, &quot;fghij&quot;));
System.out.println(simplify(&quot;abcdef&quot;, &quot;fghij&quot;, &quot; jklmn&quot;));

Output

Online demo

abcdefghij
abcdefghij klmn

huangapple
  • 本文由 发表于 2020年8月15日 23:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63427761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定