2015-06-27 171 views
0

我的想法/問題:的Java:從字符串刪除引號

我工作的一個Java挑戰(路線波紋管)。我已完成第1部分(如下面的代碼所示)。我非常接近完成Part 2/3

正如你在我的代碼中看到的,我有2個for-loops。第一,遍歷我的排序名稱數組。第二,遍歷每個名​​字中的字符。

如指示中所述,將爲每個字符生成一個int值,然後添加這些值。因此,A是1,B是2,C是3 ...,ABC是6.然後這個int值乘以給定的String/name的索引號。因此,如果ABC(值爲6)在索引2處,則分數爲12.

完成上述步驟後,我將總計所有分數(每個名稱分數)。

以上是我對方向的理解。

問題是我的輸出看起來像這樣:

"AARON" 
 
0 
 
"ABBEY" 
 
-25 
 
"ABBIE" 
 
-82 
 
"ABBY" 
 
-90 
 
"ABDUL" 
 
-80 
 
"ABE" 
 
-260 
 
"ABEL" 
 
-240 
 
"ABIGAIL" 
 
-133 
 
"ABRAHAM" 
 
-128 
 
"ABRAM" 
 
-225 
 
"ADA" 
 
-540 
 
"ADAH" 
 
-506 
 
"ADALBERTO" 
 
216 
 
"ADALINE" 
 
-182 
 
"ADAM" 
 
-574 
 
"ADAN" 
 
-600 
 
"ADDIE" 
 
-592 
 
"ADELA" 
 
-629

我已經通過我的邏輯跑了幾次,它似乎是正確的我,但我不我知道我是如何產生這些數字的。我唯一的想法是引號(「)正在拋棄我的計算,它們的ASCII值爲34.我試圖在我的代碼中的多個位置刪除它們,同時替換()& replaceAll(),但我一直沒能太

我在做什麼錯了/我怎麼能解決這個問題/什麼做我需要做來完成這項任務/我怎樣才能提高我的代碼

挑戰路線:

使用names.txt文件,這是一個46K的文本文件,其中包含在資源目錄中找到的超過五千個名字

第1部分:首先將列表按字母順序排序。將這個新文件保存爲answers目錄中的p4aNames.txt。

第2部分:使用p4aNames.txt,獲取每個名稱的字母值,並將該值乘以列表中的字母位置以獲取名稱分數。例如,當列表按字母順序排序時,值爲3 + 15 + 12 + 9 + 14 = 53的COLIN是列表中的第938個名稱。因此,COLIN將獲得938×53 = 49714的分數。將所有名稱分數的列表保存爲p4bNames.txt。

第3部分:文件中所有名稱得分的總和是多少?

產品圖的鏈接顯示輸出&目錄:

http://screencast.com/t/tiiBoyOpR

我當前的代碼:

package app; 
 

 
import java.io.BufferedReader; 
 
import java.io.BufferedWriter; 
 
import java.io.FileReader; 
 
import java.io.FileWriter; 
 
import java.io.IOException; 
 
import java.io.PrintWriter; 
 
import java.util.Arrays; 
 

 
public class AlphabetizedList { 
 
\t public static void main() throws IOException { 
 
\t \t new AlphabetizedList().sortingList(); 
 
\t } 
 
\t public void sortingList() throws IOException { 
 
\t \t FileReader fb = new FileReader("resources/names.txt"); 
 
\t \t BufferedReader bf = new BufferedReader(fb); 
 
\t \t String out = bf.readLine(); 
 
\t \t out = out.substring(out.indexOf("\"")); //get rid of strange characters appearingbeforefirstname 
 
// \t \t System.out.println(out); // output: 
 
//  "MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA"... 
 
\t \t 
 
\t \t String[] sortedStr = out.split(","); 
 
\t \t Arrays.sort(sortedStr); 
 
\t \t 
 
\t \t PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("answers/p4aNames.txt"))); \t 
 
\t 
 
\t \t for (int i = 0; i < sortedStr.length; i++) { 
 
\t \t \t 
 
\t \t \t pw.println(sortedStr[i]); 
 
\t \t \t System.out.println(sortedStr[i]);// print to console just to see output 
 
\t \t \t 
 
\t \t \t int score = 0; 
 
// \t \t \t sortedStr[i].replaceAll("\"", ""); // I used this to try to remove the "s from my Strings 
 
\t \t \t for (char ch: sortedStr[i].toUpperCase().toCharArray()) { 
 
\t \t \t  score += ((int)ch - 64); /* A is decimal 65 */ 
 
\t \t \t } 
 
\t \t \t score = score * i; /* multiply by position in the list */ 
 
\t \t \t pw.println(score); 
 
\t \t \t System.out.println(score); 
 
\t \t } 
 
\t \t bf.close(); \t \t 
 
\t \t fb.close(); \t \t 
 
\t \t pw.close(); 
 
\t } 
 
}

回答

0

你寫

// sortedStr[i].replaceAll("\"", ""); // I used this to try to remove the "s from my Strings 

Java String是不可變的。該函數返回一個刪除了引號的新字符串。你可以使用

sortedStr[i] = sortedStr[i].replaceAll("\"", ""); 

它應該可以正常工作。

+0

謝謝。我不認爲我會看到這一點。聽到你的意見,我可以問你將如何完成第3部分?第3部分:文件中所有名稱分數的總和是多少? –

+0

保留一個單獨的int值,並總結它,因爲你得到每個單詞的分數。 – twentylemon

+0

我很好奇,你在哪裏找到這個挑戰?我喜歡這些東西。 – Daniel