Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Example: String reverse

The following program reverses a string in a very straightforward, but rather inefficient way. When you learn about StringBuilder (or the equivalent StringBuffer), you can do this more efficiently. But the purpose of this is to see how looping over a string works.

Nested loops. There are two nested loops in the is program, the outer while loop reads more input. The inner for loop gets every character from the input string starting at character 0.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
// File   : loops/reverse/Reverse.java
// Purpose: Reverse a string using a loop.
// Author : Fred Swartz
// Date   : Oct 23 2005
// Comments: Building a String one character at a time
//           is very inefficient because it requires
//           creating a new object for each concatenation.
//           StringBuilder is better, and it already
//           has a reverse() method!

import javax.swing.*;

public class Reverse {
    public static void main(String[] args) {

        String input;    // Used for the input string.
        String reversed; // Reversed form or the input string.

        while (true) {
            input = JOptionPane.showInputDialog(null, "Enter a string");
            if (input == null) break;

            reversed = "";
            for (int i=0; i<input.length(); i++) {
                reversed = input.substring(i, i+1) + reversed;
            }

            JOptionPane.showMessageDialog(null, "Reversed:\n" + reversed);
        }
    }
}

While loop vs For loop

Counting. A for loop is preferred to a while loop when counting through a series of numbers -- in this case all character positions in a string.

Equivalent. A for loop has the same condition as the equivalent while loop, but also incorporates an initialization, which would be before the while statement, and the increment, which would be at the end of the while body. You can write the loop either way, but putting the initialization, condition, and increment in one statement increases the readability.

For loopWhile loop
for (int i=0; i<input.length(); i++) {
    reversed = input.substring(i, i+1) + reversed;
}
int i = 0;
while (i<input.length()) {
    reversed = input.substring(i, i+1) + reversed;
    i++;
}

A single character - String or char?

This program uses substring(...) to get a single character. It would be more efficient to use charAt(...), which returns a single primitive char value.

for (int i=0; i<input.length(); i++) {
    reversed = input.charAt(i) + reversed;
}

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.