2016-11-29 11 views
0

私は、ユーザー入力を使用してテキストファイルから2D配列を検索しようとしていますが、これまでのところ、単語が2D配列にないか、ユーザーが入力したものが配列に含まれていると言っています。例えば、単語redは配列にありますが、単語が複数回配列に出力されるのではなく、複数回出力されます。私が入力した単語ファイルは、最初の2つの数字が2次元配列のサイズのために使用される場所です。誰も私が間違っていることについて何かヒントを教えてもらえますか?2D配列ワード検索

6 6 

d e v o l g 
r e d p h k 
q c h z j c 
p o a a f o 
v a m m n l 
q t f o x b 

私のコードは次のようである私は、コードを少しアップクリアする必要がある場合は、ちょうど私が私がいつかのためにこれに取り組んできたとして代が理解し少し難しいかもしれないと確信しているように私に知らせて。それが一致するものを見つけた後、それを反復し続けないように

import java.util.Scanner; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Arrays; 

public class WordSearch { 

public static void main(String[] args) { 

    char[][] puzzle = null; 
    puzzle = fill(puzzle); 


    // create scanner for user input to take the word for searching 

    @SuppressWarnings("resource") 
    Scanner in = new Scanner(System.in); 
    System.out.print("Enter the word you wish to search for (limit of four characters): "); 
    String wordToFind = in.nextLine(); 
    play(wordToFind, puzzle); 
    printPuzzle(puzzle); 
} 

public static char[][] fill(char[][] puzzle) { 

    // file Reading 

    boolean flag = true;// boolean flag is used for prompting user if the file location is incorrect 
    @SuppressWarnings("resource") 
    Scanner in = new Scanner(System.in); 
    while (flag) { 
     System.out.print("Please input the text file locaiton: "); 

     String fileLoc = in.nextLine(); 

     try { 

      File f = new File(fileLoc); 
      @SuppressWarnings("resource") 
      Scanner fileRead = new Scanner(f); 

      int row = fileRead.nextInt();// i 
      int col = fileRead.nextInt();// j 

      puzzle = new char[row][col]; 

      for (int i = 0; i < puzzle.length; i++) { 
       for (int j = 0; j < puzzle[0].length; j++) { 
        puzzle[i][j] = fileRead.next().charAt(0); 

       } 
      } 

      flag = false;// breaks the loop so the user isn't re-prompt for 
          // file location 
     } catch (FileNotFoundException e) { 

     } 

    } 
    return puzzle; 
} 

public static void printPuzzle(char[][] puzzle) { 
    for (int i = 0; i < puzzle.length; i++) { 
     for (int j = 0; j < puzzle[0].length; j++) { 
      System.out.print(puzzle[i][j] + " "); 
     } 
     System.out.println(); 
    } 
} 

public static void play(String word, char[][] puzzle) { 
    for (int i = 0; i < puzzle.length; i++) { 
     for (int j = 0; j < puzzle[0].length; j++) { 

      if (checkUp(puzzle, word, i, j) == true) 
      { 
       System.out.println("The word " + word + " was found by the method checkUp beginnning in cell "); 


      } 
      if (checkDown(puzzle, word, i, j) == true) 
      { 
       System.out.println("The word " + word + " was found by the method checkDown beginning in cell"); 

      } 
      if(checkRight(puzzle, word, i, j) == true) 
      { 
       System.out.println("The word " + word + " was found by the method checkDown beginning in cell"); 

      } 
      if(checkLeft(puzzle, word, i, j) == true) 
      { 
       System.out.println("The word " + word + " was found by the method checkLeft beginning in cell"); 

      } 

      if(checkUp(puzzle, word, i, j) != true && checkDown(puzzle, word, i, j) != true && checkRight(puzzle, word, i, j) != true && checkLeft(puzzle, word, i, j) != true) 
      { 
       System.out.println("The word " + word + " was not in the puzzle"); 
       break; 

      } 


     } 
    } 
} 
/** 
* searches for the user defined word going up 
* @param puzzle 
* @param word 
* @param i 
* @param j 
* @return 
*/ 
public static boolean checkUp(char[][] puzzle, String word, int i, int j) { 
    char search = word.charAt(0); 
    for (i = 0; i < puzzle.length; i++) { 
     for (j = 0; j < puzzle[0].length; j++) { 
      if (search != puzzle[i][j]) { 
       return false; 

      } 

      else { 
       i = i - 1; 
       if (i < 0) { 
        return false; 
       } 
      } 
     } 

    } 
    return true; 
} 



/** 
* searches for the user defined word going down 
* @param puzzle 
* @param word 
* @param i 
* @param j 
* @return 
*/ 
public static boolean checkDown(char[][] puzzle, String word, int i, int j) { 
    char search = word.charAt(0); 
    for (i = 0; i < puzzle.length; i++) { 
     for (j = 0; j < puzzle[0].length; j++) { 
      if (search != puzzle[i][j]) { 
       return false; 

      } 

      else { 
       i = i + 1; 
       if (i < 0) { 
        return false; 
       } 
      } 
     } 

    } 
    return true; 
} 

/** 
* searches for the user defined word going left 
* @param puzzle 
* @param word 
* @param i 
* @param j 
* @return 
*/ 
public static boolean checkLeft(char[][] puzzle, String word, int i, int j) { 
    char search = word.charAt(0); 
    for (i = 0; i < puzzle.length; i++) { 
     for (j = 0; j < puzzle[0].length; j++) { 
      if (search != puzzle[i][j]) { 
       return false; 

      } 

      else { 
       j = j - 1; 
       if (j < 0) { 
        return false; 
       } 
      } 
     } 

    } 
    return true; 
} 

/** 
* this method will return true if the user defined word is found going right 
* @param puzzle 
* @param word 
* @param i 
* @param j 
* @return 
*/ 
public static boolean checkRight(char[][] puzzle, String word, int i, int j) { 
    char search = word.charAt(0); 
    for (i = 0; i < puzzle.length; i++) { 
     for (j = 0; j < puzzle[0].length; j++) { 
      if (search != puzzle[i][j]) { 
       return false; 

      } 

      else { 
       j = j + 1; 
       if (j < 0) { 
        return false; 
       } 
      } 
     } 

    } 
    return true; 
} 

} 
+0

よりもむしろ抑制の警告は、[試す--リソースで](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)を使用する必要があります。 – 4castle

+0

私はそれを試してみましょう –

答えて

0

if文の中return;文を入れてください。ループが終了した後に見つからなかったことを表示することができます。

public static void play(String word, char[][] puzzle) { 
    String foundMessage = "The word %s was found by the method %s beginnning in cell%n"; 
    for (int i = 0; i < puzzle.length; i++) { 
     for (int j = 0; j < puzzle[0].length; j++) { 
      if (checkUp(puzzle, word, i, j)) { 
       System.out.printf(foundMessage, word, "checkUp"); 
       return; 
      } else if (checkDown(puzzle, word, i, j)) { 
       System.out.printf(foundMessage, word, "checkDown"); 
       return; 
      } else if (checkRight(puzzle, word, i, j)) { 
       System.out.printf(foundMessage, word, "checkRight"); 
       return; 
      } else if (checkLeft(puzzle, word, i, j)) { 
       System.out.printf(foundMessage, word, "checkLeft"); 
       return; 
      } 
     } 
    } 
    System.out.println("The word " + word + " was not in the puzzle"); 
} 

単語が2D配列に含まれていることを確認するには、単一のループが必要です。たとえば、checkUp

public static boolean checkUp(char[][] puzzle, String word, int i, int j) { 
    if (i - word.length() >= 0) { 
     for (int offset = 0; offset < word.length(); offset++) { 
      if (puzzle[i - offset][j] != word.charAt(offset)) { 
       return false; 
      } 
     } 
     return true; 
    } else { 
     return false; 
    } 
}  
+0

あなたが受け取っている繰り返しのメッセージエラーを修正したので、あなたは素晴らしいです。私はまだ、ユーザーが入力した単語を正しく見つけるためのメソッドを取得しようとしています。使用できるヒントはありますか? –

+0

もちろん、 'checkUp'のやり方についても説明しました。他のものは、最初の 'if'ステートメントと' puzzle'インデックス付けを除いて同じです。 – 4castle