0
         for(i=0;i<m1.length();i++)
             for(j=0;j<m2.length();j++)
                 if(m1.charAt(i)==m2.charAt(j)){
                     intersection=intersection+m1.charAt(i); 
                     m2.charAt(j)=' ';



                 }

                    System.out.println(intersection);



         } while(devam==false);

    }

}

that's my code. and we are not allowed to use a method or an array, we are just beginners.my code gives an error at the point of m2.charAt(j)=' ';. i wrote that line because, when we find an intersection, we shouldn't check that element again.can you please help?

jack sparrow
  • 87
  • 1
  • 9
  • What is the error its throwing? Can you add that stacktrace? Also add your input and expected output. – Smit Jan 30 '13 at 17:45
  • 1
    You can't manipulate `String` objects, they're immutable. Hint: try using variables declared outside the `for` loops to track the position instead of setting it to a space character. – Brian Jan 30 '13 at 17:46
  • 1
    if our first multiset is 1 1 2 1 1 3 1 4 and second multiset is 1 3 5 2 1 our intersection should be 1 1 3 2. for example. – jack sparrow Jan 30 '13 at 17:48
  • 1
    `String.charAt(int i)` returns the character present at `index` i of `String`.It is not used to assign new value at index i of `String`. – Vishal K Jan 30 '13 at 17:54
  • i've forgotten. well, how can i change that character in the m2 string to ' ' ? – jack sparrow Jan 30 '13 at 17:56
  • You can use the following syntax: `m2 = m2.substring(0,i)+" "+m2.substring(i+1);` – Vishal K Jan 30 '13 at 18:00
  • Instead of using `String` you should use `StringBuilder` for efficient memory management. You can know more about `StringBuilder` class here : http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html – Vishal K Jan 30 '13 at 18:02
  • @VishalK i tried your advice, but my code gives String index out of range error. beginning of the loop is for(i =0; i – jack sparrow Jan 31 '13 at 12:59
  • This exception is coming at the last iteration of loop i.e when `i==m2.length() - 1` . To tackle this problem you should put following line instead of the previous one: `m2 = m2.substring(0,i) +" "+ (i>= m2.length()-1 ? "" : m2.substring(i+1))`. **Note** As you have told that your teacher has instructed you not to use method or an array then using `substring` method of String would be OK ? – Vishal K Jan 31 '13 at 16:52

2 Answers2

1

As Brian said, Strings in java are immutable. This means that you can't assign through a method call like m2.charAt(j)=' '.This means you have to use another way to keep track of whether you've found the character yet.

You could add it to intersection and when checking a character make sure it isn't in intersection by using intersection.indexOf(char c), if this returns -1 then it isn't in the string.

edit:

Sorry didn't put into account that the output should be a multiset. The above solves the problem if the output is a set.

You could use replaceFirst(String searchFor, String replacement) on your m2 to delete it. it would be something like:

    for( int i =0; i < m1.length(); i+=2)
    {
       if(m2.indexOf(m1.charAt(i)) != -1)
       {
          intersection = intersection + m1.charAt(1) + " ";
          m2 = m2.replaceFirst(new String(m1.charAt(i)), "");
       }
    }

So if m1 = '1 1 2 3 5' and m2 = '1 4 2 1',

first pass: looks for 1 in '1 4 2 1'

second pass: looks for 1 in '4 2 1'

third pass: looks for 2 in '4 2'

fourth pass: looks for 3 in '4'

fifth pass: looks for 5 in '4'

returning '1 1 2'

Note that that it is incrementing the variable by two to take into account spaces. This is only if we assume that the two strings are in the form 'a a a a a a', with 'a' only being a single character. If there are digits or characters that are more than a digit long then you have to skip whitespace and interpret the string in a different way, other than just looking at it at a character-by-character basis.

If we can make these assumtions, it would be wise to trim your m1 of trailing and leading whitespace using the String trim method ie m1 = m1.trim() before executing this loop.

Stephen
  • 2,365
  • 17
  • 21
  • But we want to include repetitions, what happens when we find a character that already appeared but should be considered again? – madth3 Jan 30 '13 at 18:11
  • yeah, actually this is my question. but, im gonna try the followings, i think especially m2=m2.substirng gonna work. – jack sparrow Jan 30 '13 at 18:13
  • @jacksparrow I think you will need one more loop for string operations to remove duplicates from final `intersection` String. May be bubble sort or some other sorting algorithm. – Smit Jan 30 '13 at 18:18
  • @raufio yeah, i thought about that replace method, but we are not allowed to use a method like that, unfortunately.thanks again. – jack sparrow Jan 30 '13 at 19:27
  • @jacksparrow if you dont want to use API method then you can create your own logic to replace chars in string. This will cause lil extra work but possible – Smit Jan 30 '13 at 20:33
  • but i couldn't make it, my logic has some problems :/ actually, my code now works but output is wrong. – jack sparrow Jan 31 '13 at 12:08
1

You cannot modify the content of a String with

m2.charAt(j)=' ';

Do this instead

m2 = m2.replace(m2.charAt(j), ' ');

Note that replace() will replace the first character in the string equal to the first parameter. I assume that, since the string represent a set, will not be repeated characters. If there are and you want to replace all of them, just use replaceAll() instead.

Evans
  • 1,589
  • 1
  • 14
  • 25
  • 1
    `replace(char oldChar,char newChar)` Returns a new `string` resulting from replacing all occurrences of oldChar in this string with newChar. – Vishal K Jan 30 '13 at 18:07
  • The question says clearly "multisets", repetitions are to be expected and considered. – madth3 Jan 30 '13 at 18:12