Cannot make a static reference to the non-static method ㅁㅁㅁ from the type ㅁㅁㅁJava(603979977)
static 으로 동일 하게 맞춰주면된다.
코드로 보면 이해된다.
public class Compressed {
public static void main(String[] args) {
System.out.println("Hello");
String show = "abbccccc";
System.out.println(compress(show));
}
String compress(String str) {
StringBuilder compressed = new StringBuilder();
int countConsecutive = 0;
for (int i = 0; i < str.length(); i++) {
countConsecutive++;
if (i + 1 >= str.length() || str.charAt(i) != str.charAt(i + 1)) {
compressed.append(str.charAt(i));
compressed.append(countConsecutive);
countConsecutive = 0;
}
}
return compressed.length() < str.length() ? compressed.toString() : str;
}
}
아래와 같이 Static을 추가해주면 된다... 간단하다.
public class Compressed {
public static void main(String[] args) {
System.out.println("Hello");
String show = "abbccccc";
System.out.println(compress(show));
}
static String compress(String str) {
StringBuilder compressed = new StringBuilder();
int countConsecutive = 0;
for (int i = 0; i < str.length(); i++) {
countConsecutive++;
if (i + 1 >= str.length() || str.charAt(i) != str.charAt(i + 1)) {
compressed.append(str.charAt(i));
compressed.append(countConsecutive);
countConsecutive = 0;
}
}
return compressed.length() < str.length() ? compressed.toString() : str;
}
}
728x90
반응형