java adding data to a JTable with a for-loop - java

i need some help writing a for-loop please but i can't get it.
i need to add the numbers 0 to 63 in a table like this:
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
etc.
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
table.setValueAt(""+(i+(j)), i, j);
}
}
but the inputted values are not correct
can you please help

(Assuming you're trying to do this so you count left to right top to bottom and have 8 columns)
You need to offset the value each time for the number of columns you've seen. Every time you iterate through an entire row, you've seen 8 columns, so it's not sufficient to add i and j, you need to add rows seen (i) multiplied by the number of columns (8). Thus you should be printing i*8 + j:
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
table.setValueAt(""+(i*8+j), i, j);
}
}

Related

Creating a specific pattern for list of integers

int[] box = new int[9*8];
for(int i=0; i<9; i++) {
for(int j=0; j<8; j++) {
box[j] = i;
}
}
I've tried everything and it turns out to be way harder than it looks for me. Without using ArrayLists (I understand this works using box.add(i)) I can only use int[] type. I need to create a list of integers that looks like this [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,..8,8,8,8,8,8,8,8] so 8 sets of integers from 0-8. Can anyone help me?
I believe the problem is that on line 4. The code sets a position on to a value, but this position repeats from 0 to 7.
This should work better:
int[] box = new int[9*8];
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 8; j++) {
box[i * 8 + j] = i;
}
}
Basically, it shift the 0 - 7 over 8 places for every new number.

I want to print this pattern in Java

1
333
55555
7777777
999999999
Program to print number pyramid. I want to print this pattern in Java.
My code:
private static void pyramid() {
System.out.println("Please Enter any number less than 10 : ");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int temp = num;
for (int row = 0; row <= num; row++) {
for (int column = 0; column < temp; column++) {
System.out.print(" ");
}
temp--;
for (int k = 0; k <= row-1; k++) {
if (row % 2 != 0) {
System.out.print(row);
}
System.out.println();
}
}
}
And I am getting following output:
Please Enter any number less than 10 :
9
1
3
3
3
5
5
5
5
5
7
7
7
7
7
7
7
9
9
9
9
9
9
9
9
9
System.out.println(" 1");
System.out.println(" 333");
System.out.println(" 55555");
System.out.println(" 7777777");
System.out.println("999999999");
Your teacher probably wants you to use loops.
So you should note that the bulk standard for loop which programmers can bang out with their eyes closed:
for (int i = 0; i < someNumber; i++) {
//..
}
Is fairly configurable. For instance the i++ at the end means to increment i, but we could increment by bigger amounts (or decrement, or do some other funky things like stepping through a list of objects and so on and so forth).
e.g.
i += 3;
Will increase i by three.
You can also nest loops inside one another, e.g.
for (int i = 1; i < 10; i+=2) {
String s = "";
for (int j = 0; j < i; j++) {
s += i;
}
System.out.println(s);
}
The padding at the front I leave as an exercise to the reader.
Note that this pattern (one loop inside another, and the inner loop being bounded by the outer loops counter) is actually quite common 'out in the wild', and so is worth investing the time to understand.

generate specific numbers for row and column in java

i want to write a code that makes a specific numbers in the loop .
for example generate numbers like this :
column 1 - row 1
column 1 - row 2
column 1 - row 3
and then generate this ( for example):
column 2 - row 1
column 2 - row 2
column 2 - row 3
column 2 - row 4
is there any way to write this Algorithm ? thank you so much
code , i have a code like this but i want to manage it :
boolean[][] mapEq = new boolean[mapWidth][mapHeight];
int free = mapWidth * mapHeight;
int randomFree = ((int) (free * Math.random()));
int x = 0, y = 0;
for (int i = 0, k = 0; i < mapWidth; i++) {
for (int j = 0; j < mapHeight; j++) {
if (!mapEq[i][j]) {
if (k == randomFree) {
x = i;
y = j;
break SearchRandom;
} else {
k++;
}
}
}
}
it creat a random thing but i want to make a specific number for example
for column 1 - row 1 and row 2 and row 3
and for column 2 - row 1 and row 2 and row 3 and row 4
Maybe something like this?
int[] columnLengths = {3, 4}; // Lengths of the columns
for(int i = 0; i < columnLengths.length; i++) { // Loop the columns
for(int j = 0; j < columnLengths[j]; j++) { // Loop the rows
System.out.println("column " + i + " - row " + j);
}
}
I am really not sure what you are trying to make, but this is what I understood.

Code for Numerical Pyramid in Java Programming [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Create a java code that display the following figure:
1
2 3 4
3 4 5 6 7
4 5 6 7 8 9 10
Hey guys! New on here. We have an exercise to do involving java programming and for some reason It seems to get my head blows out in solving this problem. There's what I have done so far but the output is incorrect.
public class Pyramid {
public static void main(String[] args) {
int size = 10;
for (int row = 1; row <= size; row++) {
int j = 1;
int i = 1;
int counter1 = 1;
int counter2 = 1;
// print space
while (j++ <= size - row) {
System.out.print(" ");
}
// count up
j = size - (size - row);
if (j == 10) j = 0;
counter1 = 1;
while (counter1 <= row) {
System.out.print(j);
if (j == 9 && counter1 != row) j = -1;
j++;
counter1++;
}
j = j - 2;
counter2 = 1;
// count down
while (counter2 < row) {
System.out.print(j--);
if (j < 0) j = 9;
counter2++;
}
System.out.print("\n");
}
}
}
I can see two major mistakes in what you posted.
10 is the highest number in the pyramid but it has only 4 rows. So size should be equal to 4 (unless it is just an example and the number of rows isn't important).
I don't see any reason why you make a countdown. You can see that the numbers are always increasing in the same row.
In addition to that:
You should separate the numbers by a blank. The numbers in your example were stuck to each other. This imply that you have to change the number of spaces before each row. (In your example you can simply double the spaces)
Really not important: you can use System.out.println() when you want an new line.
Here you are doing counting down after the mid point of each raw. So instead of increasing number, you are decreasing in the count down part (j is decreasing).
And here you need 4 numbers of raws. but u have programmed for 10 numbers of raws with some conditions (here you don't need to specify the highest number. You just input the number of raws is enough. Please find the Logic below).
And even though you need to increase the number of raws, the logic should not be changed.
Logic:
For each raw the number of elements should be the raw's odd number and the 1st element of each raw should be started with the raw's number.
for example,
if we consider 3rd raw, then 3rd odd number is = (3*2) - 1 = 5. So number of elements in the raw is 5.
And the start element of 3rd raw is 3.
So raw will be:
3,4,5,6,7
Please try below code:
int n = 5;
for (int i = 1; i < n; i++) {
int odd = (i*2) - 1;
for (int j = 1; j <= odd; j++) {
if (j==1) {
for (int x = n; x >=i; x--)
System.out.print(" ");
}
System.out.print(((j + i)-1) + " ");
}
System.out.println();
}
Output is:
1
2 3 4
3 4 5 6 7
4 5 6 7 8 9 10
Try this
class Pyramid {
public static void main(String[] args) {
int myLevel;
int i, j, k;
int x = 1;
myLevel = 6;
for (i = 1; i <= myLevel; i++) {
for (k = 1; k <= myLevel - i; k++) {
System.out.print(" ");
}
for (j = k + 1; j <= myLevel; j++) {
System.out.print(x);
}
for (int l = myLevel; l > k - 1; l--) {
System.out.print(x);
}
x++;
System.out.println("");
}
}
}

how to calculate the sum of elements after the diagonal in 2D array in java?

I have a function that I need to calculate the sum of elements after the diagonal in the 2D array, but the problem is that the function return the sum of the elements in the diagonal.
What I need is that if I have a matrix like this:
1 2 3
4 5 6
7 8 9
The elements of the diagonal are = 1 5 9
What I need is to calculate the numbers that follow after these diagonal numbers, so it will be like this:
1 2 3
4 5 6
7 8 9
sum = 2+3+6 = 11
I would appreciate it if someone could help me to fix my problem.
this my code:
public int calculate(){
int sum = 0;
for(int row = 0; row <matrix.length; row++){
for(int col = 0; col < matrix[row].length; col++){
if(row == col){
sum = sum + row+1 ;
}
}
}
System.out.println("the sum is: " + sum );
return sum;
}
public static void main(String[] args) {
int[][] ma = new int[2][2];
Question2 q2 = new Question2(2, 2, ma);
q2.fill();
q2.calculate();
}
the output is:
2 1
2 1
the sum is: 3
You want col to go through all elements, being always bigger than the diagonal.
Therefore try:
int sum = 0;
for(int i = 0 ; i < a.length ; ++i) {
for(int j = i + 1 ; j < a[i].length ; ++j) {
sum += a[i][j];
}
}

Categories