파닥이

Scanner의 nextLine()과 next형식을 교차 반복했을때의 주의점 본문

IT/Java

Scanner의 nextLine()과 next형식을 교차 반복했을때의 주의점

알라이또 2017. 1. 21. 11:52
반응형
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] str = { " """"1""2""3" };
for (String s : str)
System.out.println(s);
Scanner sc = new Scanner(System.in);
String such = "";
while (true) {
System.out.print("입력 : ");
such = sc.nextLine();
for (int i = 0; i < str.length; i++) {
if (str[i].indexOf(such) >= 0) {
System.out.printf("=>str[%d] : [%s] 있음\n",i,str[i]);
}
}
System.out.print("선택(종료 3) : ");
int a = sc.nextInt();
if(a==3)
break;
}
System.out.println("-----------------------");
if("3".indexOf("")>=0)
System.out.println("있음");
}
}



""은 인덱스오브로 찾으면 모든 문자가 가지고있는 속성임.

즉 nextLine을 쓰고 next로 받는것을 반복하는 형식이면

next를 입력하는 과정에서 엔터가 nextLine으로 들어가
 
개행으로 구분짓는 nextLine은 ""을 입력받는것으로 처리함


 

 

Comments