• 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

two-dimensional array

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Why doesn't the array result in a value? The indexes and the value in the first occurrence are passed to the display() method.
I can't post to the board if I paste the code in the message, so here's the link to the code.
Thanks!
http://flashpages.prodigy.net/dwolfing/question.htm
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Douglas.
I'm a little confused by what you're trying to do.
Here is the code, I had to replace i's and j's with j's and k's, to avoid HTML italicization:

First off, the syntax on your for-loops is incorrect. From the Java Language Specification, "The for statement executes some initialization code, then executes an Expression, a Statement, and some update code repeatedly until the value of the Expression is false." So to print the numbers from 0 to 9,

The name of your method is display(), but it looks like you are not only wanting to display the contents of the array, but also populate it with random numbers. Maybe you could split this population out into another method, and then call display() after the 2D array is loaded? And do you want the random integers that you populate the array with to be from 0 to 6, or 0 to 9? Also, you should cast Math.random() to an int only after you multiply it by something, or you'll always get 0 as a result.
We'll be better able to steer you in the right direction if you tell us what you need this class to do, Douglas.
Thanks,
Art Metzer
 
Douglas Wolfinger
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I was casting the value made by Math.random() to an int which, as you pointed out, always leads to a value of 0! The problem I was having that each occurence after the one passed to the method was 0. So I moved the (int) cast outside the whole calculation. And I'll make it more organized with a calculation method and display method, as you suggested.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic