0

So here's the entirity of the code that matters

       int dom = Integer.parseInt(Domin.getText());
       double fraction = Integer.parseInt(Numer.getText())/Integer.parseInt(Domin.getText());
       String currentlow = "";
       System.out.println("TEST");


       for (int i = 0; i >= dom;i++){ //ok the problem wasn't that it was > dom instead of >= dom
           System.out.println("dummy"); //this doesn't print and it would print every time if it was running the for loop.
           if((num % i == 0)&&(dom % i == 0)){ //this just = checks to see that there's no remainder (like 5/5)
               System.out.println("true"); //this for some reason never triggers even though i'm testing with 5/25
               if ((num/i)/(dom/i) == fraction){ //this is a dummy check to make sure it doesn't round improperly
                   currentlow = String.valueOf(num/i) + "/" + String.valueOf(i); //this sets the value but isn't the problem since the console never says "true"
                    System.out.println(currentlow); //nother dummy check
               }
           }
       }

edit out the comments if you want, but basically the for-loop is supposed to cause it to divde by all the numbers less than the dominator, but it never even opens up the for loop (it doesn't print "dummy" or "true" ever, when it should print them 24 times in my testing) can't figure out why

6
  • 2
    You are checking if 0 >= dom. Is dom a negative number?
    – darkhouse
    Nov 5, 2019 at 18:45
  • My guess is the dom/i call, since it is integer division.
    – zero298
    Nov 5, 2019 at 18:46
  • i >= dom — what is the text of Domin.getText() and therefore the value of dom? You are starting at i = 0 and checking if i is GREATER THAN OR EQUAL to dom so, unless dom is a negative number this will never loop.
    – Stephen P
    Nov 5, 2019 at 18:46
  • It isn't going to work as expected, either i is never >= dom or it always is because you increment i while checking greater than
    – Tyler
    Nov 5, 2019 at 18:46
  • Additionally, checking exact equality between floating point values is not quite a good idea.
    – zero298
    Nov 5, 2019 at 18:48

2 Answers 2

1

Your for loop conditions look wrong. Your starting condition is i = 0, but your ending condition is i >= dom. If dom is any integer greater than 0, then the for loop will never execute because i can't be both 0 and greater than or equal to a number that's greater than 0. I think you mean to say i <= dom?

1
  • yeah, sorry for wasting your guys' time with a typo Nov 6, 2019 at 15:19
0

You have a few defects here. First, as others have indicated, this is wrong:

for (int i = 0; i >= dom;i++)

The only case where this would actually work is if dom is 0 (in which case it'll run exactly once). If dom is greater than 0, then i will never be greater than or equal to it. On the other hand, if dom is negative, i will always be greater than it because you're doing i++ on each iteration.

Secondly, you're doing division incorrectly. The result of the following:

double x = Integer.parseInt("3") / Integer.parseInt("4");
System.out.print(x); // Prints 0.0

is actually 0.0 because it's doing integer division. However, the result of this:

double x = (double)Integer.parseInt("3") / Integer.parseInt("4");
System.out.print(x); // Prints 0.75

is the expected 0.75.

You do this in several places in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.