I seem to not be able to solve this java.lang.ArrayIndexOutOfBoundsException: 5
I understand the error, but the table is 5x5 and I think I have everything right for printing it.
public static int tulosta_matriisi(int[][] matriisi) {
int i=0, j=0;
for(i = 0; i <= 4; i++) {
for(j = 0; j <= 4; j++) {
if(j == 4 && i <= 4)
System.out.println(matriisi[i][j]);
else if(i <= 4 && j <= 4)
System.out.print(matriisi[i][j] +"\t");
}
}
return matriisi[i][j];
}
To avoid all this problem you have to use :
for(i = 0; i < matriisi.length; i++) {
for(j = 0; j < matriisi[i].length; j++) {
...
When you get out your loop, the i and j will be incremented so you should not return matriisi[i][j] this make this error java.lang.ArrayIndexOutOfBoundsException: 5, so instead you should to return matriisi[i-1][j-1] so in the end your program should look like this :
public static int tulosta_matriisi(int[][] matriisi) {
int i = 0, j = 0;
for (i = 0; i < matriisi.length; i++) {
for (j = 0; j < matriisi[i].length; j++) {
if (j == matriisi[i].length - 1 && i <= matriisi.length) {
System.out.println(matriisi[i][j]);
} else if (i <= matriisi.length && j <= matriisi[i].length) {
System.out.print(matriisi[i][j] + "\t");
}
}
}
return matriisi[i - 1][j - 1];
}
Good luck
Related
I recently completed the code for a Four in a Row Game with 7 columns (represented by i below) and 6 rows (represented by j below), however, I keep getting out of bounds errors and I'm not sure why. If anyone can help spot and fix the errors, that would be awesome. Below is the code I have (the issues lie in the play, isGameOver and winner functions):
package hw4;
public class CFGame {
//state[i][j]= 0 means the i,j slot is empty
//state[i][j]= 1 means the i,j slot has red
//state[i][j]=-1 means the i,j slot has black
private final int[][] state;
private boolean isRedTurn;
{
state = new int[7][6];
for (int i=0; i<7; i++)
for (int j=0; j<6; j++)
state[i][j] = 0;
isRedTurn = true; //red goes first
}
public int[][] getState() {
int[][] ret_arr = new int[7][6];
for (int i=0; i<7; i++)
for (int j=0; j<6; j++)
ret_arr[i][j] = state[i][j];
return ret_arr;
}
public boolean isRedTurn() {
return isRedTurn;
}
public boolean play(int column) {
for(int j = 0; j < state[column].length; j++) {
if(state[column][j] != 0 || state[column][j] < 0 || state[column][j] > 6 ) {
return false;
}
}
return true;
}
public boolean isGameOver() {
for(int j = 0; j < state.length; j++) {
for(int i = 0; i < state[j].length; i++) {
if (state[i][j] != 0) {
return true;
}
}
}
return false;
}
public int winner() {
//Checking horizontal win
for(int j = 0; j < state.length; j++) {
for(int i = 0; i < state[j].length-3; i++) {
if(state[i][j] == state[i+1][j] && state[i][j] == state[i+2][j] &&
state[i][j] == state[i+3][j]) {
return state[i][j];
}
}
}
//Checking vertical win
for(int j = 0; j < state.length-3; j++) {
for(int i = 0; i < state[0].length; i++) {
if(state[i][j] == state[i][j+1] && state[i][j] == state[i][j+2] &&
state[i][j] == state[i][j+3]) {
return state[i][j];
}
}
}
//Checking diagonal(s) win
for(int j = 0; j < state.length - 3; j++) {
for(int i = 0; i < state[j].length - 3; i++) {
if(state[i][j] == state[i+1][j+1] && state[i][j] == state[i+2][j+2] &&
state[i][j] == state[i+3][j+3]) {
return state[i][j];
}
}
}
for(int j = 0; j < state.length - 3; j++) {
for(int i = 3; i <= state[j].length; i++) {
if(state[j][i] == state[j+1][i-1] && state[j][i] == state[j+2][i-2] &&
state[j][i] == state[j-3][i+3]) {
return state[i][j];
}
}
}
return 0;
}
}
To me, the code seems fine but when I run it, it brings up the error.
If you spot any other mistakes, kindly let me know too.
Any help is much appreciated.
You use index j and i differently in different loops. It looks like j should be the index for the first dimension and i is for the second, but you have state[i][j] in some places. That will definitely cause outofbound error because j can go as high as 6 but i is capped at 5 based on your code.
for the given input I need to print the pattern. For example for input = 6 I have to print:
MMMMMMSDDDDDD
MMMMMSSSDDDDD
MMMMSSSSSDDDD
MMMSSSSSSSDDD
MMSSSSSSSSSDD
MSSSSSSSSSSSD
CSSSSSSSSSSSK
CCSSSSSSSSSKK
CCCSSSSSSSKKK
CCCCSSSSSKKKK
CCCCCSSSKKKKK
CCCCCCSKKKKKK
I have tried but couldn't go further than this could anyone help
public class tgk {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int half = ((2*size)+1)/2;
for (int i = 0; i < size ; i++)
{
for (int j = size; j > i; j--)
{
System.out.print("M");
}
for (int k = half+1 ; k > half - i; k--)
{
System.out.print("S");
}
System.out.println();
}
for(int i = size; i > 0; i--)
{
for (int j = size; j >= i; j--) {
System.out.print("C");
}
for (int k = 0; k < (i * 2 - 1); k++) {
System.out.print("S");
}
System.out.println();
}
}
}
if input = 3 it should be
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK
You can use two sets of for loops to print each half of the pattern. Assuming input variable holds the size of the problem
int input = 3;
for (int i = 0; i < input; i++) {
for (int j = 0; j < input - i; j++) {
System.out.print('M');
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print('S');
}
for (int j = 0; j < input - i; j++) {
System.out.print('D');
}
System.out.println();
}
for (int i = input - 1; i >= 0; i--) {
for (int j = 0; j < input - i; j++) {
System.out.print('C');
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print('S');
}
for (int j = 0; j < input - i; j++) {
System.out.print('K');
}
System.out.println();
}
will print for input = 3:
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK
and for input = 6:
MMMMMMSDDDDDD
MMMMMSSSDDDDD
MMMMSSSSSDDDD
MMMSSSSSSSDDD
MMSSSSSSSSSDD
MSSSSSSSSSSSD
CSSSSSSSSSSSK
CCSSSSSSSSSKK
CCCSSSSSSSKKK
CCCCSSSSSKKKK
CCCCCSSSKKKKK
CCCCCCSKKKKKK
I don't know why, but I really wanted it to work with only one set of for-loops:
int number = 8;
for (int i = 0; i < number * 2; i++) {
for (int j = 0; j < (number * 2) + 1; j++) {
System.out.print(
i < number && j+i < number ? 'M' :
i < number && j-i > number ? 'D' :
i < number ? 'S' :
i >= number && i-j >= number ? 'C' :
i >= number && j+i >= number*3 ? 'K' :
'S'
);
}
System.out.println();
}
So for 8 (like in the code) it prints:
MMMMMMMMSDDDDDDDD
MMMMMMMSSSDDDDDDD
MMMMMMSSSSSDDDDDD
MMMMMSSSSSSSDDDDD
MMMMSSSSSSSSSDDDD
MMMSSSSSSSSSSSDDD
MMSSSSSSSSSSSSSDD
MSSSSSSSSSSSSSSSD
CSSSSSSSSSSSSSSSK
CCSSSSSSSSSSSSSKK
CCCSSSSSSSSSSSKKK
CCCCSSSSSSSSSKKKK
CCCCCSSSSSSSKKKKK
CCCCCCSSSSSKKKKKK
CCCCCCCSSSKKKKKKK
CCCCCCCCSKKKKKKKK
...or for 3:
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK
I'm trying to create a square shape with numbers like this:
1234
2341
3412
4123
How would I go about doing that?
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
System.out.println();
}
This prints:
1234
234
34
4
How do I get it to restart at 1 again?
You can do by using modulo
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print((j + i) % 4 + 1);
}
System.out.println();
}
output
1234
2341
3412
4123
You need to update to
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
for (int j = 1; j < i; j++) {
System.out.print(j);
}
System.out.println();
}
add one more for loop
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
for (int j = 1; j <i; j++) {
System.out.print(j);
}
System.out.println();
}
There's probably a simpler way, but this should work:
for (int i = 1; i <= 4; i++) {
for (int j = 3; j <= 6; j++) {
System.out.print ((i + j) % 4 + 1);
}
System.out.println();
}
try this:
public class MyClass {
public static void main(String args[]) {
int count = 0;
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
count += 1;
}
if(count < 4){
int remain = 4 - count; // to identify how many times it goes again
for(int k=0;k<remain;k++){
System.out.print(k+1);
}
}
count = 0;
System.out.println();
}
}
}
Output:
1234
2341
3412
4123
How about this?
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
System.out.print(((i + j + 2) % 4) + 1);
}
System.out.println();
}
If you look at the expected output, it has a pattern, which is restricted to a finite and known sequence of numbers. Modulus is the ideal option to handle cases like this.
for (int i = 1; i <= 4; i++) {
for (int j = 0; j <= 3; j++) {
System.out.print(((i + j) % 4) + 1);
}
System.out.println();
}
Modulus (%) gives the remainder of one number divided by another, so (i+j) % 4 always gives a result between 0 and 3.
Add one to that to get the value you want (between 1 and 4).
I am trying to make the following number line is Java
2,3,5,7,11,13,17 (Prime Numbers)
I tried this code
for(int i =0; i <= 100; i++) {
if(i < 2) {
continue;
}
for(int j = 2; j < 1; j++) {
if(i % j == 0) {
break;
} else {
System.out.print(i + ",");
}
}
}
But it doesn't work
Anyone help please?
Your code is quite poor, but the minimal amount of changes needed to make it work yields this code:
outerLoop:
for(int i = 0; i <= 100; i++) {
if(i < 2) {
continue;
}
for(int j = 2; j < i; j++) {
if(i % j == 0) {
continue outerLoop;
}
}
System.out.print(i + ",");
}
But a further improvement would be to start the first loop at 2 right away:
outerLoop:
for(int i = 2; i <= 100; i++) {
for(int j = 2; j < i; j++) {
// and so on...
EDITED
There are a lot of errors in that code, first of all that second loop is a infinite loop and second one that the System.out.println line should not be in second loop it should be at end of first loop! If you place it in second it will print numbers hundreds of time.
This is the correct code :
for(int i = 2; i <= 100; i++)//begin loop from 2 instead of 0
{
boolean flag = true;
for(int j = 2; j < i; j++)
{
if(i % j == 0)
{
flag = false;
break;
}
}
if(flag)System.out.print(i + ",");
}
You need to set a flag to check if a factor was found outside the loop.
https://discuss.leetcode.com/topic/5056/any-shorter-o-1-space-solution
I took this c++ solution from here and tried converting it into java
public void setZeroes(ArrayList<ArrayList<Integer>> a) {
int colzero = 1;
int rows = a.size();
int cols = a.get(0).size();
for (int i = 0; i < rows; i++)
{
if (a.get(i).get(0) == 0)
colzero = 0;
for (int j = 1; j < cols; j++)
{
if (a.get(i).get(j) == 0)
a.get(i).set(0, 0);
a.get(0).set(j, 0);
}
}
for (int i = rows - 1; i >= 0; i--) {
for (int j = cols - 1; j >= 1; j--) {
if (a.get(i).get(0) == 0 || a.get(0).get(j) == 0)
a.get(i).set(j, 0);
}
if (colzero == 0)
{
a.get(i).set(0, 0);
}
}
}
you need curly braces around your if block because you have split it into two instructions (there was only one in C++).
if(a.get(i).get(j)==0) {
a.get(i).set(0,0);
a.get(0).set(j,0);
}