• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Console Program

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So again I am back with a test question. We went over the other one in class after the test so I have that much figured out, but again we have made a massive jump on to something completely new to me and the rest of the class.

I will lay out the exact guidelines for this below.

1. Display menus for commands and navigation

2. Only terminate upon user request

3. Allow users to load data file on-demand(file provided in csv format)

4. Allow users to display the following.

a. Complete customer list sorted by ID#

b. a. Complete customer list sorted by Name

c. Customers eligible for retirement(currently 65+ age but could change so code has to allow for this change)

e. Customers by State

f. Drugs used by employees at a given employer.

5. Allow users to display a single customer's info by entering the customer's Id#

6. Allow users to modify a single customer's employer.

We have to accomplish the data handling via Maps.


My class layout as of now looks like so.

Top Level: Class to handle file input and output

Mid: Class to handle all the manipulation of the data in that file via maps, sets, etc.

Low level: class to handle all the user input and interface via cases using the methods from the two classes above it.

 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what's your question ?
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:So what's your question ?



I'm kinda needing someone to help me go through and figure out how to actually do this. The last project consisted of getting some basic info from a string and formatting it, nothing like this. We read from HeadFirstJava and don't really get taught anything by our instructors, then get thrown into these tests which we don't have the slightest clue how to begin on and our pay is based on these scores. As a group(4 of us) we could do this over a few days without a ton of trouble but I have 4 hours to do this and as individuals no group work.

So I guess my first question is how would I take the csv file and store the data from it into a treemap as follows. Each ID should be the map name, then all the data inside that should be key value pairs.

So something like this for the first map. Map name is 1000 data it contains is {Name: Dara Neal, Age: 19, etc, etc}

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
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Casey Clayton wrote:The last project consisted of getting some basic info from a string and formatting it, nothing like this.


Well the first step in this one is exactly that. Before you can do anything with the data, you need to extract the relevant information from the strings.
So your first step should be to print out the data from each line.
So try completing this simple program first

Note that your csv file actually appears to be tab separated. So you can use the String.split() with '\t' specified as the separator to break up each line.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!!



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.
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I am thinking I could go about this a little different. I could make a class called employee that contains all of the information needed which I could set using that csv file, until I hit the end then just store all of my objects in hash map which would make life easier. Input on this idea?

Here is my barebones Employee class as of now. I will have to add methods to compare these object to each other and some formatting methods, etc.

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



It looks good in notepad++. I decided to take everything in as a string since I don't have to do any math on it so it made things way easier. 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. 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();

 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).



I have 8 fields in the CSV file and I need it to make a new object once it hits that, so in turn restarting the loop, hence why I need to figure out how to name the objects different things.
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I have in the class that handles the creation of new employee objects from the data in the CSV file

 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Delete inner while loop*. What do you think it does?
Delete it*. It just sets the fields in Employee object eight 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
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:Delete inner while loop*. What do you think it does?
Delete it*. It just sets the fields in Employee object eight 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.



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?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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. We don't know what are you trying to read...

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
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 used with separatoe "," 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 reinvent the wheel?
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



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.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)...



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?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Ah yes I do. After looking over my output again mine is doing the same exact thing, sorry I didn't see that before.

1000
Dara Neal
19
Ap #778-655 Vestibulum Road
Beaumont
NC
97394
Macromedia
"Metformin HCl --These 3 lines when it should be "Metformin HCL, Januvia, Abilify" all in one position
Januvia
Abilify"
1010
Odette Tyler
92
4737 Fringilla Rd.
Sassocorvaro
NE
94379
Altavista
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Casey Clayton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Well yea that's what I meant to put.
 
reply
    Bookmark Topic Watch Topic
  • New Topic