• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does anyone know how i can solve this problem? yr help is much needed.



 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, I missclicked quote in stead of edit =(
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouter Oet , I tried out your suggestion to use the arrayList. Ta da!

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh. Noted. Thanks!
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic