메인에서 메소드호출 오류 및 for문 탈출
조회수 886회
public class Testtest{
public int countCaps(String einString){
String[] array_word;
array_word = einString.split(""); //
for(int i=0; i<array_word.length;i++){
array_word[i]; //오류!!
}
}
public static void main(String[] args){
String word = Terminal.askString("String: ");
countCaps(einString); //메소드호출
System.out.println(array_word[i]);
}
}
string 입력받아서 string 문자를 하나하나 잘라 배열에 넣은 뒤 출력하는 코드를 짜는 중입니다. array_word[i] 부분이랑 for문 return값이 없다고 자꾸 오류가 나네요. 메인에서 메소드 호출해서 처리하고 싶은데, 뭐가 어떻게 되는지 잘 모르겠습니다..
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
static method 에서 instance method 를 호출할 수 없습니다. instance method 는 항상 첫파라미터가 this(나 자신) 라는 것이 전달됩니다. 그런데 static method 는 instance 의 method 가 아니므로 this 라는 것이 없습니다.
코드를 좀 더 보니...그것도 문제지만 자바 튜터리얼부터 보셔야 할 것 같습니다.
public class Testtest{ public String[] countCaps(String einString){ String[] array_word = einString.split(" "); // 공백으로 문장을 분리하여 배열로 저장한다. return array_word; } public static void main(String[] args){ String word = Terminal.askString("String: "); Testtest instance = new Testtest(); String[] results = instance.countCaps(word); //메소드호출 for(String result: results) // 배열에 저장된 토큰(단어)을 출력 System.out.println(result); } }
- 네 감사합니다. 말하시는 부분이 지금 배우고있는 부분이고, 과제인데 제가 아직 이해가 부족한가봅니다. 알 수 없는 사용자 2019.12.9 08:43
- line 대신 word 로 수정했습니다. 정영훈 2019.12.11 21:19
댓글 입력