-
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.
// as global variables
int digit;
String output="";
String input;
public void actionPerformed( ActionEvent actionEvent ) {
input = inputField.getText();
displayDigits( input );
} // end method actionPerformed
public int displayDigits( input ) {
for( int numdigits=input.length; numdigits>=0; numdigits--) {
digit = Integer.parseInt( input )/math.pow( 10, numdigits-1 );
String digit = toString( digit );
digit = Integer.parseInt( input )%math.pow( 10, numdigits-1);
output += ""+digit+"";
} // end for loop
outputField.setText( output ); // writes output string to textbox
} // end method displayDigits
} // end class
-------------------------------------
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.
-
erm...that's a *much* easier way to do this (BTW, your displayDigits() method says it returns an int). Here's the simplier code:
Code:
public class NumbersTest {
private int digit;
private String output="";
public static void main(String[] args) {
String input = args[0];
displayDigits(input);
}
public static void displayDigits(String input) {
// Calculate the capacity of the new string so that
// StringBuffer never has to grow
int stringLength = input.length();
int newStringLength = (stringLength * 3) - 2;
StringBuffer sb = new StringBuffer(newStringLength);
for(int index = 0; index < stringLength; index++) {
sb.append(input.charAt(index));
if(index < stringLength - 1) {
sb.append(" ");
}
}
String result = sb.toString();
System.out.println("'" + result + "'");
}
}
If you call this program java NumbersTest 1234, it will output '1 2 3 4'.
Hope this helps,
ArchAngel.
ArchAngel.
O:-)
-
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.
Laziness is a virtue.
-
From a quick glance at the code I saw some of your errors.
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.
Two ways around this..
The lazy way is to code
Code:
String sDigit = digit + "";
or a not so lazy way of
Code:
String sDigit = Integer.toString(digit);
Hope this helps.
Laziness is a virtue.
-
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...
public void displayDigits( int input ) {
String inputString;
int i=0;
output = "";
inputString = "" +input;
for( int numdigits=inputString.length(); numdigits>0; numdigits--) {
digit = input/(int)Math.pow( 10, numdigits-1 );
digit = digit%10; // get last digit
output += digit +" ";
i++;
} // end for loop
outputField.setText( output ); // writes output string to textbox
showStatus( "Numbers are separated using displayDigits method." );
} // end method displayDigits
} // end class prob622
-
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..
You guys have any ideas or reference.. I would be appreciated.
-
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);
}
Laziness is a virtue.
-
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.
Laziness is a virtue.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks