3

It is not looping through the if loop in the code. The desired o/p is:

This is real project with  problems  problems are tough to solve  

My code:

public static void main(String[] args) {
    String s = "This is real project with real problems real problems are tough to solve";
    String key = "real";
    int count = 0;
    Map<Integer, String> map = new HashMap<Integer, String>();
    StringTokenizer st = new StringTokenizer(s);
    Integer v = null;
    String next;
    while (st.hasMoreElements()) {
        next = st.nextToken();
        // To assign unique key to the value in the string
        if (v == null)
            v = 1;
        else
            v++;
        map.put(v, next);
    }
    System.out.println("before: " + map);
    System.out.println(count);
    //To delete the word "real" from the string
    for (int i = 0; i <= map.size(); i++) {
        //This is not working
        if (map.containsValue("real") && count >= 1) {
            //To delete value in the hashMap with ""
            next = "";
        }
        count++;
    }

    System.out.println("After deleting: " + map );
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
user123
  • 243
  • 4
  • 10
  • What output are you getting? – shmosel May 16 '16 at 19:37
  • I am getting the same o/p before performing the for loop. before: {1=This, 2=is, 3=real, 4=project, 5=with, 6=real, 7=problems, 8=real, 9=problems, 10=are, 11=tough, 12=to, 13=solve} – user123 May 16 '16 at 19:39
  • 3
    What exactly do you think `next = "";` is going to accomplish? – shmosel May 16 '16 at 19:41
  • I think it will remove the word "real" in the map after its first occurrence.!! – user123 May 16 '16 at 19:42
  • 2
    Evidently. But why would it do that? – shmosel May 16 '16 at 19:42
  • Here my count is 3 and I am writing if (map.containsValue("real") && count >= 1).. So, if map contains the word "real" in its value I thought it would delete it. Correct me if I am wrong. Please let me know if u know how to resolve it. – user123 May 16 '16 at 19:44
  • "I thought it would delete it" but, what makes you think that setting variable `name` to `""` should affect map (its structure or content) in any way. [Java is not pass-by-reference but pass-by-value](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Pshemo May 16 '16 at 19:46

2 Answers2

2

Let's keep it simple:

String s = "This is real project with real problems real problems are tough to solve";
String key = "real";
// Get the index of the first occurrence
int index = s.indexOf(key);
StringBuilder buffer = new StringBuilder(s.length());
// Adds to the first part of the string
buffer.append(s.substring(0, index + key.length()));
// Adds to the second part of the string from which we removed the next occurences
buffer.append(s.substring(index + key.length()).replace(key, ""));
System.out.println(buffer);

Output:

This is real project with  problems  problems are tough to solve
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
0

I have no idea what you're trying to do in your loop. But if I've understood your requirements, this should do it:

for (int i = 1; i <= map.size(); i++) {
    if ("real".equals(map.get(i)) && count++ >= 1) {
        map.remove(i);
    }
}

Output:

After deleting: {1=This, 2=is, 3=real, 4=project, 5=with, 7=problems, 9=problems, 10=are, 11=tough, 12=to, 13=solve}
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • but I want the first real to be there. This code deletes all the "real" 's in the string – user123 May 16 '16 at 19:51
  • @sai did you test it? – shmosel May 16 '16 at 19:52
  • yeah. I tested it .. It worked but this removes the first occurred "real" in the map also but I want the first occurred "real" and the next occurrences must be removed. – user123 May 16 '16 at 19:55
  • @sai, no it doesn't. Look at the output I posted. – shmosel May 16 '16 at 19:56
  • yeah. I don't know y i am not getting the correct o/p. – user123 May 16 '16 at 19:58
  • Map map = new HashMap(); StringTokenizer st = new StringTokenizer(s); Integer v = null; String next; while (st.hasMoreElements()) { next = st.nextToken(); if (v == null) v = 1; else v++; map.put(v, next); } System.out.println("before: " + map); for (int i = 1; i <= map.size(); i++) { if ("real".equals(map.get(i)) && count++ >= 1) { map.remove(i); } } System.out.println("After deleting: " + map ); – user123 May 16 '16 at 20:00
  • 1
    @sai You missed `String s = "This is real project with real problems real problems are tough to solve"; int count = 0;`. After adding I get the same output: `After deleting: {1=This, 2=is, 3=real, 4=project, 5=with, 7=problems, 9=problems, 10=are, 11=tough, 12=to, 13=solve}` – shmosel May 16 '16 at 20:03
  • Yeah. I got it..Thanku so much but Can you help me to print this o/p as a string? – user123 May 16 '16 at 20:07
  • If you have Java 8, you can do this `System.out.println(String.join(" ", map.values()));`. But I would recommend using `LinkedHashMap`, because iteration order is not guaranteed for `HashMap`. – shmosel May 16 '16 at 20:09
  • String repeated[] = null; for(String s1:map.keySet()){ if(map.get(s1).equals(biggest)) repeated[0]=s1; if(map.get(s1).equals(secondBiggest)) repeated[1]=s1; } return repeated; I would like to return two strings as a string array. how to return array of strings? i'm getting nullpointer exception. so how to overcome and how can i return my map has{real=6,to=2,world=1,hello=0} so i want to return real and to which are top two repeating elements. so how can in return them through string array – user123 May 16 '16 at 20:13