Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 623997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:09:11+00:00 2026-05-13T19:09:11+00:00

I’m working on some beginning Java logic, and I’m not sure why this is

  • 0

I’m working on some beginning Java logic, and I’m not sure why this is not working. Here is a method I made:

private void printSubclassBoxes(){
        int  coordinateX = ((getWidth() - BOX_WIDTH) /4);

        for ( int i = 0; i < 3; i++){

            double coordinateY = (getHeight() / 2);     
            GRect classBox = new GRect (coordinateX, coordinateY, BOX_WIDTH, BOX_HEIGHT);

            GLabel classLabel = new GLabel ("Program");
            double labelCoordinateX = (coordinateX + ((classBox.getWidth() / 2) - (classLabel.getWidth() / 2)));
            double labelCoordinateY = (coordinateY + ((classBox.getHeight() / 2) + (classLabel.getAscent() / 2)));
            add(classBox);
            add(classLabel, labelCoordinateX, labelCoordinateY);
            if (i == 1){
            coordinateX = (((getWidth() - BOX_WIDTH) /4) * 2);
        } 
        if (i == 2){
            coordinateX = (((getWidth() - BOX_WIDTH) /4) * 3);
        } 

        }   

    }

Now I’m sure there are probably better ways to do this, but please-I’m not interested in that right now (I’m trying to learn without being spoonfed the answers). All I am wanting to know is why the ending two if statements are not working like I think they should.

For simplicity sake, let’s say

100 = ((getWidth() - BOX_WIDTH)

int coordinateX = 25;

My understanding is that int i gets to that first if statement and adds 25 + 25 so then coordinateX = 50.

then next time in the loop, i = 2 so coordinateX would = 75.

This is what I’m expecting to happen, but it’s not. I seem to be printing the first two boxes directly on top of each other, and then the third is moving 25.

Thanks for your help guys. Now that I got that loop figured out, I went ahead and solved it a different way. I ended up assigning coordinateX to another variable and using that to add to the end:

int  coordinateX = ((getWidth() - BOX_WIDTH) /4);
        int otherCoordinateX = coordinateX;

        for ( int i = 0; i < 3; i++){

            double coordinateY = (getHeight() / 2);     
            GRect classBox = new GRect (coordinateX, coordinateY, BOX_WIDTH, BOX_HEIGHT);

            GLabel classLabel = new GLabel ("Program");
            double labelCoordinateX = (coordinateX + ((classBox.getWidth() / 2) - (classLabel.getWidth() / 2)));
            double labelCoordinateY = (coordinateY + ((classBox.getHeight() / 2) + (classLabel.getAscent() / 2)));
            add(classBox);
            add(classLabel, labelCoordinateX, labelCoordinateY);
            coordinateX = otherCoordinateX + coordinateX;

        }   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:09:11+00:00Added an answer on May 13, 2026 at 7:09 pm

    at the first iteration of the loop:

    • i = 0, and
    • coordinateX = 25, as expected.

    At the end of the iteration coordinateX is not updated (since i == 0).

    Then, at the second iteration:

    • i = 1,
    • but still coordinateX = 25 since it wasn’t updated.

    At the end of the second iteration coordinateX is updated because of the if i == 1) test.

    In the third iteration

    • i = 2
    • the new value of coordinateX is used, which was set at the end of the second iteration.

    Solution

    To not change your code in any essential way, simply replace if (i == 1) by if (i == 0), and if (i == 2) by if (i == 1).

    EDIT: Lesson

    In the for(...) { } declaration, the i < 3 test is checked at the beginning of each iteration, but the i++ part is called at the end of each iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.