-
how to reverse the digits..
Here's the program I'm working on that includes a method that takes an integer value and returns the number with its digits reversed. For example, after the user enters the number 3456, the method should return 6543.
----------------------------------------
import java.awt.*; // Container, FlowLayout
import java.awt.event.*; // ActionEvent, ActionListener
import java.swing.*; //JApplet, JTextField
public class reverseDigits extends JApplet implements ActionListener {
JLabel, promptLabel;
JTextField inputField;
public void init() {
Container container = getContentPane();
container.setLayout( new FlowLayout() );
promptLabel = new JLabel( "Enter an integer and press Enter: " );
inputField = new JTextField( 8 );
inputField.addActionListener( this );
container.add( promptLabel );
container.add( inputField );
} // end method init
public void actionPerformed( ActionEvent actionEvent ) {
int integerValue;
int number = Integer.ParseInt( actionEvent.getActionCommand() );
integerValue = number.reverse();
showStatus( "The reversed integer is: " + integerValue );
} // end method actionPerformed
} // end class reverseDigits
------------------------------------
This program has a number of errors in there I'm trying to fix..
-
I'm still unsure about the number.reverse(); as reverse() only seem to work with stringbuffer.. and it reverses the string only. I probably could use it right before Integer.parseInt(...) which converts the string to integer.
There's other way, maybe with for or while loop.. with -
string s=" ";
s += number%10; // it gets the last digit
number = number/10;
I'm not sure which is the best way to reverse the digits.. any of you guys have better idea? Your input will be appreciated.
-
here's a suggestion, though it's a bit lame and lazy....
since you just want the user to enter a value and return it reversed, why don't you just reverse it first in it's string form before you convert it to int. you just use the StringBuffer class's reverse method like you said. or you don't really have to make it an int, right? you just want to reverse it.
but then again i don't really know what's your purpose for making a reverse method go through what you just have done. a loop will do just fine.
-
using for loop to count numdigits...
Yeah, its lazy way to use stringbuffer thats why I dont want to do it. Here's the change I made...
public void actionPerformed( ActionEvent actionEvent ) {
int integerValue;
String s = "";
s = inputField.getText();
for( int numdigits=input.length; numdigits>=0; numdigits-- ) {
s += numdigits%10; // get last digit
int number = numdigits/10;
} // end for loop
integerValue = Integer.parseInt( s );
showStatus( "The reversed integer is: " + integerValue );
} // end method actionPerformed
-----------------------
The problem is.. how can I equate s and number for the integerValue to show result. Anyone can help me??
-
2nd change to cut down errors..
public void actionPerformed( ActionEvent actionEvent ) {
int integerValue;
String input;
String s = "";
input = inputField.getText();
for( int numdigits=input.length; numdigits>=0; numdigits-- ) {
s += numdigits%10;
int number = numdigits/10;
} // end for loop
integerValue = Integer.parseInt( s );
showStatus( "The reversed integer is: " + integerValue );
-----------------------------
C:\JAVAPR~1>javac problem627.java
problem627.java:34: cannot resolve symbol
symbol : variable length
location: class java.lang.String
for( int numdigits=input.length; numdigits>=0; numdigits-- ) { ^
1 error
------------------------------
Couldnt figure out what's the problem with input.length yet..
-
length isn't a field of String, it's a method. It should therefore be called:
myString.length()
not
myString.length
(BTW, you're making so much work for yourself reversing the number this way and it's quite computationally expensive)
Hope this helps,
ArchAngel.
ArchAngel.
O:-)
-
I am not sure why being "lazy" in using StringBuffer is a problem, but an alternative method would be using a char array.
StringBuffer s = new StringBuffer();
char[] digits = input.toCharArray();
for (int numDigits = digits.length-1; numDigits >= 0; numDigits--) {
s.append(digits[i]);
}
integerValue = Integer.parseInt(s.toString());
...
Laziness is a virtue.
-
Guys, forget stringbuffer... I'm content with this:
public int reverseDigits( int input ) {
String inputString;
int i=0;
output = "";
inputString = "" +input;
for( int numdigits=1; numdigits<=inputString.length(); numdigits++) {
digit = input/(int)Math.pow( 10, numdigits-1 );
digit = digit%10; // get last digit
output += digit;
i++;
} // end for loop
input = Integer.parseInt(output);
return input;
} // end method reverseDigits
} // end class problem627
-
As for the actionPerformed method, look at the error checking...
Try this program in your computer.. you'll enjoy it.
-------------------------------------
public void actionPerformed( ActionEvent actionEvent ) {
int result;
int input;
int isNumber = 1;
temp = inputField.getText();
int i = 0;
while( i < temp.length() && isNumber != 0 )
{
// checks if the current character is a number
if( !Character.isDigit( temp.charAt(i) ) )
isNumber = 0; // if current character is not a number, set isNumber to 0
i++;
}
if( isNumber==1 )
{
input = Integer.parseInt(temp);
if(input >= 1 && input <= 99999)
{
result = reverseDigits( input );
showStatus( "Reversed integer is " +result );
}
else
{
showStatus( "Error: Pls enter an integer >=1 and <=99999" );
}
}
else
{
showStatus( "Error: Invalid input" );
}
} // end method actionPerformed
-
hey there's no problem with using methods that are already in java. that's the purpose of reusability. but then these methods are already tried and tested. you wouldn't want to reinvent the wheel, right? but i know it feels good when you've done it your way. it also helps you get around with java better. in other words, practice makes perfect....
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