Question:
How to make a java program that tells you if a number is a palindrom using an array?
Jay
2013-02-28 17:37:54 UTC
This is more of less what I have:


public static void main(String[] args) {
while (true) {
display(check(retrieveInput()));
}
}

public static String retrieveInput() {
Scanner scan = new Scanner(System.in);
return scan.next();
}

public static boolean check(String input) {
boolean check = false;
try {
Integer.parseInt(input);
if (input.charAt(0)==input.charAt(4) && input.charAt(1)==input.charAt(3))
check = true;

} catch(Exception e) {
check = false;
}

return check;
}

public static void display(boolean check) {
if(check) System.out.println("Is a five-digit palindrome.");
else System.out.println("Is not a five-digit palindrome.");

It works but I don't know how to integrate an array. Please help!
Three answers:
richarduie
2013-02-28 19:43:34 UTC
import java.util.Scanner;



public class PalindromeChecker

{

public static void main(String[] args) {

while (true) {

// capture String of number for later use in output

String num = retrieveInput( );

// check whether palindromic

boolean isPalindrome = check( num.toCharArray() );

// report on state

display( num, isPalindrome );

}

}



public static boolean check(char[] parray) {

// initialize for logical ANDing

boolean is = true;

// total number of characters

int len = parray.length;

// maximum number of tests required

int last = (int)Math.floor( len / 2);

// test inward towards middle from ends of array

for (int i = 0; i < last; i++) {

is = is && parray[ i ] == parray[ len - i - 1 ];

// quit for first failure

if (!is) break;

}

return is;

}



public static void display(String num, boolean isPalindrome) {

System.out.println(

num + " is " + ((isPalindrome) ? "":"NOT ") +

"a (" + num.length() + "-digit) palindrome."

);

}



public static String retrieveInput() {

Scanner scan = new Scanner(System.in);

return scan.next();

}

}
riddle
2016-12-17 11:36:36 UTC
ok, first think of of the (naive) set of rules to get a fibonacci variety: int fibonacci(int n) { if(n == 0) return 0; if(n == a million) return a million; return fibonacci(n-a million) + fibonacci(n-2); } This works, yet overall performance is sluggish. Why? this may be a quite recursive set of rules, as a fashion to locate the 10th merchandise of the sequence, you may calculate the ninth, and the 8th, each and every of those calculates the seventh, and the 6th, and onwards until the backside case(s) are reached. a greater effectual set of rules(no longer the terrific IMO) is to apply an array to cache intermediate values. the problem with the above set of rules, is which you re-calculate the values in the sequence. that's pseudo-code(that's quite often java splendid) for a fashion that provide you an array of ints for the variety you will provide it. public static int[] fib(int n) { if(n == 0) return 0; int[] numbers = new int[n]; // create the array numbers[0] = 0; numbers[a million] = a million; for(int i=2; i <= n; i++) { numbers[i] = numbers[i-a million] + numbers[i-2]; } return numbers; } Use it by making use of calling fib(20), storing the consequence as an integer array, then print out each and every merchandise in the array as required. I unquestionably have not compiled or run it yet you may get the assumption.
2013-02-28 19:33:09 UTC
'string' in http://docs.oracle.com/javase/7/docs/api/allclasses-frame.html

has method 'toCharArray()'

char a[] = input.toCharArray ();

int i=0;

while ( i < a.length )

{

a [ i ] ....

i++;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...