Apologies for what is probably a simple question but I am just a beginner and trying to learn what I can of Java.
I currently have a 3D array (array[][][]). What I want to do is loop through the array and save the third value to a variable.
for (int x = 0; x < 100; x++) {
for (int y = 0; y < 100; y++) {
(array[x][y][value]);
int savedValue = value;
I guess I have something very small wrong but would be very grateful if somebody could help me out.
Related
In a checkers game for my CS class I have run into trouble with counting how many of a specific color piece are on the board.
Here is the getter method:
public int getCheckersBlue()
{
int counter = 0;
for(int x = 0; x <= 8; x++)
{
for(int y = 0; y <= 12; y++)
{
if(c[x][y].equals(Color.BLUE))
{
counter++;
}
}
}
return counter;
}
The constructor for c:
private CheckersBoard[][] c = new CheckersBoard[8][8];
Whenever trying to run the game in Greenfoot a null pointer exception is thrown even when the code compiles. Even with everything declared, everything has something it's pointing to. Any suggestions?
I see two major problems in your code:
You're iterating over 0-8 elements for x and 0-12 for y, but in declaration you has 0-7 for x and the same for y
NullPointerException. This caused because array declaration will fill your array with null values, so you're comparing null with Color.Blue which is incorrect.
With the both fixes code will look like this
public int getCheckersBlue()
{
int counter = 0;
for(int x = 0; x < 8; x++)
{
for(int y = 0; y < 8; y++)
{
if(Color.BLUE.equals(c[x][y]))
{
counter++;
}
}
}
return counter;
}
But I think it's still logically incorrect.
Okay, a few of things...
You are iterating through 12 every time in the inner loop when it should be 8.
Color.Blue is a JavaFX Paint, I believe. This is not really what you should be using. Unless you have made your own Enumeration type of Color, you should do so. Here is a link for how to do so with relevant examples.
You are checking equality of two different types: CheckersBoard and Color. You should have a CheckersBoard.getSpace(x, y).getColor() or CheckersBoard.getSpace(x, y).getOccupancy() (which should have a NoOccupancy option) if we want to be completely logistically sound with our naming.
As a short preamble I'd like to apologize for asking such a positively stupid question.
Now, the proper problem - we're tasked to program a simple Mean/Median (both) Filter for our study program, which will later serve as an introduction and basis for more advanced filter types.
I've studied how the kernel 2D matrix works and flows over the image data matrix (at least, I hope I understand how it's supposed to work), but I'm running into a problem with my code. Each image I filter with the following code shows as a flat colour, usually the first colour from the top-left corner. I'm sure the error is probably tiny, but I just can't seem to find it.
I'd be grateful if you guys could maybe take a look.
public void medianFilter(){
System.out.println("Initializing 2nd image.");
initializeAltImage(); // <-- Preps a blank image the same size as the original.
System.out.println("2nd image initialized.");
int kernelwidth = 3;
int kernelheight = 3;
int[] rMedian = new int [kernelwidth*kernelheight];
int[] gMedian = new int [kernelwidth*kernelheight];
int[] bMedian = new int [kernelwidth*kernelheight];
int kerneliter = 0;
// Walk the entire image but stop before you go out of bounds at the kernel boundraries.
for (int i = 0; i<this.x-kernelwidth; i++){
for (int j=0; j<this.y-kernelheight; j++){
// Walk the kernel itself.
for (int ki = i; ki<kernelwidth; ki++){
for(int kj = j; kj<kernelheight; kj++){
Color col = new Color(this.img.getRGB(ki, kj));
rMedian[kerneliter] = col.getRed();
gMedian[kerneliter] = col.getGreen();
bMedian[kerneliter] = col.getBlue();
kerneliter++;
}
}
kerneliter = 0;
Arrays.sort(rMedian);
Arrays.sort(gMedian);
Arrays.sort(bMedian);
Color colfinal = new Color(rMedian[4], gMedian[4], bMedian[4]);
this.altimg.setRGB(i+1, j+1, colfinal.getRGB());
}
}
}
Edit #1: Added full minimal compilable code for help's purposes.
Edit #2: Removed said code. The answer turned out to be simple silly loop limits.
When you get the value from the source image img.getRGB(ki, kj) you are actually taking a pixel at coordinates between 0 and kernelwidth/kernelheight.
For simplicity, assume that kernelwidth=kernelheight=3. When i,j >=3 the inner loop is not executed, and the median is not updated.
for (int i = 0; i<x-3; i++)
for (int j=0; j<y-3; j++)
for (int ki = i; ki<3; ki++)
for(int kj = j; kj<3; kj++)
//ki, kj at most between 0 and 2
img.getRGB(ki, kj)
Actually it should be:
for (int i=1; i<x-1; i++)
for (int j=1; j<y-1; j++) {
for (int ki = 0; ki<3; ki++)
for(int kj = 0; kj<3; kj++)
img.getRGB(i+ki-1, j+kj-1) {
...
Note that I'm not dealing with borders. For the borders you would probably use a reduced kernel, or took values for pixels outside the image as either constant (e.g. white/black) or with the same value as the border.
I am currently trying to make a function to prepopulate fields and determine if they have been set up or not. I need to add a total of 46 entries into my database so I figured a for loop would come in handy:
public void prepopulate() {
Locker setup = new Locker(this);
for (int x = 46; x < 47; x++) {
setup.open();
String lockerNumber = "Locker" + x;
setup.createLockerEntry(lockerNumber, 0);
setup.close();
}
I
Any ideas, maybe a for loop wont work?
if you want to add 46 entries the why are you using for (int x = 46; x < 47; x++). This will work only for one entry.
Just change you for loop like
for (int x = 0; x < 46; x++) //or any appropriate condition
Thanks in advance for your feedback. I can't make sense of an ArrayIndexOutofBoundsException error in the snippet of code below. Is it obvious to any of you?
public void run() {
int x = 10;
int y = 10;
double[][] Lx = new double[x][y];
double[][] Ly = new double[x][y];
for (int i=0; i<Lx.length; i++) {
for (int j=0; j<Lx[0].length; i++) {
Lx[j][i] = 2*j+i-1;
Ly[j][i] = Math.sqrt(3)*(i-1);
}
}
}
Your j for loop is incorrect in incrementing i, not j. Change
for (int j=0; j<Lx[0].length; i++) {
to
for (int j=0; j<Lx[0].length; j++) {
In addition, your array access indices are backwards. Change
Lx[j][i]
to
Lx[i][j]
and likewise with Ly. Otherwise you'll get a flipped 2D array.
With addition to #rgettman's answer
As in here your x and y is same so you will not have any issue with Lx[0].length. But it is good to use Lx[i].length.
Lx[0].length will always give the length or number of columns present in the 0th row. But Lx[i].length will give the length or number of columns present in the ith row.
So in case your x and y differs you will not again get ArrayIndexOutBoundException.
It should be j++ not i++ in the nested for-loop.
It's better not to use i and j for the index because they look too similar.
i and k are much more distinct than i and j
I'm working on this bacteria life game thing I have to make.
Basically I have a 2d string array let's say 20 by 20.
What would be the best way to check all 8 spots around a certain index. Each index is suppose to represent a bacteria. For each bacteria(index) I have to check to see if any of the 8 spots around this index has another bacteria in it, if the index has a bacteria in it, it's represented simply by a "*", asterik.
What would be the best way to go about checking all 8 spots around each index, because based on what is in the indices around a certain index I have to make certain changes etc.
The only idea I have come up with is having a bunch of if statements to check all 8 spots, I was wondering if there is a better way to do this
ex:
row 1 - www , row 2 = wOw , row 3 - www ,
if I am at the O index, what would be the best way to check all the index spots around it for a certain string.
Sorry, I am not very good at explaining my problems, bad english :o.
thanks for any of the help.
so you have something like this
char[][] table = new char[20][20]
for(int i = 0; i < 20; i++) {
for(int j = 0; j < 20; j++) {
int surroundingBacteria = 0;
for(int x = max(i-1,0); x < min(20,i+1); x++) {
for(int y = max(i-1,0); y < min(20,i+1); y++) {
if(table[x][y] == '*') surroundingBacteria++;
}
}
switch(surroundingBacteria) {
// put your case logic here
}
}
}
Here is how I've accomplished this in the past:
for(int x = -1; x<=1; x++){
if ( i+x < xLength && i+x >= 0){
for(int y = -1; y<=1; y++){
if(j+y < yLength && j+y >= 0){
//logic goes here
}
}
}
}