• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

troubleshooting

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey can anyone help me figure out what's wrong with my array method? its commented under Question 3








Thanks!
 
Marshal
Posts: 27368
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Jessica!

So... what makes you think there's something wrong with that method?
 
Jessica Benady
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thank you for answering! Because when I run the program with the values 100, .1 and 4, I get this:

"The amount of money accumulated after each period in time is [D@50fba502"

which tells me somethings wrong.. would you happen to know what?
 
Paul Clapham
Marshal
Posts: 27368
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that's just what you get when you feed an array of doubles to System.out.println. If you want to actually see the contents of the array, you have to write some code to output each entry separately.

But I don't understand why you think that has something to do with the generateBenfordNumbers method, when you don't even call that method anywhere.
 
Jessica Benady
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I've only been using Java a couple of weeks, I've sorted that problem now, now I have a new one! This i my code for an assignment so far, it's a little messy..





I havn't included the last bit because its incomplete, but when I try and compile this part it says '[' expected for line where I cast my double array into an int in the main method: " "


would anyone happen to know why?
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jessica Benady wrote:
int [] benfordWholeNumbers = new int [benfordNumbers.length];
for (int i= 0;i< benfordNumbers.length; i++) // don't put less than or = ?
{
int benfordWholeNumbers [i]= (int) benfordNumbers[i];
}"


would anyone happen to know why?


Jess, you have benfordNumbers array declared as a double and are trying to cast it as int. That's illegal for arrays.

From the language spec section 5.5.1 (I think SC=source class and TC=target class):

If T is an array type TC[], that is, an array of components of type TC, then a compile-time error occurs unless one of the following is true:

TC and SC are the same primitive type.

TC and SC are reference types and type SC can undergo casting conversion to TC

.

If you put it in a "for" loop you can cast each double to int and then assign to the int array.

I am not sure why that particular warning about the "[" char except that is where it figured out there is a problem with the cast.

Arrays are a different kind of animal. They don't cast like their contents but you can cast each cell of contents one by one, down and dirty.

p


 
Marshal
Posts: 76416
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don’t think there is anything wrong with the cast. what you are doing is declaring it twice. Remove the int from the middle of that loop. You are re‑declaring that array. What you want is the i-th element, which you should get if you delete int.

Have added code tags to your code, and you can see how much better it looks; always use the code button. Unfortunately it also highlights your inconsistent indenting. [Added as edit:] It also highlights the long comments, which make the code difficult to read. Have a look at some suggestions about indenting, etc.
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic