简体   繁体   中英

reading in csv file and storing in arrays

the practice question i got says that i need to

create a java code that reads in csv file with name and height.

to read a file you must get a file name from user as string.

then you must store contents of file into two arrays one for name (string) and height(real number).

You should read the file at least twice, once to check how many students are in the file (so you know how many students you need to store) and a couple more times to actually read the file (to get the names and height).

then prompt the user for name you want height of. it should output the height for userinput.

example csv file is

chris,180

jess,161

james, 174

its not much but this is all i could come up with i have no idea how to store name and height separately and use that array to output the results. and would i need to use split somewhere in the code? i remember learning it but dont know if its used in this situation

import.java.util.*;
private class StudentNameHeight
private void main (string [] args)
{ 
    String filename;
    Scanner sc = new scanner(system.in);
    System.out.println("enter file name")
    filename = sc.nextline();
    readFile (filename);
}

private void readFile (String filename)
{
    FileInputStream fileStrm = null;
    InputStreamReader rdr;
    BufferedReader bufRdr;

    try 
   {
        fileStrm = new FileInputStream(filename);
        rdr = new InputStreamReader(fileStrm);
        bufRdr = new BufferedReader(rdr);

        // ?


    catch (IOException e)
    {
        if (fileStrm != null)
        {
           try {fileStrm.close(); } catch (IOException e2){}
        }
        System.out.println("error in processing" + e.getMessage());
    }
}

im new to java so, any small tip or help would be great

thanks

You code looks messy. As far as I understand from your question, you are willing to read a CSV file containing two entities, one is name and another is height and store these two entities in two different data structures. I'm teaching you a simple way to accomplish this in below code snippet.

public void processCSVFile(String filePath){
        try(BufferedReader fileReader = new BufferedReader(new FileReader(new File(filePath)))){
            //Create two lists to hold name and height.
            List<String> nameList = new ArrayList<>();
            List<Integer> heightList = new ArrayList<>();

            String eachLine = "";
            /*
             * Read until you hit end of file.
             */
            while((eachLine = fileReader.readLine()) != null){
                /*
                 * As it is CSV file, split each line at ","
                 */
                String[] nameAndHeightPair = eachLine.split(",");

                /*
                 * Add each item into respective lists.
                 */
                nameList.add(nameAndHeightPair[0]);
                heightList.add(Integer.parseInt(nameAndHeightPair[1]));
            }

            /*
             * If you are very specific, you can convert these
             * ArrayList to arrays here.
             */
        }catch(IOException e1){
            e1.printStackTrace();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM