简体   繁体   中英

Java: removing quotation marks from string

My thoughts/Questions:

I'm working on a Java challenge(Directions bellow). I have Part 1 finished(shown in the code bellow). I'm very close to having Part 2/3 finished.

As you'll see in my code I have 2 for-loops. The first, to iterate through my array of sorted names. The second, to iterate through the characters in each name.

As stated in the directions, an int values is to be generated for each character, and these values are then added. So, A is 1, B is 2, C is 3...and ABC is 6. This int values is then multiplied by the index number of the given String/name. So, if ABC(with a value of 6) was at index 2, it's score would be 12.

After the above step is complete, I am to total all of the all of the scores(each names score).

The above is my understanding of the directions.

The problem is my output looks like this:

 "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 

I've ran through my logic a few times and it seems correct to me, but I don't know how I'm generating these numbers. The only thought I have is that the quotation marks(") are throwing off my calculations. They have an ASCII value of 34. I have attempted to remove them at multiple places in my code with both replace() & replaceAll(), but I have not been able too.

What am I doing wrong/how can I fix it/what do I need to do to complete this assignment/how can I improve my code?

Challenge Directions:

Use the names.txt file, a 46K text file containing over five-thousand first names found in the resources directory.

Part 1: Begin by sorting the list into alphabetical order. Save this new file as p4aNames.txt in the answers directory.

Part 2: Using p4aNames.txt, take the alphabetical value for each name, and multiply this value by its alphabetical position in the list to obtain a name score. For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714. Save the list of all name scores as p4bNames.txt.

Part 3: What is the total of all the name scores in the file?

Pic Link Showing Output & Directory:

http://screencast.com/t/tiiBoyOpR

My Current Code:

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

You wrote

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

Java String is immutable. That function returns a new string with the quotes removed. You can use

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

and it should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM