Stuart A. Burkett wrote:So what's your question ?
Casey Clayton wrote:The last project consisted of getting some basic info from a string and formatting it, nothing like this.
Casey Clayton wrote:
Example lines from csv:
ID# Name Age Street Address City State ZIP Employer Favorite Drugs
1000 Dara Neal 19 Ap #778-655 Vestibulum Road Beaumont NC 97394 Macromedia Metformin HCl, Januvia, Abilify
1010 Odette Tyler 92 4737 Fringilla Rd. Sassocorvaro NE 94379 Altavista
1020 Bernard Moss 61 8433 Mollis. Street Moorsele IA 99287 Google Simvastatin, Citalopram HBR, Trazodone HCl
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:
Casey Clayton wrote:
Example lines from csv:
ID# Name Age Street Address City State ZIP Employer Favorite Drugs
1000 Dara Neal 19 Ap #778-655 Vestibulum Road Beaumont NC 97394 Macromedia Metformin HCl, Januvia, Abilify
1010 Odette Tyler 92 4737 Fringilla Rd. Sassocorvaro NE 94379 Altavista
1020 Bernard Moss 61 8433 Mollis. Street Moorsele IA 99287 Google Simvastatin, Citalopram HBR, Trazodone HCl
That is a pretty lousy CSV file...there are almost no commas!!!
Casey Clayton wrote:Here is my barebones Employee class as of now.
Casey Clayton wrote:Lol I copied it exactly from excel and it is suppose to be in csv but I get a ton of errors saying it isn't.
Pawel Pawlowicz wrote:
Casey Clayton wrote:Here is my barebones Employee class as of now.
Looks solid. I think it should be named Employee.
Use int instead of Integer unless you really need to. From your code I can see that you don't need Integer.
Consider adding equals and hashCode method for comparing equality.
If you want to compare Employee objects to determine which is "larger" (for example for sorting) let your class implement Comparable<Employee> or write appropriate Comparator<Employee>.
Adding toString method is also a good idea.
Next case. You should not allow an object to be created with illegal state. Is having age = -9123 or name = null legal for your object? I think it isn't. So don't allow creating instances of Employee with such values.
Casey Clayton wrote:Lol I copied it exactly from excel and it is suppose to be in csv but I get a ton of errors saying it isn't.
Open it with a text editor (for example Notepad++) and see how it looks.
Casey Clayton wrote:It looks good in notepad++
Casey Clayton wrote:Now I just need to figure out my loop, I know that I have 8 fields so I need to have a while loop to create a new object once it hits 8 so something like.
Casey Clayton wrote:But I need to increment the variable name based on the counter variable, is that possible. So on the 3rd time through it would be Employee employee3 = new Employee()
Pawel Pawlowicz wrote:
Casey Clayton wrote:It looks good in notepad++
How exactly? Can you post a couple of lines? Is it proper CSV file?
Casey Clayton wrote:Now I just need to figure out my loop, I know that I have 8 fields so I need to have a while loop to create a new object once it hits 8 so something like.
That doesn't soud good. If your file realli is a CSV, then read it line by line and split the string (using String.split). No need to loop till 8...
The only loop you need to read a CSV file is one that reads line by line until the file ends.
Actually reading CSV is not so trivial (because you might want to have a comma in a value for example) but for your program I think it will suffice.
Casey Clayton wrote:But I need to increment the variable name based on the counter variable, is that possible. So on the 3rd time through it would be Employee employee3 = new Employee()
No it is not possible. If you need to look for Employee by its name, store your Employees in Map<String, Employee>. Remember to override equals and hashCode if you plan to do that. (or compareTo if you want to use sorted map like TreeMap).
I see that you want to look employees by a number (index). So, consider using one of List implementations (for example ArrayList).
Pawel Pawlowicz wrote:Delete inner while loop*. What do you think it does?
Delete it*. It just sets the fields in Employee objecteight times... (well... it does it nine times...). And you only need to do this once. You don't need this lopp (as I said before).
*By delete the loop I mean delete while statement and the pair of curly braces. Leave the body of the loop as it is.
You named your array String[] employees. I think you are a little confused. This array doesn't hold a number of employees (as its name would suggest). It holds values of fields for exactly one employee.
Also your readCsv method does not acomplish anything. Yes, it reads the file and creates a list. But this list will be eligible for garbage collection as soon as the method finishes executing. Have this method return the list so you can use it later.
Casey Clayton wrote:Ok I have removed that but I keep running into a problem, I have a few places in the csv where there is a comma and no data after it, I cant change the csv file so how would I account for this?
Pawel Pawlowicz wrote:
Casey Clayton wrote:Ok I have removed that but I keep running into a problem, I have a few places in the csv where there is a comma and no data after it, I cant change the csv file so how would I account for this?
Show us relevant part of the file.
Also, I noticed a comment in code you posted says // add values from csv to car object
Where did you copy it from? Are you putting some code you don't understand into your project? That's not the way to program.
You need to StopCoding -< click that
Turn your computer off. Then figure out how to solve the problem. Then and only then start writing the code.
Casey Clayton wrote:
Pawel Pawlowicz wrote:
Casey Clayton wrote:Ok I have removed that but I keep running into a problem, I have a few places in the csv where there is a comma and no data after it, I cant change the csv file so how would I account for this?
Show us relevant part of the file.
Also, I noticed a comment in code you posted says // add values from csv to car object
Where did you copy it from? Are you putting some code you don't understand into your project? That's not the way to program.
You need to StopCoding -< click that
Turn your computer off. Then figure out how to solve the problem. Then and only then start writing the code.
It was a piece of code that was already done, I do understand how it works or I wouldn't even be close to this far.
1000,Dara Neal,19,Ap #778-655 Vestibulum Road,Beaumont,NC,97394,Macromedia,"Metformin HCl, Januvia, Abilify"
1010,Odette Tyler,92,4737 Fringilla Rd.,Sassocorvaro,NE,94379,Altavista,
Notice nothing after the comma on the second line here. It stops importing after it hits that.
And I would stop coding but I am doing a test for my training class at work and only have so long to finish, seeing how my pay is based off my score if I stop and don't finish I am screwed.
Pawel Pawlowicz wrote:
Casey Clayton wrote:
Pawel Pawlowicz wrote:
Casey Clayton wrote:Ok I have removed that but I keep running into a problem, I have a few places in the csv where there is a comma and no data after it, I cant change the csv file so how would I account for this?
Show us relevant part of the file.
Also, I noticed a comment in code you posted says // add values from csv to car object
Where did you copy it from? Are you putting some code you don't understand into your project? That's not the way to program.
You need to StopCoding -< click that
Turn your computer off. Then figure out how to solve the problem. Then and only then start writing the code.
It was a piece of code that was already done, I do understand how it works or I wouldn't even be close to this far.
1000,Dara Neal,19,Ap #778-655 Vestibulum Road,Beaumont,NC,97394,Macromedia,"Metformin HCl, Januvia, Abilify"
1010,Odette Tyler,92,4737 Fringilla Rd.,Sassocorvaro,NE,94379,Altavista,
Notice nothing after the comma on the second line here. It stops importing after it hits that.
And I would stop coding but I am doing a test for my training class at work and only have so long to finish, seeing how my pay is based off my score if I stop and don't finish I am screwed.
It stops there because when there is a separator at the end of a string, split method ignores it. So the size of resulting array is 8. You try to read element with index 8 which yields ArrayIndexOutOfBoundsException.
Use split(String regex, int limit) method instead as it allows you to type size of resulting array.
You will encounter another serious problem. Solving it will complicate reading part of your program. It's what I was worried before. Reading CSV is not so trivial.
Look at this entry "Metformin HCl, Januvia, Abilify"
It should not be split because it is in double quotes. And double quotes are not part of the data. String.split method is not so smart to deal with it.
Are you allowed to use third-party libraries in your project?
You could find one with support of CSV reading. Unless you are required to write your own mechanism, why reinvend the wheel?
Casey Clayton wrote:Nope if I were I would just use a SQLite database and import all the data into that then just write methods to pull from that. The quotes actually help me because they put all the FavoriteDrugs into one spot in the array which is good but when I am hitting these damn empty fields is where the problem is occurring.
Pawel Pawlowicz wrote:
Casey Clayton wrote:Nope if I were I would just use a SQLite database and import all the data into that then just write methods to pull from that. The quotes actually help me because they put all the FavoriteDrugs into one spot in the array which is good but when I am hitting these damn empty fields is where the problem is occurring.
I already told you how to deal with empty string at the end. I think you really should write code that deals with double quotes. What if you get double quotes (and commas inside) in 1st field? All you data from that line would be read incorrectly (being shifted into wrong index of array)...
Casey Clayton wrote:It seems to be taking that data in as a single string into the array which is what I want and I know that doesn't happen in the array and if it were to i'm not concerned I just want a halfway working project to give them since everyone else is having a terrible time with it as well.
I have never done a regex by the way, how would that look?
Pawel Pawlowicz wrote:
Casey Clayton wrote:It seems to be taking that data in as a single string into the array which is what I want and I know that doesn't happen in the array and if it were to i'm not concerned I just want a halfway working project to give them since everyone else is having a terrible time with it as well.
I have never done a regex by the way, how would that look?
This might lead to serious bugs...
Look at this example. I implemented splitAndPrint method similiar to yours. It takes a string and splits it by comma. It hopes to be good for CSV.
For trivial case 1,2,3,4 it behaves correctly and prints the expected result:
[1]
[2]
[3]
[4]
But for non-trivial data like 1,"2,2",3,4 it prints:
[1]
["2]
[2"]
[3]
[4]
while the correct result is:
[1]
[2,2]
[3]
[4]
Do you see what I am trying to show you?
Casey Clayton wrote: when it should be "Metformin HCL, Januvia, Abilify" all in one position
Pawel Pawlowicz wrote:
Casey Clayton wrote: when it should be "Metformin HCL, Januvia, Abilify" all in one position
No, it should be Metformin HCL, Januvia, Abilify.
Without the quotes.
Too bad you can't use opencsv or Apache commons CSV.