Removing fractions from Integers

Sorry for the vague title it's 3:30 in the morning and I really want to get this done.

My problem is that I am adding together two integers and dividing by two to find the average of them.

Because the numbers are between 0 and 255 I will sometimes end up with a '.5' on the end which of course my compiler doesn't like.

Is there a method for removing such occurrences? If not, how would I got about getting around this problem.

Quick note, no I can't just declare a double instead as I am passed an array of Int's to work with.

Here is my code for reference

Code Java:

public void mergeImage(){
int [][] other = this.loadImage(UIFileChooser.open());
int rows = Math.min(this.image.length, other.length);
int cols = Math.min(this.image[0].length, other[0].length);
for(int i = 0; i < rows-1; i++){
for (int ii = 0; ii < cols-1; ii++){
int a = this.image[rows][cols];
int b = other[rows][cols];
this.image[rows][cols] = (a+b)/2;
}
}
}

Re: Removing fractions from Integers

You'll never get doubles as a result of int/int division. So (a + b) / 2 will always be an integer.

Re: Removing fractions from Integers

Oh really? Thank you for that informative tidbit, must be another error in my code some where.

--- Update ---

Turns out I'm getting an "Array out of bounds exception" with this line.

Code Java:

int b = other[rows][cols];

Still got no clue why though.

Re: Removing fractions from Integers

Of course you get. Look at the meaning of rows and cols variables. Also, remind me, why might you need i and ii variables in the loops?

Re: Removing fractions from Integers

i stands for index. ii is simply my second index for my nested loop. It's the incrementation of these which controls the loop. I understand that there is a syntax 'for each' that I haven't quite mastered yet as I have only just started learning programming.

The rows variable is the minimum number of rows out of the two arrays and the same for cols. This is to ensure that I don't try to assign a number to the this.image array that may be out of bounds.

Re: Removing fractions from Integers

It's your program, so forgive me for saying that I and ii don't stand for "index": they stand for "row" and "column". Or, to use angstrem's word, they mean row and column. Which raises the question... Why, when you use for loops to set row and column to various values, don't you actually use those values inside the loops?

Re: Removing fractions from Integers

Oh wow I can't believe I didn't see what I was doing wrong. Feel like a right idiot now. It was so obvious yet somehow it eluded me, it always seems to be that way though. Thanks for the help this makes so much more sense now.

1 Attachment(s)

Re: Removing fractions from Integers

Attachment 2105

Here's the finished product :)