If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

using the displayDigits() method to return integer value with spaces

The program takes the user's input which is an integer (no chars) that is > 1 and <= 99999 and calls the displayDigits() method to tear apart using division and modulus in for loop. For example, after the user enters an integer: 3456, the method should return the input value with two spaces between each digit.

There are 4 errors related to the displayDigits() method including the parameter. I'm not sure whats wrong... The brackets are fine. Maybe it has to do with how this method ends.. Can anyone help me out here.. I would be appreciated.

If you post the compile errors you get with this, it may be easier to see where it is going wrong. I am assuming you are required to use modulus and division in solving this problem, else using ArchAngel's method is much less of a headstrain.

Firstly, the parameter of the displayDigits() method needs to have its type declared.

I.e.

Code:

public int displayDigits(String input) {

rather than

Code:

public int displayDigits(input) {

Second, in your for() loop declaration you have stated

Code:

int numdigits = input.length

Input is a String object and the length is accessed by calling the length() method. Only arrays have an accessible variable of length.

Code:

int numdigits = input.length()

The third and fouth errors are in the line:

Code:

String digit = toString( digit);

You cannot make a new variable called digit here since you have the public int digit. You can rename this to be
String sDigit. The second problem is that you are attempting to call a method toString on no object.

displayDigits method - revised (final)

Guys, your inputs are helpful. Archangel's method is good one, its simpler but the questions asks for the use of modulus and division in this program. Here's the changes I made in displayDigits method...

self-review exercises - JAVA HTP 5/e

The programs I've been working on comes from the questions in self-review exercises from the book - JAVA How To Program (Deitel&Deitel). I couldnt find solutions and answers in the book in case I need to look at that would save me alot of time and sweats..

Okay, no worries. Since you have to use division and modulo try the code below .. it simplifies the algorithm a bit. I have documented to explain what and why it is doing what is doing, but please ask if you dont understand why it works.

Hope it is what you need.

Code:

public void displayDigits( int input) {
String result = "";
//We keep doing this until we have a single digit left
while (input >= 10) {
// By using modulo 10 we can find the number in the right most digit
int remainder = input % 10;
// we add the remainder unto the end of the result string
result = result + remainder;
// Thanks to integer division, by dividing by 10, we basically drop off the last
// digit, since integers can only hold whole numbers
input = input / 10;
}
//Now we remember to add the final single digit on to the end of the result;
result = result + input;
outputField.setText(result);
}

Sorry, it seems I was thinking you were trying to reverse the numbers rather than place spaces in between them. the only lines that are differemt...

Code:

result = result + remainder;

becomes

Code:

result = remainder + " " + result

and

Code:

result = result + input;

becomes

Code:

result = input + " " + result;

Note that you are always putting the remainder at the beginning of the line, not the end.

Also you could continue with your method of putting one space before it and one after it, or you can add two spaces between the result and the new digit. All depends on if you want to have 2 blanks at the end of the string, or one before and one after the final string.