Results 1 to 9 of 9
  1. #1
    brian123321 is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Working with information from a CSV file

    I hope this is the correct subforum for this question.

    I have a little knowledge of programming. I have played around with writing programs to read csv files and print out the information in them.

    I am looking to create a program that will read a csv and store the information so that it can be accessed by the user. For example, I have a csv that has 5 columns. The first column contains a name and other 4 columns contain different information about that name. After gathering the data from the csv i would like the user to be able to input two names out of the csv and the program can grab those two names and the information associated with them and run some calculations that i would define to spit out an answer.

    I am a little lost on how to go about doing this. I am not here looking for someone to provide me code. I would like some guidance on how to go about doing this. I think that if i can figure out how to store and then grab the information i could figure out the rest. The storing and grabbing of the information is the part that is boggling my mind right now.

  2. #2
    brian123321 is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Working with information from a CSV file

    Lets see if i can expand on this a bit without being confusing.

    Each line in the CSV is a name with statistics.

    The first column of the CSV is the name, the four columns after that are statistics related to that name only

    After reading the CSV file i need to be able to enter in 2 names and have these statistics, which are numbers calculated against each other. So i cant just return those two names and the statistics they each have bc i need to run calculations using the statistics.

    A quick question?

    Could i read the CSV and create 5 arrays containing the information in the corresponding columns and then use the array numbers as a way to quickly grab all the info for each of the names i want to compare. Using the array numbers in the math formula?

    I feel like this is a way to do it but that would require me to go into the csv and figure out what number in the array a given name was which would take a little time and therefore not ideal.

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Working with information from a CSV file

    Well, assuming you're using a BufferedReader for this, you could do your standard loop to read a line, then call the readLine() method 4 more times in the loop and set the returns to variables. These would be your 4 stats.

  4. #4
    brian123321 is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Working with information from a CSV file

    Quote Originally Posted by zFollette View Post
    Well, assuming you're using a BufferedReader for this, you could do your standard loop to read a line, then call the readLine() method 4 more times in the loop and set the returns to variables. These would be your 4 stats.
    Could i get a little expansion on the setting variables part.


    So for example to make this simplistic lets say i have the following 2 lines(the csv i am using has 350 plus).

    brian, 100.7, 77.1, 65.9, 70.1
    john, 99.6, 56.7, 60, 87.1

    after the csv is read i want to take those two by the user inputting

    name1: brian
    name2: john

    and then the numbers are calculated. An example of a calculation would be (100.7+56.7)/2=?

    so when setting the variables the variables it will be in a way that is associated with the name? If so then i need to look into this further and do some more studying.

    for example in the equation the 4 columns would be a, b, c, d

    and i would enter in 2 names and then press enter then the program would go to the name and plug in the numbers associated with that name.

    in the example equation above it would be (a+b)/2 with the a coming from name brian and the b coming from name john.

    ill look into this a little more tomorrow. I am heading out for the evening.

    Thanks for your help.

  5. #5
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Working with information from a CSV file

    My bad, I failed to realize the format of the file.

    You would have a loop to read every line of the file, and since a line contains the name and the data, you can use the String.split(String regex) method to split up each part of the line.

    Java Code:
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
    
    while ((line = br.readLine()) != null) {
    String[] split = line.split(", ");
    String name = split[0];
    String stat1 = split[1];
    //And so on
    }
    Last edited by zFollette; 01-01-2014 at 08:55 AM.

  6. #6
    brian123321 is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Working with information from a CSV file

    Quote Originally Posted by zFollette View Post
    My bad, I failed to realize the format of the file.

    You would have a loop to read every line of the file, and since a line contains the name and the data, you can use the String.split(String regex) method to split up each part of the line.

    Java Code:
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
    
    while ((line = br.readLine()) != null) {
    String[] split = line.split(", ");
    String name = split[0];
    String stat1 = split[1];
    //And so on
    }
    Thank you for the help. I have all of that working now.

    I do have more questions though now. I been thinking about this a little a bit. When writing out the next part of the program, the part that actually looks to see what two names to compare and do the calculations i am not sure how the best way to go about this is.

    In the code above i see that it is reading the CSV and creating the variable name and is assigning name the first column of the csv. It is then creating the variable stat1 and assigning it the second column of the csv and so forth. The problem i am having here is that i need to use two names and their stats. This is where i becoming lost.

    I am also lost on how to access a given name in the csv using user input. It might be that i am just not familiar enough with Scanner. Maybe there is something else i should be using?

    It is pretty late and i am pretty tired right now so i may not be able to express myself clearly. I am going to play around with it for a while and see where it gets me. Hopefully tomorrow after some rest i will be able to more clearly state the predicament i am in and where i am having trouble. Thanks again for the responses.

  7. #7
    brian123321 is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Working with information from a CSV file

    Also another thought. For what i am trying to do am i going about this all wrong? Should i be creating a database and putting the information into that and then creating a program that asks the end user to input two names then access the database grab the two names and calculates the equations for their stats?

  8. #8
    brian123321 is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Working with information from a CSV file

    using Scanner how am i able to grab a certain name and its associated stats? Or is using a data base the correct way to go about this?

  9. #9
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Working with information from a CSV file

    Ok, I challenged myself to recreate your problem, I have made a set of classes with javadocs that let me read from the file, add things to a custom HashMap I made (to allow duplicate entries) and remove from it, as well as getting the name and stats by index from the Map. I will Private Message you about it.

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2012, 04:01 AM
  2. need to read information from file into array
    By sadeem in forum New To Java
    Replies: 8
    Last Post: 05-15-2011, 11:00 PM
  3. add information to HTML file
    By newbieal in forum New To Java
    Replies: 2
    Last Post: 10-03-2008, 09:59 PM
  4. Replies: 3
    Last Post: 04-07-2008, 07:02 PM
  5. how to get information of a file
    By leonard in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 06:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •