2014-11-05 35 views
-2

因此,我正在嘗試使用文本文件和機械手類來讀取每行中的前3個單詞並使用機器人將這些單詞輸入到另一個Windows應用程序的對象。試圖從文本文件中保存每行的前3個字,java

該文本文件將看起來像那樣,它可以有更多的行和每個單詞由選項卡分隔。

abc def hij klm opq rstu 

cba fed jih mlk qpo utsr 

現在,我想第一個3個字每行存儲,並與機器人對象,這工作完全正常,我需要它的工作方式使用它們。

我可以閱讀整行並分開單詞,但不只是前3個單詞。 我剛剛學會了如何使用掃描器對象來編寫和如何從文件中讀取數據,所以我想繼續使用這種方法。 幫助將非常感激。

這裏是我的代碼:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Scanner; 
import java.awt.AWTException; 
import java.awt.Robot; 
import java.awt.event.KeyEvent; 

public class IO_Example_03 { 

    public static void main(String[] args) throws FileNotFoundException, AWTException { 
     run(); // Runs the main program. 
     Robot r2 = new Robot(); 
     r2.keyPress(KeyEvent.VK_ENTER); 
    } 

    public static void run() throws AWTException, FileNotFoundException { 
     Robot r1 = new Robot(); 

     // Put the file path, separated by \\ 
     File f = new File("filename.txt"); 
     Scanner in = new Scanner(f); 
     ArrayList<String> newArr = new ArrayList<String>(); 


     while (in.hasNext()) { 
      // Inputs all the words from the file that are separated by a Tab. 
      String input = in.next(); 

      // Adds all the words to the ArrayList, one by one 
      newArr.add(input); 
     } 


     try { 
      // How much time in milliseconds to pause, to give the user time to 
      // open the desired application. 1000 milliseconds = 1 second. 
      Thread.sleep(7000); 
     } catch (InterruptedException ex) { 
      Thread.currentThread().interrupt(); 
     } 


     // An outer loop that go the beginning until the end of the ArrayList 
     for (int i = 0; i < newArr.size(); i++) { 

      // An inner loop that takes each word separate from the ArrayList. 
      for (int j = 0; j < newArr.get(i).length(); j++) { 

       // Saves to the singleChar variable each character from 
       // individual word. Also, it makes all the words as a lower case. 
       char singleChar = newArr.get(i).toLowerCase().charAt(j); 
       key(singleChar); // Invoking the key method. 

       try { 
        // How much time in milliseconds to pause between each character. 
        // 1000 milliseconds = 1 second. 
        Thread.sleep(99); 
       } catch (InterruptedException ex) { 
        Thread.currentThread().interrupt(); 
       } 

      } 

      r1.keyPress(KeyEvent.VK_ENTER); // A delimiter like Enter, Tab, or Period. 
     } 
    } 

    // A switch method that takes each character, and convert it into a KeyPress robot object. 
    public static void key(char character) throws AWTException { 
     Robot r = new Robot(); 
     switch (character) { 
     case 'a': r.keyPress(KeyEvent.VK_A); break; 
     case 'b': r.keyPress(KeyEvent.VK_B); break; 
     case 'c': r.keyPress(KeyEvent.VK_C); break; 
     case 'd': r.keyPress(KeyEvent.VK_D); break; 
     case 'e': r.keyPress(KeyEvent.VK_E); break; 
     case 'f': r.keyPress(KeyEvent.VK_F); break; 
     case 'g': r.keyPress(KeyEvent.VK_G); break; 
     case 'h': r.keyPress(KeyEvent.VK_H); break; 
     case 'i': r.keyPress(KeyEvent.VK_I); break; 
     case 'j': r.keyPress(KeyEvent.VK_J); break; 
     case 'k': r.keyPress(KeyEvent.VK_K); break; 
     case 'l': r.keyPress(KeyEvent.VK_L); break; 
     case 'm': r.keyPress(KeyEvent.VK_M); break; 
     case 'n': r.keyPress(KeyEvent.VK_N); break; 
     case 'o': r.keyPress(KeyEvent.VK_O); break; 
     case 'p': r.keyPress(KeyEvent.VK_P); break; 
     case 'q': r.keyPress(KeyEvent.VK_Q); break; 
     case 'r': r.keyPress(KeyEvent.VK_R); break; 
     case 's': r.keyPress(KeyEvent.VK_S); break; 
     case 't': r.keyPress(KeyEvent.VK_T); break; 
     case 'u': r.keyPress(KeyEvent.VK_U); break; 
     case 'v': r.keyPress(KeyEvent.VK_V); break; 
     case 'w': r.keyPress(KeyEvent.VK_W); break; 
     case 'x': r.keyPress(KeyEvent.VK_X); break; 
     case 'y': r.keyPress(KeyEvent.VK_Y); break; 
     case 'z': r.keyPress(KeyEvent.VK_Z); break; 
     case '1': r.keyPress(KeyEvent.VK_1); break; 
     case '2': r.keyPress(KeyEvent.VK_2); break; 
     case '3': r.keyPress(KeyEvent.VK_3); break; 
     case '4': r.keyPress(KeyEvent.VK_4); break; 
     case '5': r.keyPress(KeyEvent.VK_5); break; 
     case '6': r.keyPress(KeyEvent.VK_6); break; 
     case '7': r.keyPress(KeyEvent.VK_7); break; 
     case '8': r.keyPress(KeyEvent.VK_8); break; 
     case '9': r.keyPress(KeyEvent.VK_9); break; 
     case '0': r.keyPress(KeyEvent.VK_0); break; 
     case '-': `enter code here` 
      r.keyPress(KeyEvent.VK_MINUS); 

     } 
    }  
} 
+0

請澄清:你想讀取所有的話,即使你只是想前三,或者你想讀的只有三個字? – MarsAtomic 2014-11-05 00:17:50

+0

http://sscce.org/ – 2014-11-05 00:17:58

回答

1

String#split()是你在找什麼。

當讀取文件逐行,只是分裂返回String通過\t(製表符)。 您想要的第三個字將是指數0到2

while (in.hasNext()) { 
    String[] words = in.nextLine().split("\t"); 

    for (int i = 0; i < 3; i++) { 
     newArr.add(words[i]); 
    } 
} 
+0

謝謝,這解決了問題。我試圖在while循環中放置一個for循環,但由於某種原因,我把它放在了錯誤的地方。 – Zurn 2014-11-05 00:34:44

0

你可以嘗試分別讀取每一行與String line = in.nextLine(),那麼你可以通過做String[] words = line.split("\t")使每行每一個字的數組。然後,您可以通過words[0],words[1]words[2]訪問該行的前三個單詞。

下面是一些示例代碼:

while (in.hasNext()) { 
      String input = in.nextLine(); 
      String[] words = input.split("\t"); 
      newArr.add(words[0]); 
      newArr.add(words[1]); 
      newArr.add(words[2]); 
} 
+0

我試圖將前三個單詞存儲在數組中,但如果我有150行,如何將每個行中的前三個單詞存儲起來?我將需要創建數百個數組,然後分別訪問每個數組。我更喜歡將每3個單詞存儲在一個ArrayList中,就像第1行的前三個單詞將在點0-2中,第2行將在點3-5中,依此類推...... – Zurn 2014-11-05 00:28:27

+0

@Zurn您可以將每個元素在你的ArrayList'newArr'的'words'中。 – axl 2014-11-05 00:31:50

相關問題