0

So I have a char variable called "temp" which I'd like to compare to the element stored in "X" CharArray[X][Y] while I'm in a third for loop after the 2D array.

For example:

char temp;
temp = ' ';
String end;
end = "";
for (int i = 0; i < CharArray.length; i++){
        for (int m = 0; m < 2; m++){
            if (somethingY){
                if (somethingZ){
                    for (int j = 0; j < something.length; j++){
                        //something
                        temp = somethingX;
                        if (temp == String.valueOf(CharArray[i][m]).charAt(0)){
                            end = String.valueOf(CharArray[i][m]);
                            System.out.print(end);
                        }
                    }
                }
            }
        }
    }

I've tried printing "temp" where it says "temp = somethingX" and it prints just fine. But when I try to save the String into a String variable, it will not print the variable called "end".

According to this, it won't do anything if the object is something else, but "end" is a String.

So, what am I doing wrong?

EDIT: In case there's a confusion, "I'm trying to print "end", but I figured if temp == String.valueOf(CharArray[i][m]).charAt(0) is correct, so should "end"'s part.".

EDIT2: Defined "temp" for people...

EDIT3: I tried "end.equals(String.valueOf(CharArray[i][m]));", but still nothing happens when I try to print it. I get no errors nor anything.

EDIT4: I tried putting String.valueOf(CharArray[i][m]).charAt(0) into a another variable called "temp2" and doing if (temp == temp2), but still the same thing.

EDIT5: I tried temp == CharArray[0][m] and then end = CharArray[0][m], but still nothing prints.

EDIT6: OK. Sense this will never get resolved, I'll just say the whole point of my problem. -> I have an ArrayList where each line is a combination of a letter, space and a number (e.g. "E 3"). I need to check if a letter is repeating and if it is, I need to sum the numbers from all repeating letters.

For example, if I have the following ArrayList:

Z 3
O 9
I 1
J 7
Z 7
K 2
O 2
I 8
K 8
J 1

I need the output to be:

Z 10
O 11
I 9
J 8
K 10

I didn't want people to do the whole thing for me, but it seems I've no choice, since I've wasted 2 days on this problem and I'm running out of time.

Community
  • 1
  • 1
Doombringer
  • 596
  • 4
  • 19

1 Answers1

1

Use a map :

ArrayList<String> input=new ArrayList<String>();
input.add("O 2");
input.add("O 2");
Map<String, Integer> map= new HashMap<String, Integer>();
for (String s:input) {
     String[] splitted=s.split(" ");
     String letter=splitted[0];
     Integer number=Integer.parseInt(splitted[1]);
     Integer num=map.get(letter);
     if (num==null) {
         map.put(letter,number);
     }
     else {
         map.put(letter,number+num);
     }
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
     System.out.println(entry.getKey() + " " + Integer.toString(entry.getValue()));
}

Without using a map :

ArrayList<String> input=new ArrayList<String>();
input.add("O 2");
input.add("O 2");
ArrayList<String> letters=new ArrayList<String>();
ArrayList<Integer> numbers=new ArrayList<Integer>();
for (String s:input) {
     String[] splitted=s.split(" ");
     String letter=splitted[0];
     Integer number=Integer.parseInt(splitted[1]);
     int index=-1;
     boolean isthere=false;
     for (String l:letters) {
          index++;
          if (l.equals(letter)) {
              isthere=true; //BUGFIX
              break;
          }
     }
     if (isthere==false) { //BUGFIX
         letters.add(letter);
         numbers.add(number);
     }
     else {
         numbers.set(index,numbers.get(index)+number);
     }      
}
for (int i=0; i < letters.size(); i++) {
     System.out.println(letters.get(i));
     System.out.print(numbers.get(i));
}

Converting it back to have a nice output :

ArrayList<String> output=new ArrayList<String>();
for (int i=0; i < letters.size(); i++) {
    output.add(letters.get(i)+" "+Integer.toString(numbers.get(i));
}

Feel free to comment if you are having any questions.

Luatic
  • 8,513
  • 2
  • 13
  • 34
  • I can't use `Map`, it's too advanced for me. But I think using `.substring()` somehow might work, I'm just not sure how to go about it right now. – Doombringer May 13 '17 at 15:12
  • @DoombringerBG What do you mean with too advanced ? – Luatic May 13 '17 at 15:13
  • As in, never seen it before. – Doombringer May 13 '17 at 15:18
  • @DoombringerBG So why cant you use it ? Have a look at the code. A map works very easy : Key - Value – Luatic May 13 '17 at 15:19
  • @DoombringerBG : Have a look at my edit that is emulating a map. – Luatic May 13 '17 at 15:28
  • I re-read the one without the `Map` several times to make sure I understand it correctly and now I think I do, but is there a way to save the final result in the same `input ArrayList`, because I'm not sure how I'm supposed to update it if the letters and numbers are in different `ArrayList`s. – Doombringer May 13 '17 at 15:47
  • @DoombringerBG : Please have a look at my second edit *Converting it back to have a nice output* – Luatic May 13 '17 at 16:06
  • So, I checked a few times and even directly copy/pasted it, just to be sure I wasn't doing something wrong, and it doesn't seem to work properly. When you have multiple different letters, it takes the first one and adds all of the numbers from the rest of the letters, but it doesn't separate them. Could I have done something wrong or was that the original intent? I'll put more examples with letters in the original post. – Doombringer May 13 '17 at 16:39
  • @DoombringerBG : Already found the error. Please look at the code now. – Luatic May 14 '17 at 08:28
  • Sorry to bother you again, I just have a small question. Why when I remove `break;` it doesn't sum the numbers? I'm just trying to understand, since I don't see the connection. – Doombringer May 14 '17 at 16:18
  • @DoombringerBG : Im using an image to explain, so lets continue in chat : http://chat.stackoverflow.com/rooms/144137/discussion-between-user7185318-and-doombringerbg – Luatic May 14 '17 at 18:00