2012-05-17 104 views
1

我在哪裏編輯此代碼以從我的輸入文件中找到每個單詞的第一個字母,保持頻率和百分比,而不是每個單個字符?例如我可以在哪裏實施charAt(0)或者我需要更改/添加什麼?如何找到每個單詞中的第一個字母?

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

public class FirstWordLetters 
{ 
    public static void main(String[] args) throws FileNotFoundException 
    { 
     char[] capital = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N', 
       'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; 

     char[] small = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
       'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; 


     //Input Scanner into the system   
     Scanner scan; 
     //2. Locate file using the scanner class - search to computer data location 
     try { 
      scan = new Scanner(new File("F:/programming principles/Programming Principles - PART B/enciphered.txt")); 
     } catch (Exception e) { //throw exception e (meaning prevent any runtime errors) 
      System.out.println("File not found"); 
      return; 
     } 
     //3. Set up int's (to count, and for the complete count.) 
     //the aplhabet has 26 characters so the new int will be 26. The mem. space of the array. 
     int[] count = new int[26]; 
     int completeTotal = 0; 
     //4. Start scanning the system 
     //Scan every line and notify user that each line has been read properly. 
     //each time line has been read store value in array and increment by one 
     while(scan.hasNextLine()) { 
      String line = scan.nextLine(); 
      System.out.println("Line read: " + line); 
      char[] digit = line.toCharArray(); 
      for(int i = 0; i < digit.length; i++) { 
       for(int j = 0; j < 26; j++) { 

        if(digit[i] == capital[j] || digit[i] == small[j]) { 
         count[j]++; 
         completeTotal = completeTotal + 1; 
         break; 
        } 
       } 
      } 
     } 
     //5. Display results 
     //Print the overall data - letter, frequency and percentage. 
     System.out.println(""); 
     System.out.println("First Word Count"); //notify user of what has been counted? (full count) 
     for (int i = 0; i < 26; i++) //increment each letter frequency by one each time 
     { 
      System.out.print(" " + small[i]); 
      System.out.print(" " + count[i]); 
      //calculate and display percentage for the full count 
      if (count[i] > 0) 
       System.out.println(" " + (((float) count[i]/completeTotal)*100) + "%"); 
      else 
       System.out.println(" 0%"); 

     } 
    }  
} //end of source code. 
+0

你可以發佈你的輸入數據和你想從它得到的輸出嗎?我不清楚你想在這裏做什麼。 – Tharwen

+0

完成:) @Tharwen – Kieran

+0

你刪除了所有...我會看看無論如何,雖然 – Tharwen

回答

2

此代碼將做到這一點。 inputString是整個文件存儲爲單個字符串。另請注意,如果任何單詞以非字母字符開頭,它將會崩潰。此外,它假定每個單詞都用空格或換行隔開。

int[] charCounts = new int[26]; 

    //Separate the string into individual words 
    //The string " /n" tells it to look for spaces and newline characters "/n" 
    StringTokenizer st = new StringTokenizer(inputString, " /n"); 

    //Loop until all the words are processed 
    while (st.hasMoreTokens()) { 

     //Select the next word 
     String word = st.nextToken(); 

     //Convert the string to upper case so that lower case and upper case characters are represented the same 
     word = word.toUpperCase(); 

     //Get the first character from the word 
     char firstChar = word.charAt(0); 

     //Convert the character to an integer representing it as an ASCII code 
     int charCode = (int) firstChar; 

     //Increment the count for that character by 1 
     //(We subtract 65 from the ASCII code because the array starts at 0 but 'A' is at 65) 
     charCounts[charCode - 65]++; 
    } 

    //Obviously replace this section with whatever you like. It's just to show you how to get the values out again. 
    for (int i = 0; i < charCounts.length; i++){ 
     System.out.println((char)(i + 65) + ": " + charCounts[i]); 
    } 

字符串 「asduhasa ioajsdu ijqwjfoscn kopeurfc eqiwdfjs/nijoasdij iohwefscd aosd8ch」,這個輸出:

A: 2 
B: 0 
C: 0 
D: 0 
E: 1 
F: 0 
G: 0 
H: 0 
I: 4 
J: 0 
K: 1 
L: 0 
M: 0 
N: 0 
O: 0 
P: 0 
Q: 0 
R: 0 
S: 0 
T: 0 
U: 0 
V: 0 
W: 0 
X: 0 
Y: 0 
Z: 0 

如果有什麼你不明白,我會盡我所能來解釋它。

+0

優秀幫助^非常感謝。榮譽給你,先生。 – Kieran

0

只是一個過度使用子的()

或者你可以使用的charAt()

+0

你可以嗎給我一個例子,我會在代碼中使用子字符串? – Kieran

+0

是的,CharAt(0)是我在想什麼,但我不確定它在代碼中的位置,以便它能夠工作...... – Kieran

+0

這很簡單。你接受一個字符串(我認爲是你正在使用的),然後調用子字符串。 例如 String s =「Lol」; s.substring(0,1); < - 第一個字母。 – OmniOwl

1

關於使用正則表達式如何?下面是一個簡單..

String line = scan.nextLine(); 
System.out.println("Line read: " + line); 
for(String s : line.split("\\s+")) { 
    System.out.println(s.charAt(0)); 
    break; 
} 
+0

我對正則表達式的理解很少。當然,在代碼中有一個位置,我可以使用charAt(0)或其他東西,而逐行讀取和數組? – Kieran

+0

我上面的代碼取自問題中的代碼片段。我只是添加了正則表達式。當你想要一個單詞的第一個字母時,必須有一個代碼通過標記器或正則表達式分隔出兩個單詞。就閱讀數組而言,我個人認爲我們可以使用地圖或散列表來查找數量和百分比。 – user1401472

相關問題