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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Removing fractions from Integers

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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 [][] 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;
                }
            }
        }


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

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

  3. The Following User Says Thank You to angstrem For This Useful Post:

    Blinktwink (May 26th, 2013)

  4. #3
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default 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?

  6. The Following User Says Thank You to angstrem For This Useful Post:

    Blinktwink (May 27th, 2013)

  7. #5
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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?

  9. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Blinktwink (May 27th, 2013)

  10. #7
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

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

  11. #8
    Junior Member
    Join Date
    May 2013
    Location
    Wellington, NZ.
    Posts
    11
    My Mood
    Cold
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Removing fractions from Integers

    Thanks.jpg
    Here's the finished product

Similar Threads

  1. Array, fractions, etc
    By Kristenw17 in forum Collections and Generics
    Replies: 1
    Last Post: April 17th, 2013, 08:29 PM
  2. Fractions
    By ps3lover3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 29th, 2013, 09:10 PM
  3. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  4. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  5. Fractions
    By debug in forum Java Theory & Questions
    Replies: 1
    Last Post: March 8th, 2010, 11:29 PM

Tags for this Thread