aspose file tools*
The moose likes Java in General and the fly likes Datatype casting. Split Strings to double 2D array. (loss of precision) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Datatype casting. Split Strings to double 2D array. (loss of precision)" Watch "Datatype casting. Split Strings to double 2D array. (loss of precision)" New topic
Author

Datatype casting. Split Strings to double 2D array. (loss of precision)

Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
does anyone know how i can solve this problem? yr help is much needed.



Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 145

I'd really like to help you solve your problem, but... what exactly is your problem? Seems you forgot to mention it.
And please, UseRealWords.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
Haha. I meant to say, I am having difficulty initializing the array because there is a loss of precision at runtime. The casting of the String into a double datatype. I have listed it in the comment in the code. I am not sure why.
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Creating arrays requires int not double. In Java you need to explicitly downcast, because of the possible loss of precision. There are at least two ways of fixing you code:



or ...



First way is preffered

Hope this helped.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
sorry, I missclicked quote in stead of edit =(
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

When I helped you out with this question in the performance category and noted that it should not be there I didn't mean open a new topic (UseOneThreadPerQuestion).
I'm still assuming because you don't give us much information. I think you want something like this:


But with this approach you can end up with a partially filled array. Especially if the value of MAX_LINE_IN_FILE is high. You might want to consider an approach with something more flexible then an array such as an ArrayList


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
*reply to Wouter Oet further down...


Hey i followed Martin's approach and I can run it. Only that I want to print out the array to make sure the elements are coming through. However, it doesnt seem to be printing and I can't seem to solve the problem. Sorry to trouble you again.





*and Wouter Oet - I think you could advise me in case i am doing it the wrong way...

Ok. I don't really know what information i need to give you before you'd understand. But I think this would help,

you see, my text file contains the coordinates this way

"0.0 0.0
0.0 0.0
101.601 38.534
103.062 38.513
103.354 38.323
103.025 37.252
103.8 37.438
105.274 37.822
103.166 37.064
...."

thereafter, i downcast it from Strings to int then to doubles in 2D array.

What i hope to acheive is something like this:

element 1 [[0.0][0.0]]
element 2 [[101.062][38.534]]
element 3 [[103.354][38.323]]
....


Please tell me whether this is correct. I think I should define 2 columns only in the 2D array initialisation as oppose to the arrays of arrays that i am getting now.. How should I do it?

Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
Wouter Oet , I tried out your suggestion to use the arrayList. Ta da!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38862
    
  23
Beware of lots of spaces before your //main method comment; it makes the code difficult to read because of the long line. I have deleted the comment.
Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
oh. Noted. Thanks!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It's an improvement but I still see no use of the ArrayLists and the values in the ArrayList are strings not doubles.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38862
    
  23
Why are you saying "cast" when you mean "parse"?
Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
Sheriff -I'm sorry. I'm not that familiar with Java terms yet.
Rancher - I realised that there's another way round to implementing my algorithm, which doesnt require me to change the datatype. I can just them keep in Strings first.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Datatype casting. Split Strings to double 2D array. (loss of precision)