-4

When i give input as mama the code below prints mamaa, but i want the output to be ma only. What is wrong with is code

import java.util.*;

class Duplicate1 {

    public static void main(String args[]) {
        String str;
        Scanner s=new Scanner(System.in);
        System.out.println("enter the string");

        str=s.nextLine();
        String result="";
        char c=str.charAt(0);
        result=result+c;

        for(int i=1; i<str.length(); i++) {
            char d = str.charAt(i);
            for(int j=0; j<i+1; j++) {
                if(d != result.charAt(j)) {
                    result=result+d;
                }
            }
         }
         System.out.println(result);
    }
}
  • when i give input as "mama" it prints "mamaa" .. but i want output as "ma". what is wrong with is code .. help me!! – manoj kumar Jul 23 '16 at 13:04
  • 2
    Welcome to SO. Please read this article about how to ask a good question, then format yours accordingly. That way you will have more chances to get a helpful answer. http://stackoverflow.com/help/how-to-ask – lmazgon Jul 23 '16 at 13:07
  • 2
    Please note: never put more infos in comments, update your question instead. And please: you want us to help, you spend at least the 3 minutes it takes to **properly** format your question! – GhostCat Jul 23 '16 at 13:07
  • Should post to http://codereview.stackexchange.com/ – Yuchen Jul 23 '16 at 13:17
  • @YuchenZhong code postet on CR should be working code only, according to the OP this code is NOT working as intended and therefore is well suited here on SO – Roman Vottner Jul 23 '16 at 13:19
  • This may be on-topic for Code Review Stack Exchange, assuming **A)** the code works **and B)** it's not hypothetical or incomplete in any way – Quill Jul 23 '16 at 13:21

2 Answers2

1

Please make yourself some kind of pen & paper test which should look somethin like this:

init result = "m";

| i | j | d | c | comparison | result |
|---|---|---|---|------------|--------|
| 1 | 0 | a | m | a != m     | ma     |
| 1 | 1 | a | a | a != a     | ma     |
| 2 | 0 | m | m | m != m     | ma     |
| 2 | 1 | m | a | m != a     | mam    |
| 2 | 2 | m | m | m != m     | mam    |
| 3 | 0 | a | m | a != m     | mama   |
| 3 | 1 | a | a | a != a     | mama   |
| 3 | 2 | a | m | a != m     | mamaa  |
| 3 | 3 | a | a | a != a     | mamaa  |

and/or use a debugger which is standard in allmost any Java Editor (NetBeans, IntelliJ, Eclipse, ...) and step through your application to learn why things go wrong.

What you basically do is add the currently processed character if it does not match a character in your result string. For every non-matching character contained in the current result string a new character of d is added to the end of the result string.

Instead you should only add the character if it is not yet available in the result string. This can be done by keeping a boolean flag found (initialized with false) in the outer loop that indicates if a match was found or not and in the inner loop set only this flag to true if a match was found and keep iterating over the inner loop and after the inner loop is done check the found flag and only add the character currently stored in d if found equals false.

The more direct solution would be to check if the current result string simply contains a certain character and only add the character if it does not. This would make the inner loop completely redundant.

Roman Vottner
  • 12,213
  • 5
  • 46
  • 63
0

I can see a few bugs. But since this is clearly a homework or other learning exercise, you need to learn to find your own bugs. So here is a hint.

Hint: you are only testing for characters that are a duplicate of the first character. (Read your code again.)

Finally, here are some ideas on debugging your own code:

  1. Read the code that you have written and try to mentally execute it, a statement at a time. Will the statements that you have written actually do what you need them to do?

  2. Learn to use a debugger, and how to single step, set breakpoints and look at the values of local variables.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216