Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2006

Tip: Looking for answers? Try searching our database.

Reverse Numbers

Thread view: 
IanH - 28 Feb 2006 09:19 GMT
Hi
In the following program, what ever digits are typed into the inputbox
I want them displayed in reverse order in the status bar. At the moment
only the first digit appears in the status bar.  I can't see where i'm
going wrong.  Maybe someone can take a look.
Cheers Ian

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Reverse extends JApplet implements ActionListener {
  JTextField inputField;
  JLabel prompt;

  // set up GUI components
  public void init()
  {
     prompt = new JLabel( "Enter Digits:" );
     inputField = new JTextField( 4 );
     inputField.addActionListener( this );

     Container container = getContentPane();
     container.setLayout( new FlowLayout() );
     container.add( prompt );
     container.add( inputField );

  }  // end method init

  // call method qualityPoints if user input is within range
  public void actionPerformed( ActionEvent actionEvent )
  {
     int inputNumber = Integer.parseInt( inputField.getText() );

    if ( inputNumber != 0 ){
        showStatus( "Reversed is: " + qualityPoints( inputNumber ) );}
     else
        showStatus( "Invalid input." );

  }  // end method actionPerformed

  // return single digit value of grade
  public int qualityPoints( int num)
  {
      int rightDigit =0;
      int newnum = 0;

      rightDigit = num % 10;
      newnum = newnum * 10 + rightDigit;
           num = num / 10;

           return num;
   
  }  // end method qualityPoints

}  // end class Reverse
Bart Cremers - 28 Feb 2006 09:31 GMT
   public int qualityPoints(int num) {
       int res = 0;

       while (num > 10) {
           res = (res * 10) + num % 10;
           num /= 10;
       }
       res = (res * 10) + num;

       return res;
   }

If you only need to revers a two digit number:
       return (num % 10) * 10 + num / 10;

Regards,

Bart
IanH - 28 Feb 2006 09:52 GMT
Cheers Bart, that was a great help.
Roedy Green - 28 Feb 2006 17:37 GMT
>In the following program, what ever digits are typed into the inputbox
>I want them displayed in reverse order in the status bar.

see String.reverse
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 28 Feb 2006 18:25 GMT
>> In the following program, what ever digits are typed into the inputbox
>> I want them displayed in reverse order in the status bar.
>
> see String.reverse

Doesn't exist...

    new StringBuffer(str).reverse().toString()

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.