• Post Reply Bookmark Topic Watch Topic
  • New Topic

cvs file parsing issues from arraylist  RSS feed

 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
]Hi all,

Im doing an excersize in java since i have a big exam in like 3 weeks and I´ve googled for like 5 days now tried different things without result but now I have like 3 hairs left on my head so I need to ask because I have issues i cannot solve no matter what i do.

I have to read this CSV file into an arraylist. Arraylist i call weight. The data in the csv file has 200 double values which is the weight of all the 200 bananas. Since the csv file is a string i guess I cannot create an double arraylist from scratch, but i have to parse the csv file afterwards, right? Well this does not work and ill show you the code in a bit, just need to state all my issues.

My second issue is how to be able to print out the the weight items (in this case bananas) next to the weight of the bananas taken from the array?
This has to be done by the help of a constructor i assume? I just dont know how constructors work with arraylists.

Id like it to look something like this in the output window.

Banana 34,55
Banana 43,55
Banana 23,74

and so on (200 times)

so these are my issues, how to parse and how to print the arraylist weights and the item banana in the output window with the help of a constructor. Here is the code.








Thanks in advance
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that the contents of the file that you showed on screen? That is not a CSV, but an ordinary text file.
It may be easier to use the next and nextDouble methods of a Scanner object. If you go through the Scanner documentation you will see how to use a different delimiter for the default. Beware: you probably cannot simply use "." but must escape it to "\." or more likely "\\."
That is because . is a metacharacter for regular expressions.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why Campbell doesn't think that's a CSV file (it is), or what the difference would be between a text file and a CSV file (there is none), but my first question is: why aren't you using one of the existing CSV libraries rather than rolling your own?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Andersen wrote:]I´ve googled for like 5 days now tried different things without result but now I have like 3 hairs left on my head so I need to ask because I have issues i cannot solve no matter what i do.

Believe me, Patricia, we've ALL been there. Just a bit longer ago for some of us than others.

My first piece of advice: StopCoding (←click).

I suspect you're getting panicky because you think you need to solve all your problems at once, and you DON'T. What you do need to do is to take a step back from your code for a while, chill out, and write down WHAT you need to do - Note: WhatNotHow (←click) - and break the problem up into manageable pieces.

As I see it, your first problem is that you don't know how to convert a String to a double (or indeed, a Double, which is what you'll need to store in an ArrayList).

So I'll give you that one for free: Double.valueOf(String).

There is another method called Double.parseDouble(String), but that returns a double, not a Double.
Look it up in the API docs, and write a little test class to make sure you understand how it works. It only needs to be a few lines in a main() method, supplying some string literals to valueOf() - ie, keep it really simple. You're writing a test program, not a sophisticated system.
You might want to supply an invalid String like "xyz" though; just to make sure you understand what happens when you do.

Once you've done that, come back to your program and see how you might plug it in to your existing code.

Tip: Since your ArrayList is going to contain Doubles, you should specify that in your definition, viz:
ArrayList<Double> weight = new ArrayList<>();

Indeed you should ALWAYS specify a type for any collection you create.

Another, slightly more advanced tip:
List<Double> weight = new ArrayList<>();
is almost always better than:
ArrayList<Double> weight = new ArrayList<>();
because the rest of your program shouldn't usually need to know what type of List it's working with. A List is a List, and they all behave the same.

HIH; and come back if you have any more problems. But deal with one thing at a time ... I think you'll be a lot less frustrated.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!