Не получается сравнить массив ни с чем в java
Задача (Task):
Evaluate a given floor of the casino to determine if there is a guard between the money and the thief, if there is not, you will sound an alarm.
Input Format:
A string of characters that includes $ (money), T (thief), and G (guard), that represents the layout of the casino floor.
Space on the casino floor that is not occupied by either money, the thief, or a guard is represented by the character x.
Сначала я пробовал сравнивать индекс элементов массива, через indexOf, в ответ массив выдавал: -1, на любой элемент, потом я сравнивал сам массив с "TG$", он не сравнивает, хотя в систем аут показывает именно так, пробовал сравнивать и массивы , создал второй массив и через equals пытался из сравнить, все равно не видит, почему-то.
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
public class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> arr =
new ArrayList<>(Arrays.asList(sc.nextLine().split("x")));
for (String n : arr) {
String r = new String("TGS");
if (arr.contains(r)) {
System.out.print("yess");
} else {
System.out.print("peshalll'");
}
}
}
}
Ответы (1 шт):
Умение пользоваться отладчиком сильно упрощает жизнь, советую освоить этот инструмент и активно им пользоваться в непонятных ситуациях.
Ваш код возвращает массив из пустых элементов, вместо строки, которой вы ожидаете.
ArrayList<String> arr =
new ArrayList<String>(Arrays.asList("xxxxxx$xxxxxxxTxxxxxxxxGxxxxxx".split("x")));
Если хотите получить нужную вам строку, тогда лучше просто убрать все х через replace, заодно выкинете ненужный цикл.
String test = "xxxxxx$xxxxxxxTxxxxxxxxGxxxxxx".replace("x", "");
P.S. Ну или в качестве задачки для размышления подумайте, как вам сделать то, что вы хотите, через перебор массива.

