-3

I am having trouble with the following question...

"write a method displayDigits that receives an integer between 1 and 99999 and displays it as a sequence of digits, separating each pair of digits by two spaces. For example, the integer 4562 should appear as 4 5 6 2"

This is what I have currently, I know it displays the numbers backwards, but won't compile with a "error: missing return statement" error.

public static int displayDigits(int num1)
{
    for (int i=0; num1>0; i++)
    {
        return(num1 % 10)
    }
}

The book noted that we only use % and / to solve the problem. Help please!! I've been working on this for hours...

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Ann Nguyen
  • 23
  • 1
  • 5
  • 3
    You need a return statement after the loop. Because if num1 is null the loop never will be executed – Jens Mar 04 '16 at 18:05
  • 3
    do you understand what `int` before `displayDigits` mean? do you understand how for loop works? what `return` does? – Iłya Bursov Mar 04 '16 at 18:06
  • Additionally your method doesn't try to display again despite being called "displayDigits". Moreover, if you want to generate something with spaces in that function, you'll need to return a string. – Matti Virkkunen Mar 04 '16 at 18:06
  • 1
    The method is supposed to *display* something (i.e. call `System.out.print()`), not return something, so change return type to `void`, and remove the `return` statement. – Andreas Mar 04 '16 at 18:14
  • You're on the right track, but you'll need a String variable to store the digits as you go. That way you can tack on to the front of the string to reverse the order of digits, since as you stated the method gets the number backwards. – Calvin P. Mar 04 '16 at 18:19

3 Answers3

-1
public static void main(String... args)
{
    int i=4562;
    String str ="";
    int rem;
    int q;
    while(true)
    {
        q = i/10;
        rem = i%10;
        if(rem==0)
            break;
        str +=" "+rem;
        i=q;

    }


    StringBuilder builder = new StringBuilder(str);
    System.out.println(builder.reverse());


}
-1
public static void displayDigits(int num1) {
    String result = "";
    for (int i=0; num1>0; i++) {
        if(i>0) result = " " + result;
        result = num1%10 + result;
        num1 /= 10;
    }
    System.out.println(result);
}
Calvin P.
  • 1,116
  • 2
  • 8
  • 13
-3

Do following:

String resultNumber = "";
for(int i = 0; i < num1.toString().length(); i++)
    resultNumber += num1.toString().substring(i, i+1) + " ";
resultNumber = resultNumber.substring(0, resultNumber.length() - 2);

When num1 is 4564, then resultNumber is "4 5 6 4" and that works for any number (between min and max value of integer)

xdevs23
  • 3,824
  • 3
  • 20
  • 33
  • 1
    OP indicated that the assignment required them to solve the problem with `%` and `/` arithmetic. – PM 77-1 Mar 04 '16 at 18:11
  • While this does work, please do bother to read the entire question. OP stated that the goal is to do this using only `%` and `/` – Calvin P. Mar 04 '16 at 18:13