Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Logic Error on printing 2d array :( help please

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


output:
+-+-+-+
|1|2|3|
+-+-+-+
|2|3|4|
+-+-+-+
|3|4|5|
+-+-+-+

i wanted to extend the columns by 5. i already put a variable 5. but nothings happen. help me please!
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error is at line 20 in the for loop condition.
Can you spot it?
 
mark patindol
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol. i see it already. i change the value of data.length to j.

thanks for fast response.
by the way. how can i get a random value using this line
data[r][c] = t <= j ? t : t - j;

i dont what formula to have random value.

i wanted the output to be like this..


|55|12|4|36|29|... and so on.. i google the formula but i fail to compile.

|2|3|-1|0|1|

|3|-1|0|1|2|
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use java.util.Random class.
Maybe you copied some code you googled but forgot to add an import statement?

I can only guess.

You said you copied some code from the internet (what code?) and it gave you a copilation error (what error?)
Please, TellTheDetails and ItDoesntWorkIsUseless. (those are links)
 
mark patindol
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im done on putting a random number using .Random

my next task is... when the user enter 1. i need to print the largest value among all the values inside the table. i really dont have idea where i put my code.

this is my work done so far..
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mark patindol wrote:i really dont have idea where i put my code.


Inside a method. Possibly called findMax.

Your method is way too long. You need to split it into smaller parts.
Generating an array should be in a sepatate method.
Printing an array should be in a sepatate method.
Handling user input / interaction with an user should be in a separate method.
Finding maximum entry in an array should be in a separate method.
Etc...
 
Marshal
Posts: 76486
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
mark patindol
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



output:


actually i get this from my last program that i encode. my 2nd task is to produce a random value in each columns and row..
 
Campbell Ritchie
Marshal
Posts: 76486
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want an array containing randomly‑chosen numbers? You can extract a Stream from a Random object. You can also find code which will do it on the Ranch somewhere. Try here and associated links.
 
Campbell Ritchie
Marshal
Posts: 76486
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These two threads seem to have such similar material so I think they are better merged into one thread.
 
mark patindol
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:

mark patindol wrote:i really dont have idea where i put my code.


Insida a method. Possibly called findMax.

Your method is way too long. You need to split it into smaller part.
Generating an array should be in a sepatate method.
Printing an array should be in a sepatate method.
Handling user input / interaction with an user should be in a separate method.
Finding maximum entry in an array should be in a separate method.
Etc...



Okay.. But what will be my main method here?
 
Campbell Ritchie
Marshal
Posts: 76486
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Main methods are really easy to write. They should be short, preferably one statement. Example:-…and that doesn't help you write your findMax method
 
Goodbye moon men. Hello tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic