Vishal Ramcharan

Greenhorn
+ Follow
since Feb 20, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Vishal Ramcharan

Thank you!! that works perfectly! i did make a constructor to accept all 4 variables - i just kept thinking i had to assign values to the variables before passing them to the constructor. I also forgot that string.split() automatically creates an array of sorts of the string that it splits.

This is what i ended up having as my code -Tony thank you very very much!



my intention is to now move this code to the Product.class so it can be called from the main method to create catalogue's from different files.

thank you!
11 years ago
lol this brings me right back to what i am trying to do - to split a string so that i can create an object using that data.


i get the following error -

ProdWriteToFile.java:47: error: no suitable constructor found for Product(String[])
Product p = new Product(line.split(","));
^
constructor Product.Product(String,String,double,String) is not applicable
(actual and formal argument lists differ in length)
constructor Product.Product() is not applicable
(actual and formal argument lists differ in length)
1 error

Process completed.
i do have a Product.class file defined with constructors and getters and setters. What i want to do is indeed pass a string to a method to create an object from it. I am just not sure how - the only way i know how to do it is to split the string. assign each split to avariable, pass the variable to the method to create the object. It's all i have been shown this far.

thanks,
11 years ago

Then you create an instance of Product for each line you read in and set the value of each of the fields



This is exactly what i am trying to do - i am just not sure how to. I have my data stored in a csv file. I want to read that data and assign each line to a new Product object. This is why i am trying to separate the string by "," so i can assign each part to a variable and them have a new Product(prodCode, prodDescription, prodCost, prodUnit)

I do have a product class file - I am just not sure how to achieve this other than the way i currently am doing it. I am not sure how to assign a string of values to an object without going through the string and splitting the string first.

11 years ago
the only way i have figure out how to get it working with string.split() is with the following :



Is there a better way to do this? I want to eventually create an array of objects so i want to assign each part of the string to a variable, it just seems a bit wasteful to read it into one array, then assign the variables, then create a second array of objects. I'd much prefer finding a way to do this using only one array.

I am not sure how you would use two scanners - can you elaborate a bit more please?

thanks
11 years ago
Hello all.

I have started learning java and i need some help on an issue i am having. I am trying to create a program that writes 4 variables to a file (separated by commas ",") and then prints out the file line by line in a formatted manner

I actually have it workign using string.tokenizer, however i know this is depricated and am trying to get it working using string.split(",") or .useDelimiter(",")

the problem i am having is that my code seems to go past the end of line character picking up the first variable of the next line and adding it to the last variable of the previous line. the loop is only running twice and then erroring out.
It is also printing the “.” At the beginning of the first value of the next line instead of the end of each line.

I have tried several variations and i have no idea as to why it is doing this. any help at all would be appreciated. Below is the code i am using



the file is a csv file and the contents are in the format :
P3847,Condensed Powdered Water,2.5,per packet
K3876,Distilled Moonbeams,3.0,a dozen
Z9983,Anti-Gravity Pills,12.75,for 60
Q9887,Powdered Fairy Dust,20.5,per pack


the output i am getting is here : http://snag.gy/9mBqI.jpg
11 years ago