Connect and share knowledge within a single location that is structured and easy to search. The best i have been able to do is read it in a streaming fashion, but I cant store it in an array due the variable size of the file. I belive that ArrayList is a method to deal with variable size array, but I don't know how to work with it.
Essentially I want to be able to access the String array "values" after the while loop finishes. Y is perfectly fine, I try to provide a solution that is closer to your code and maybe easier for you to follow:. I added 3 more imports: ArrayList , Arrays and List - you will see very soon for what they are good. They are all taken from the java. Every class name in Java starts with a capital letter by convention - it will build with a small letter as well, but you should get used to this convention - I "fixed" this in your code.
This might look a little confusing at first, but what it means is that we create a variable lines that holds the results of our parsing. With the lines. This allows your lines to have variable length. The last lines I added simply print the content of 2-dimensional array and should give you a good example for how to access the values in this array.
If you need further help for this construct, check the foreach loop documentation. I am new to java and wold be open to any method that reads a csv into a file that a beginner could understand. If you are not familiar with how List and ArrayList works in Java, please refer to pretty much any Java tutorials online including the Oracle Tutorial.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 5 years, 3 months ago. Active 1 year, 5 months ago. Viewed 65k times. Scanner; import java. FileNotFoundException; import java. The headers will contain the first row of the CSV file, while the rows will contain all the values, from the second row to the last.
This can be achieved by first slicing the string, then use the split method to split a string into an array. First, you need to map the rows array and split the values from each row into an array. Then, you need to use the reduce method on the headers array, returning an object with each header as the property name and the data from values at the same index as the property value. Finally, you just need to return each mapped row as the array element.
The full function code is as follows:. With that, your csvToArray function is finished. You just need to call the function from the onload event:. Depending on the data returned from your request, you can either use the FileReader to read the CSV content as a string first, or you just need to parse the string as an array if you already receive a string from your API. Also, pay attention to the delimiter of your CSV file. Standard Template Library. Python Python Tutorial.
Python Programs. Python How Tos. Numpy Module. Matplotlib Module. Tkinter Module. Network Programming with Python. Learn Web Scraping. More in Python Python Compiler.
Java Core Java Tutorial. Java Type Conversion Examples. Java Wrapper Class. Java 8. Java More in Java Java Compiler. Computer Sci. Computer Architecture. Computer Network. Database DBMS. Learn SQL. Practice SQL. More Tutorials Game Development. GO Language.
0コメント