2011-03-26 2 views
0

키보드에서 Enter 키를 누르면 코드가 녹음을 멈추도록 할 수 있습니까?Java 스캐너가 키보드에서 라인을 읽고 스택에 저장합니다.

당신은 스택에 저장되어 원하는 작업
Scanner sc = new Scanner(System.in) ; 
Stack<String> stack = new Stack<String>() ; 
System.out.println("Enter sentence:"); 
    while (????) 
    { 
     stack.push(sc.next()) ; 
    } 
+0

? 별도의 요소로 각 라인? – pajton

+0

처음에는 단어로 시도한 다음 역순으로 인쇄하려고합니다. – raoulbia

답변

2
Scanner sc = new Scanner(System.in); 
Stack<String> stack = new Stack<String>() ; 
System.out.println("Enter sentence:"); 

sc = new Scanner(sc.nextLine()); 

while (sc.hasNext()) 
    stack.push(sc.next()); 
+0

감사합니다. – raoulbia

1
String input; 
while ( !(input = sc.nextLine()).equals("")) { 
    stack.push(input) ; 
} 
+0

@both, 도와 주셔서 감사합니다. 만약 내가 한 단어에 대해서 똑같이하고 싶다면, 즉 각 문자를 하나의 요소로 저장하고 싶다면? – raoulbia

+0

은 String 클래스의 toCharArray() 함수를 사용합니다. http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#toCharArray() –

관련 문제