1

I have a problem with a task. I must transform the following doWhile loop into a While loop, so that the result of both loops is the same? As you can see the type Integer is just in the doWhile loop. But what's the difference between type int. How do I solve this task?

doWhile loop:

public Integer a_doWhile(int x){
    int i = 0;
    Integer result;
    do {
        result = ++i*i;
    } while (result < x);
    return result;
}

My solution:

public Integer a_while(int x){
    int i=0;
    int result;
    while(result < x){
        result = ++i*i;
    return result;
    }
}

But my solution is wrong. It has to do sth with this "Integer", Can someone help me please? Thanks :)

Ryan Walker
  • 2,928
  • 1
  • 20
  • 26
John Doe
  • 65
  • 8

1 Answers1

0

int and Integer are different representation of same thing. Int is primitive while Integer is object representation. This is discussed here What is the difference between an int and an Integer in Java and C#?

You can convert you dowhile to while like this

public Integer a_While(int x){
    int i = 0;
    Integer result = ++i*i;
    while (result < x) {
        result = ++i*i;
    }
    return result;
Community
  • 1
  • 1
Leadfoot
  • 610
  • 7
  • 21