2011-03-17 97 views
1

我想掃描由三個單詞組成的字符串,每個單詞由一個空格分隔。掃描儀類幫助

例如

字符串ABC =我喜歡運動

的問題是,我只能設法返回2個字用空格隔開......像這樣

我喜歡

我想問禮貌如果有方法掃描所有三個單詞並返回它們,沒有任何循環或條件。

這是我的一段代碼。

import java.util.*; 

public class fast 
{ 
    public static void main(String[] args) 
    { 
     Scanner scan = new Scanner (System.in); 
     String message1; 
     String message2; 
     // 
     String message3; 
     String message4; 
     // 
     String message5; 
     String message6; 

     System.out.print("Enter message"); 

     message1 = scan.next(); 
     message3 = scan.nextLine(); 

回答

2

待辦事項不需要對掃描儀類,所以才分開,你可以使用yourstring.split(" ");讓所有單詞Array的話。

1
String input = "1 like sport"; 
Scanner s = new Scanner(input).useDelimiter(" ");