You should therefore consider using String#split instead. I would prefer to use a Scanner because that will give the values directly as doubles or some other type directly, but I suspect that is not in the spirit of using tokenizers or String#split. Try ",\\s*" or "\\s*,\\s*" as the regex to split on. The meaning of the comma should be obvious; I think it is not a meta‑character. The \s bit means whitespace and the * means any number (including 0) of repetitions. You have to escape the \ so it turns into \\. I am not an expert on regexes, so test that regex carefully with String#split before you let it loose on an unsuspecting world.StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.
You know how to make a class? Make one called Investment. You know how to add fields? Add the ones I mentioned. Post what you have so far and let us know where you're stuck.Adey Fater wrote:i dont understand what you mean pLease. any example?
Adey Fater wrote:
header1 | header2 |
---|---|
cell 1 | cell 2 |
Carey Brown wrote:Good. Now create a constructor that takes a single String as a parameter. This parameter will be one row from a CSV file. Use split to extract the necessary fields. Then show us again.
Well, you are now calling a bunch of constructors that you haven't created. I was asking you to make (not call) a single constructor inside of your Investment class.Adey Fater wrote:
Where's the constructor you made?Adey Fater wrote:okay...
Don't call constructors methods; the two are different. You cannot call a constructor apart from part of the object creation process, and constructors are not members of the class.Zachary Griggs wrote:A constructor method is a method . . .
Agree about initialising all the instance variables. Good point there
The constructor will be a member inside of the Investment class. There are some rules to writing a constructor:
The name of the constructor must be identical to the class name There is no return type on a constructor (at least, not like on other methods) Constructors should set the initial values of all class instance variables, where needed . . .
That sounds like a factory method, which is common in Java® too, but different languages do different things and feature X in language Y is often not a direct analogue of feature Z in language W.Zachary Griggs wrote:. . . in one of the other languages I use, constructors are declared like "public static method create takes nothing returns thistype"
You will also get a 0 if you plagiarise code. If anybody copies code, the original will be here for all to see.. . .And you should really stop copy/pasting code written here. . . . you'll end up with nonsense code.
Consider Paul's rocket mass heater. |