Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with scanner reading past end of line  RSS feed

 
Vishal Ramcharan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I actually have it workign using string.tokenizer, however i know this is depricated

StringTokenizer isn't deprecated, but it is a legacy class and it's use is discouraged.

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

I've never tried changing a delimiter pattern part way through scanning so don't know if there are any implications to doing this.
Personally I would either use 2 scanners or I'd be more likely to read in a line at a time and split each line at the commas using the String's split() method.
 
Vishal Ramcharan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

You already have the data in an array so why do you want to assign it to variables and then back to an array?
If you want to do anything with the data other than read it in and print it out then you should really create a class (possibly called Product) which has the 4 fields in it. Then you create an instance of Product for each line you read in and set the value of each of the fields. I suggest you store the Product objects in a collection such as an ArrayList.

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

Use one Scanner to read in each line and use a second Scanner to break the line you have just read in into comma separated parts.
 
Vishal Ramcharan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure how to assign a string of values to an object without going through the string and splitting the string first.

The simple answer is you can't unless your class has a constructor/method that takes a concatenated String of values but then it has to do the splitting, so something has to split the String to extract the values.

Calling the split() method gives you an array of Strings in the order they where in the original String. You are going to have to make the assumption the order is correct as there is no easy way of handling malformed data. So to populate the Product object you have several choices such as you can pass all values into a constructor or create an instance using a no parameter constructor and then call methods to populate the values or have a static factory method that creates an instance for you from the original String or array of data. Given there are only 4 values it's probably easier to use a constructor which takes all 4 parameters.
 
Vishal Ramcharan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is because you are passing the array to the constructor but you don't have a constructor that takes an array. If you have a constructor that takes the 4 arguments then use that as follows:

You might want to add some checks to make sure you have an array with 4 elements etc before trying to use it.
 
Vishal Ramcharan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!