Getting rid of comma, whitespace, sorting in Java -


I'm learning about text processing in Java for class and the class was read in data from the example file, the text When processing, write the file back to the data (list). I understand the example in which he reads in the string in each line and adds that line to the list and uses .split (""), and then the archive. However, if there are commas and additional white spots, then I do not know how to format those formats. I read on regex, but it was not certain if it was needed, because we did not cover it and was going to trim () method. But if I put the trim () method in the comparison method of my class, which implements the compilation, which is passed for the collection. So I think I'm looking for some general guidelines to help with this assignment, but I'm not giving the answer completely. Thank you.

EDIT: The assignment list needs to be written in order to remove duplicates and extra white space.

  public class text processing {public static zero main (string [] args) {try {// get data from class file list < String & gt; Data = textfile utility Redfest file ("addressbooktest.txt"); // Data processing. Indeed, only the address book that first appears with a first name, last name, phone, email / email with a comma, but a duplicate removal, extra white space, and a collection of archives in alphabetical order. Collection (data, FIRSTNAMECOMPARATOR); // write the output file to TextFileUtils.writeTextFile (data, "parsedaddressbooktest.txt"); } Hold (IOException e) {e.printStackTrace (); }} Private Static Final First Name COMPARATOR FIRSTNAMECOMPARATOR = New Firstname Compterator (); } Class First Nime Comparator Optimizer & lt; String & gt; {Comparison of public int (string s1, string s2) {string [] st1 = s1.split (","); String [] st2 = s2.split (","); String first name 1 = whistle 1 [0]. Toucherace (). Trim (); String lastName1 = st1 [1] .toUpperCase (). Trim (); String First Name 2 = Stay2 [0]. Toucherace (). Trim (); String lastName2 = st2 [1] .toUpperCase (). Trim (); If (FirstName1.equals (firstName2)) return firstName1.compareTo (firstName2); otherwise return lastName1.compareTo (lastName2);}}  

A Comparator is the only way to determine the relative sequence of two objects, and nothing. You will use it when you control the order You want to make the collection of the object sorted, but in this case it seems that you want to change the objects within your comparator;

You are right that the trim () will get rid of the law chief and the following whitespace (under your own definition of white space, which is easy to use like you Okay.) You will need to use it first; after that you have removed the "raw" data, but before you add data to the list.

In addition, you actually It is not said that what are the requirements I can assume that you need to leave the backward space, but what about the comma? These should be defined as element separators, in a functional equivalent path for new lines? Or something else?

I think that you are on the right track in general; Just think about the necessary steps and try each one separately as it is clear in this way. What I can tell you, your steps may be:

  1. Identify and open a stream to read data from a file (data).
  2. Use this stream for the service
  3. For each line, delete the white space and split it on a comma.
  4. For each formatted string, add it to the list.
  5. Sort the list in the given order.

Comments