Skip to content

Instantly share code, notes, and snippets.

@MrBean83
Created May 12, 2015 16:07
Show Gist options
  • Save MrBean83/eed68427ba38ae7dc9c2 to your computer and use it in GitHub Desktop.
Save MrBean83/eed68427ba38ae7dc9c2 to your computer and use it in GitHub Desktop.
Prints out string if string length is even; prints out reversed copy of string if string length is odd.
import javax.swing.JOptionPane;
public class StringOddEven {
public static void main(String args[]) {
String input = JOptionPane.showInputDialog("Please enter a string.");
int length = input.length();
if (length % 2 == 0) {
System.out.println(input);
} else {
int i = length - 1;
// Using a DO-WHILE loop for this program.
do {
System.out.print(input.charAt(i));
i--;
} while (i >= 0); // end do-while
} // end if
} // end main
} // end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment