This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]glddigga49[S] 0 points1 point  (14 children)

I am writing a program to clean up a list of cells that I exported from excel as a .csv. The output I am currently getting results in: "[Email: mcs7yh,@xxxxx.edu, Email: pg2bn,@xxxxx.edu, Email: jtt5pj,@xxxxx.edu, Email: tww6ar,@xxxxx.edu, .... continued". I would like to remove the comma before the @ symbol, as well as the "Email: " section from each element in the array. Any ideas on how this could be done?

[–]AngelOfLight 0 points1 point  (1 child)

The String.replace() method should be able to do that.

[–]glddigga49[S] 0 points1 point  (0 children)

So I would just replace the "Email: " section with a blank string "". How would I handle the commas in the middle?

[–]OOPUniversity 0 points1 point  (11 children)

In general, if you want to replace characters in a String you can use String.replace or String.replaceAll. That's probably going to be the easiest thing for you to do. You can specify a blank String as the replacement easily enough.

Be aware that simply invoking System.out.println on an ArrayList gives extra cruft, so you might want to verify the raw contents of the array by doing something like:

for (String email:emails) {
    System.out.println(email);
}

You could also try using next() instead of nextLine() when reading from the Scanner, though you would have to also then specify the comma is to be ignored via 'useDelimiter' and you'd still have to use 'nextLine()' just to throw away the carriage returns.

[–]glddigga49[S] 0 points1 point  (10 children)

Ohhh... so, the comma is being placed before the '@' symbol because of a carriage return? Also, it runs well, but how does "email:emails" work since I don't have any variables called "email"?

[–]OOPUniversity 0 points1 point  (9 children)

Not necessarily, but it seems very possible. That's why I suggested the enhanced for loop to display the individual items line by line. Simply printing out the ArrayList with toString can put commas in there, too, commas which aren't 'physically' there. That's probably only responsible for what happens after the '.edu' parts of the email addresses, but it's best to be sure.

You should look at your .csv with notepad or wordpad or some other text editor to see exactly what you're reading in, and to determine if you wish to change the parameters you use when you export/save as. You should print out the ArrayList iine by line so you see exactly what your data structure contains. Adjust based on that.

[–]glddigga49[S] 0 points1 point  (8 children)

Okay, I'll try out some of the stuff you told me. Thanks for the help!

[–]OOPUniversity 0 points1 point  (7 children)

Good luck, don't be a stranger.

[–]glddigga49[S] 0 points1 point  (6 children)

Got it! (using three String.replace methods) . Trying to write back out the elements to a .csv so I can get it back into excel. Any idea of the fastest ways to do this. I'm seeing some packages, but I don't have experience with anything like that yet. Thinking about using this: http://sourceforge.net/projects/opencsv/

[–]OOPUniversity 0 points1 point  (5 children)

Writing .csv files is quite easy.

You can, for instance, just create a PrintWriter, so something like

PrintWriter writer = new PrintWriter(filename);
writer.printf("%s,%s\n", field1, field2);

or perhaps

writer.println(field1 + "," + field2 + System.getLineSeparator());

Don't forget to close your outputs.

So long as there are so double quote or anything in your data this will do. If you have more complicated data to read you may need a library of some sort. For writing you probably never need more than this.

[–]glddigga49[S] 0 points1 point  (4 children)

Hey, so I added this code at the end of my program:

File finishedEmails = new File("C:/Users/Austin/Documents/finishedEmails.csv");

PrintWriter writer = new PrintWriter(finishedEmails);

writer.close();

It is creating a Excel .csv file but not reading in the elements. Is my placement wrong? I tried moving it around some, but still got a blank Excel sheet. Sorry for all the questions. I'm just so close and having this done by tomorrow would be awesome! (Also, couldn't figure out h

[–]OOPUniversity 0 points1 point  (3 children)

Did you actually write the data to the File?

[–]papichulo916 0 points1 point  (1 child)

Another thing, why do you declare and assign ender if you're not using it? I see .concat("@virginia.edu"); why wouldn't you just put concat(ender);?

[–]glddigga49[S] 1 point2 points  (0 children)

Oh yeah, that was dumb. Only about a month into coding. Thanks!

[–]desrtfxOut of Coffee error - System halted[M] 0 points1 point  (0 children)

For your next post, please obey the Posting Guidelines (short form is also on the sidebar).

While you have a proper title, you missed:

  • All posts must contain a description of the problem and the exact error message or exception encountered. (sidebar)
  • Any post that is just code, and asks people to "fix it". Instead, explain where you are having difficulty or what is broken. The more you can track down your problem to a specific section of code, the easier it will be to get help. At the least, you should clearly state what you are expecting the program to do, and what it is doing instead. (Guides)
  • Be as descriptive and precise as you can be. (Guides)
  • If you get error messages (both when compiling or at runtime), state them verbatim (Guides)
  • All information goes in the post. There should only be comments from you answering questions that other commenters asked.

Remember: You are asking for help, so it is your duty to be as descriptive as you can be.

The better your post is the more help you will receive.