Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. 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

public void mergeImage(){
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;
}
}
}

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.

4. 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.
int b = other[rows][cols];
Still got no clue why though.

5. 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?

7. 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.

8. 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?