无法打印char []数组

时间:2016-01-24 00:05:43

标签: java

我们如何打印一个char数组,这样在比较之后,我确实附加到字符串构建器然后转换为char数组。

import java.util.*;

public class duplicatesremoval {

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);
        String input=sc.next();
        String output= "";  

        char[] str=input.toCharArray();
        char[] str1=output.toCharArray();

        StringBuilder sb = new StringBuilder(64);


        for(int i=0;i<str.length-1;i++){
            for(int j=0;j<str1.length-1;j++){
                if(str[i]!=str1[j]){
                    sb.append(str);
                    sb.append(str1);

                    char[] result = sb.toString().toCharArray();
                }

            }

        }
        System.out.println(result); // error result cannot be resolved to a variable. 
        sc.close();
    }

}

我甚至尝试过使用result.toString,但它没有用。谢谢

3 个答案:

答案 0 :(得分:2)

char[] 声明初始化移到循环之外(因此具有范围)。此外,您还需要Arrays.toString(char[])(因为数组不会覆盖Object.toString()。例如,

            // char[] result = sb.toString().toCharArray();
        }
    }
}
char[] result = sb.toString().toCharArray();
System.out.println(Arrays.toString(result));
// ...

答案 1 :(得分:0)

首先关闭:

for(int i=0;i<str.length-1;i++){

不需要-1,因为你的号码小于&#39;&lt;&#39;这意味着如果数组长度为5,则不会超过4。

其次:

System.out.println(result); // error result cannot be resolved to a variable. 

编译器很烦人,因为你声明了&#39;结果&#39;在if语句中。它不会允许这样做,因为它有机会在它到达System.out时,结果不会被声明。

由于您的问题不明确,我只能修复您当前的代码,以便编译和运行。请使用此更新您的问题与工作代码,并提供前后输入。请注意,只要&#39;输出&#39;,for循环就不会改变任何内容。没有内容。

public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String input=sc.next();
        String output= "";  

        char[] str=input.toCharArray();
        char[] str1=output.toCharArray();
        String result = "";

        StringBuilder sb = new StringBuilder(64);


        for(int i=0;i<str.length;i++){
            for(int j=0;j<str1.length;j++){
                if(str[i]!=str1[j]){
                    sb.append(str);
                    sb.append(str1);

                    result = sb.toString();
                }

            }

        }
        char[] endResult = result.toCharArray();
        System.out.println(endResult); // error result cannot be resolved to a variable. Update: fixed
        sc.close();
    }

答案 2 :(得分:0)

答案可能比您想象的要简单,但首先我要解决代码中的一些基本缺陷。

System.out.println(result); // error result cannot be resolved to a variable.  

原因是你在char[] result语句的范围内声明变量if的变量,并且不能在此范围之外使用,因此如果你将它向上移动一点:

public static void main(String[] args){

    Scanner sc=new Scanner(System.in);
    String input=sc.next();
    String output= "";  //<- Important to note

    char[] str=input.toCharArray();
    char[] str1=output.toCharArray();
    char[] result

    StringBuilder sb = new StringBuilder(64);


    for(int i=0;i<str.length-1;i++){
        for(int j=0;j<str1.length-1;j++){
            if(str[i]!=str1[j]){
                sb.append(str);
                sb.append(str1);

                result = sb.toString().toCharArray(); //Moved the declaration to the method scope
            }

        }

    }
    System.out.println(result); // error result cannot be resolved to a variable. 
    sc.close();
}

是的,这已被提及,但解释不准确,所以我认为适当解决。

至于打印出char数组,你已经知道了答案:System.out.println(char[] x)
来源:System.out
当然,除非你想要它作为字符串:new String(char[] value)