• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does anyone know how i can solve this problem? yr help is much needed.



 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, I missclicked quote in stead of edit =(
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet , I tried out your suggestion to use the arrayList. Ta da!

 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh. Noted. Thanks!
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you saying "cast" when you mean "parse"?
 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic