2016-10-19 48 views
-3

我是新來的JAVA,我不知道如何開始這個。我正在尋找一個好的開始。我需要讀取具有特定格式的txt文件並將其放入視圖中。我首先需要閱讀網格的尺寸,然後按照拼圖的順序讀出單詞,然後查找需要查找的單詞數量並保留實際的單詞。如果有人能以一個例子讓我走向正確的方向,那真的會有所幫助。 這是txt文件閱讀縱橫字謎文本文件

5 5 
abcd 
dfad 
adfe 
lkjl 
ekkf 
5 
realword 
realword 
realword 
realword 
realword 

編輯的格式:所以這是我的測試以讀出工作的文件後,嘗試(謝謝!)。但我在這裏得到stuk,我仍然需要從char [] []更改爲box [] [],因爲我將需要它來填充letterGrid。

import java.io.*; 
import java.util.List; 

public class Puzzle { 

    //Box[][] letterGrid; 
    char[][] letterGrid; 
    List<Word> wordList; 
    List<Box> wordInWording; 

    public Puzzle() { 
     try { 
      BufferedReader br = new BufferedReader(new FileReader("..\\word.txt")); 

      String[] dimensions = br.readLine().split(" "); 
      letterGrid = new char[Integer.parseInt(dimensions[0])][Integer.parseInt(dimensions[1])]; 

      for (int i = 0; i < letterGrid[0].length; i++) { 
       String val = br.readLine(); 
       letterGrid[i]= val.toCharArray(); 
      } 
      //while something something 
int r = br.read(); 
     int c = br.read(); 
     letterGrid = new char[r][c]; 

     for (int i = 0; i<r; i++){ 
      String getChar = new String(br.readLine()); 
      for(int j=0; j<c; j++){ 
       letterGrid[i][j] = getChar.charAt(j); 
      } 
     } 

//   String sCurrentLine; 
//   while ((sCurrentLine = br.readLine()) != null) { 
//    System.out.println(sCurrentLine); 
//   } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

} 
+0

我添加了一些東西,但我似乎無法得到ints後面的邏輯,然後字符然後int然後字符串 –

回答

0

這是一個良好的開端:

我只會給你提示瞭如何讀取文本文件行。你必須在閱讀後自行構建邏輯。

public static void main(String[] args) { 

     BufferedReader br = null; 

     try { 

      String sCurrentLine; 

      br = new BufferedReader(new FileReader("C:\\testing.txt")); 

      while ((sCurrentLine = br.readLine()) != null) { 
       System.out.println(sCurrentLine); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (br != null)br.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 

    } 

你不應該在SO中發佈這樣的問題,甚至沒有嘗試過。嘗試編碼,如果你被卡住,然後尋求幫助。社區不鼓勵這樣的問題。

+0

爲什麼這是downvoted? –

+0

文件閱讀工作(謝謝!),但然後我卡住了尺寸和單詞閱讀。我只是沒有得到如何首先閱讀int之間的空格,然後閱讀謎題的字符,然後閱讀大量的單詞和最後的實際單詞。這個邏輯還不適合我。任何幫助? –