3

入力行の残りの部分を読み取る方法がわかりません。最初の単語をトークン化してから、残りの入力行を 1 つのトークン全体として作成する必要があります

public Command getCommand() 
{
    String inputLine;   // will hold the full input line
    String word1 = null;
    String word2 = null;

    System.out.print("> ");     // print prompt

    inputLine = reader.nextLine();

    // Find up to two words on the line.
    Scanner tokenizer = new Scanner(inputLine);
    if(tokenizer.hasNext()) {
        word1 = tokenizer.next();      // get first word
        if(tokenizer.hasNext()) {
            word2 = tokenizer.next();     // get second word
            // note: just ignores the rest of the input line.
        }
    }

    // Now check whether this word is known. If so, create a command
    // with it. If not, create a "null" command (for unknown command).
    if(commands.isCommand(word1)) {
        return new Command(word1, word2);
    }
    else {
        return new Command(null, word2); 
    }
}

入力:

take spinning wheel

出力:

spinning

望ましい出力:

spinning wheel
4

4 に答える 4

3

使用するsplit()
String[] line = scan.nextLine().split(" ");
String firstWord = line[0];
String secondWord = line[1];

これは、行をスペースで分割する必要があり、それが配列に変換されることを意味します。インデックスを使用すると、必要な単語を取得できます

于 2013-03-01T04:28:58.583 に答える
0

このように試すこともできます...

String s = "This is Testing Result";
System.out.println(s.split(" ")[0]);
System.out.println(s.substring(s.split(" ")[0].length()+1, s.length()-1));
于 2013-03-01T04:44:35.113 に答える
0

また -

String inputLine =//Your Read line
String desiredOutput=inputLine.substring(inputLine.indexOf(" ")+1)
于 2013-03-01T04:35:04.293 に答える