2

how can we print a char array such that after comparison, I did append to string builder and then converting to char array.

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();
    }

}

I did even tried using result.toString but it didn't work. Thank you

Aaron
  • 24,009
  • 2
  • 33
  • 57
Sneha
  • 21
  • 1

3 Answers3

2

Move the char[] declaration and initialization outside the loop (so that is has scope). Also, you'll want Arrays.toString(char[]) (because arrays don't override Object.toString(). Something like,

            // char[] result = sb.toString().toCharArray();
        }
    }
}
char[] result = sb.toString().toCharArray();
System.out.println(Arrays.toString(result));
// ...
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

First off:

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

The -1 is not needed as you have a less than sign '<'. This means if the array length is 5, it won't go beyond 4.

Secondly:

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

The compiler is fussing because you have declared 'result' inside an if statement. It won't allow this as there is a chance in its mind that by the time it reaches the System.out, result won't have been declared.

Due to your question not being clear, I can only fix your current code so that it will compile and run. Please use this to update your question with working code, and provide a before and after input. Note that the for loop will change nothing so long as 'output' has no content.

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();
    }
z7r1k3
  • 718
  • 1
  • 5
  • 20
  • Thank you. This was my problem statement - To remove the duplicate characters in a string without using any additional buffer . No extra copy of array. I am unable to get the expected output. – Sneha Jan 24 '16 at 02:28
  • I was able to get output using LinkedHashSet but still this program is not executing. – Sneha Jan 24 '16 at 02:41
  • You're welcome! Just a tip for future questions, make sure you include the desired output and what you entered to get it :) – z7r1k3 Jan 24 '16 at 02:45
0

The answer may be simpler than you think, but first I want to address some fundamental flaws in your code.

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

The reason for this is your decleration of the variable char[] result is declared in the scope of the if statement and cannot be used outside this scope, therefor if you move it up a bit:

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();
}

Yes yes, this have been mentioned but the explanation was inaccurate so I felt it appropriate to address.

As for printing out the char array, you already know the answer: System.out.println(char[] x)
Source: System.out
Unless of course you want it as a String: new String(char[] value)

Simon Jensen
  • 488
  • 1
  • 3
  • 19
  • No, I wasn't able to print the character array by System.out.println(char[] x). It throws an error. – Sneha Jan 24 '16 at 02:39
  • As mentioned, it cant see the char array `result` due to it being declared in a nested scope, once the program leaves that scope, it forgets the value, try to copy the code example I provided and see if that works. – Simon Jensen Jan 24 '16 at 02:45