How to print characters entered in reverse order in Java? The answer is here! We probably look Java documentation to find a single statement function to do this, but we failed at most of the time. Following is the source code to reverse any character entered into Java. Java reverse these characters by splitting each character into an Character array. I have provided both pseudocode as well as source code. Instead of traditional System.in.read(); I have used Scanner class which was introduced to Java 1.5.
Logic of the Program (Pseudocode)
Use Variables:
status, chrArray[] OF TYPE Character,
userInput OF TYPE String
Begin
status = 'y'
Do
Print "Enter Number you want to reverse: "
Read userInput
for(count = 0 to usersValues.number_of_characters)
chrArray[count] = usersInput[character_at_count_index]
End
Print "Reversed Value "
for(count = chrArray.number_of_characters-1 to 0
Print chrArray[count]
End
Print "Press y to repeat program"
Read status
while(status = 'y')
End
Java Source Code: Reverse Characters/Integers or any (Name of the Java file is - Reverse.java)
import java.util.Scanner;
public class Reverse{
public static void main(String[] args) throws Exception{
/**status is used to repeat the program if user wanted to*/
char status;
do{
/**String variable is used to hold value entered by the user.
*We split this value into Character array 'chrUserInput'*/
String strUserInput;
char[] chrUserInput;
/**Scanner is used to read input from Keyboard. Program will print program title at the beginning.*/
Scanner scan = new Scanner(System.in);
System.out.println("\nWelcome to Reverse Program\n");
/**User input is assigned to strUserInput. String.toCharArray() will split
*String into a Character array*/
System.out.print("Inset Number to Reverse: ");
strUserInput = scan.next();
chrUserInput = strUserInput.toCharArray();
System.out.println("\nYour input is: " + strUserInput);
System.out.print("\nReversed Value: ");
/**This for loop reverse the values in array and print*/
for(int firstForLoop = chrUserInput.length-1; firstForLoop>-1; firstForLoop--){
System.out.print(chrUserInput[firstForLoop]);
}
/**Asking user whether he/she want to re-play the program. If user pressed y, program will repeat*/
System.out.print("\n \nDo you want to run again (y/n)? ");
status = Character.toLowerCase((char)System.in.read());
}while(status == 'y');
}//end of main
}//end of class
[Program Designed and Uploaded by H.W Thushara Indika, Jayamine, Dambathugoda, Kithal Ella Road, Heeloya, Bandarawela, Sri Lanka (28th September 2009). Copy and modify as you wish and its your own risk. ]
Above code has error tracking and other features. Following is one of the very simple method to implement this:
import java.util.Scanner;
public class Reverse{
public static void main(String args[]){
int intUserInput = 00000;
String tempString;
Scanner scan = new Scanner(System.in);
System.out.print("Enter 5 digits to Reverse: ");
tempString = String.valueOf(scan.next());
if(tempString.length() > 5)
{
System.out.println("Please enter 5 numbers only.");
}
else{
intUserInput = Integer.parseInt(tempString);
System.out.print("Reversed Value is: ");
for (int loop=4; loop >-1; loop--)
{
try{
System.out.print(String.valueOf(intUserInput).charAt(loop));
}
catch(StringIndexOutOfBoundsException e)
{
System.out.print("0");
}
}//end for
}//end if
}//end main method
}//end class
Please check that the exception occurs has been ignored and I have let java to corp them.
Program Designed and Uploaded by H.W Thushara Indika, Jayamine, Dambathugoda, Kithal Ella Road, Heeloya, Bandarawela, Sri Lanka (28th September 2009). Copy and modify as you wish and its your own risk.