1

たとえば、この文字列を(ファイルではなくコンソールから)読みたい:

one two three
four five six
seven eight nine

したがって、行ごとに読み取り、すべての行を配列に入れたいと思います。どうすれば読めますか?スキャナーを使用すると、1 行または 1 単語 (次の行または次の行) しか読み取れないためです。

私が意味するのは、たとえば読むことです:one two trhee \n four five six \n seven eight nine...

4

3 に答える 3

8

あなたは自分でやるべきです!同様の例があります:

public class ReadString {

   public static void main (String[] args) {

      //  prompt the user to enter their name
      System.out.print("Enter your name: ");

      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String userName = null;

      //  read the username from the command-line; need to use try/catch with the
      //  readLine() method
      try {
         userName = br.readLine();
      } catch (IOException ioe) {
         System.out.println("IO error trying to read your name!");
         System.exit(1);
      }

      System.out.println("Thanks for the name, " + userName);

   }

}  // end of ReadString class
于 2012-06-01T17:42:24.530 に答える
3

最初の回答のコメントで明確になった質問に回答するには:

読み取りたい行ごとに、Scanner の nextLine() メソッドを 1 回呼び出す必要があります。これはループで実現できます。必然的に遭遇する問題は、「結果の配列がどうあるべきかを知るにはどうすればよいですか?」ということです。答えは、入力自体で指定しないとわからないということです。プログラムの入力仕様を変更して、次のように読み取る行数を要求できます。

3
One Two Three
Four Five
Six Seven Eight

そして、これで入力を読むことができます:

Scanner s = new Scanner(System.in);
int numberOfLinesToRead = new Integer(s.nextLine());
String[] result = new String[numberOfLinesToRead];
String line = "";
for(int i = 0; i < numberOfLinesToRead; i++) { // this loop will be run 3 times, as specified in the first line of input
    result[i] = s.nextLine(); // each line of the input will be placed into the array.
}

または、 ArrayListと呼ばれるより高度なデータ構造を使用することもできます。ArrayList には、作成時に長さが設定されていません。必要に応じて情報を追加するだけでよいため、読み取る入力の量がわからない場合に入力を読み取るのに最適です。たとえば、元の例の入力を使用した場合:

one two trhee
four five six
seven eight nine

次のコードで入力を読み取ることができます。

Scanner s = new Scanner(System.in);
ArrayList<String> result = new ArrayList<String>();
String line = "";
while((line = s.nextLine()) != null) {
    result.add(line);
}

したがって、固定長の配列を作成するのではなく、入力で遭遇したときに ArrayList に各行を単純に .add() することができます。ArrayList を使用する前に、ArrayList について詳しく読むことをお勧めします。

tl;dr: ループを使用して、読み取りたい行ごとに next() または nextLine() を呼び出します。

ループの詳細: Java ループ

于 2012-06-01T18:56:51.417 に答える
-1

このコードを見てください:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class SearchInputText {

  public static void main(String[] args) {
    SearchInputText sit = new SearchInputText();
    try {
        System.out.println("test");
        sit.searchFromRecord("input.txt");
        System.out.println("test2");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void searchFromRecord(String recordName) throws IOException {
    File file = new File(recordName);
    Scanner scanner = new Scanner(file);
    StringBuilder textFromFile = new StringBuilder();
    while (scanner.hasNext()) {
        textFromFile.append(scanner.next());
    }
    scanner.close();

    // read input from console, compare the strings and print the result
    String word = "";
    Scanner scanner2 = new Scanner(System.in);
    while (((word = scanner2.nextLine()) != null)
            && !word.equalsIgnoreCase("quit")) {
        if (textFromFile.toString().contains(word)) {
            System.out.println("The word is on the text file");
        } else {
            System.out.println("The word " + word
                    + " is not on the text file");
        }
    }
    scanner2.close();

 }

}
于 2014-08-03T17:58:13.440 に答える