patternjavaMinor
Palindrome program with reverse method
Viewed 0 times
reversemethodwithprogrampalindrome
Problem
I wrote a program for a college assignment, and I'd like to receive some pointers on making my code a little more efficient.
The program should ask the end user to enter an integer. Use the
the integer is a palindrome. Any one-digit integer or negative
integer should be rejected with the specific error message (negative
or one digit), and then the program should ask the user to re-enter
the integer. Hint: To reverse the digits of a number, try this
routine:
My solution:
```
package lab07;
import java.util.Scanner;
public class Lab07 {
public static int reverse(int number){
int result = 0;
while (number !=0){
int remainder = number % 10;
result = result * 10 + remainder;
number = number / 10;
}
return result;
}
public static boolean isPalindrome(int input){
int Palindrome = reverse(input);
if (Palindrome == input){
return true;
}
else
return false;
}
public static void main(String[] args) {
int integer = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive, multi-digit integer: ");
integer = input.nextInt();
while (integer 0)
{
System.out.println(integer + " is a single digit. Please re-enter another integer: ");
integer = input.nextInt();
The program should ask the end user to enter an integer. Use the
isPalidrome method to invoke the reverse method, and report whetherthe integer is a palindrome. Any one-digit integer or negative
integer should be rejected with the specific error message (negative
or one digit), and then the program should ask the user to re-enter
the integer. Hint: To reverse the digits of a number, try this
routine:
int result = 0;
while (number != 0) { // e.g. number = 123
// Iteration 1 Iteration 2 Iteration 3
int remainder = number % 10; // remainder = 3 remainder = 2 remainder = 1
result = result * 10 + remainder; // result = 3 result = 32 result = 321
number = number / 10; // number = 12 number = 1 number = 0
} // result contains the reverse of numberMy solution:
```
package lab07;
import java.util.Scanner;
public class Lab07 {
public static int reverse(int number){
int result = 0;
while (number !=0){
int remainder = number % 10;
result = result * 10 + remainder;
number = number / 10;
}
return result;
}
public static boolean isPalindrome(int input){
int Palindrome = reverse(input);
if (Palindrome == input){
return true;
}
else
return false;
}
public static void main(String[] args) {
int integer = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive, multi-digit integer: ");
integer = input.nextInt();
while (integer 0)
{
System.out.println(integer + " is a single digit. Please re-enter another integer: ");
integer = input.nextInt();
Solution
Apart from the other comments about how to calculate the input, I would also suggest that your main method is buggy, and not very 'pretty'. One the the tricks in programming that Java programmers seem to forget, is the do-while loop (note, C programmers use this all the time, and Java programmers do all sorts of crazy things to avoid it ...! ).
Consider the an altered main method
I should add that having 'return' in a main method is unconventional.... not 'wrong' but it probably means you are doing something weird...
Consider the an altered main method
- renamed Scanner to be
scanner
- renamed integer to be
input.
- use do-while for input validation
- use 'simpler' printf for output
public static void main(String[] args) {
int input = 0;
Scanner scanner = new Scanner(System.in);
boolean ok = false;
do {
System.out.print("Enter a positive, multi-digit integer: ");
input = scanner.nextInt();
if (input < 0) {
System.out.println(input + " is negative. Please re-enter another integer: ");
} else if (input <= 9) {
System.out.println(input + " is a single digit. Please re-enter another integer: ");
} else {
ok = true;
}
} while (!ok);
System.out.printf("%d %s a palindrome\n", input, isPalindrome(input) ? "is" : "is not");
scanner.close();
}I should add that having 'return' in a main method is unconventional.... not 'wrong' but it probably means you are doing something weird...
Code Snippets
public static void main(String[] args) {
int input = 0;
Scanner scanner = new Scanner(System.in);
boolean ok = false;
do {
System.out.print("Enter a positive, multi-digit integer: ");
input = scanner.nextInt();
if (input < 0) {
System.out.println(input + " is negative. Please re-enter another integer: ");
} else if (input <= 9) {
System.out.println(input + " is a single digit. Please re-enter another integer: ");
} else {
ok = true;
}
} while (!ok);
System.out.printf("%d %s a palindrome\n", input, isPalindrome(input) ? "is" : "is not");
scanner.close();
}Context
StackExchange Code Review Q#33822, answer score: 5
Revisions (0)
No revisions yet.