-1

Okay, so I'm almost done with this program, but I need it to display all the palindromes from 100 to "user input" AND all prime palindromes from that list. So far I can only get it to display all the prime palindromes.

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
System.out.print("Please enter a number greater than 100: ");
int userInput = input.nextInt();

    if(userInput <= 100)
    {
        System.out.print("That number is not big enough, please enter a "
                + "number greater than 100");
    }
    else
    {
        System.out.println("The Prime Palindrome Integer between 101"
                + " and " + userInput + " are: ");
    }

for (int i = 101; i <= userInput; i++)
    {
        Integer userInt = new Integer(i);
        String userString = userInt.toString();
        char[] userChar = userString.toCharArray();

        if (isPrime(i) && arraysAreEqual(userChar, reverseArray(userChar)))
        {
            printArray(userChar);
            System.out.print(", ");
        }
    }
}

public static char[] reverseArray(char[] list)
{
    String reverse = new StringBuilder(new String(list)).reverse().toString();

char[] revArray = reverse.toCharArray();
    return revArray;
}

public static boolean arraysAreEqual(char[] list1, char[] list2)
{
    if (Arrays.equals(list1, list2) )
    return true;
    else return false;
}

public static boolean isPrime(int intPrime)
{
for (int i = 2; i < Math.sqrt(intPrime); i++)
    {
        if (intPrime % i == 0)        
        return false;
    }
return true;
}

public static void printArray(char[] list)
{
for (int i = 0; i < list.length ; i++)
    {
        System.out.print(list[i]);
}
}

}

campbellj21
  • 9
  • 1
  • 3
  • 1
    The prime palindromes are just a subset of all the palindromes. Do you mean that you have to print two lists? – Tavo May 01 '15 at 12:20
  • Yes, sorry about that. I need to print all of the palindromes and then print all of the prime palindromes. – campbellj21 May 01 '15 at 12:26
  • possible duplicate of [Find all substrings that are palindromes](http://stackoverflow.com/questions/19801081/find-all-substrings-that-are-palindromes) – JFPicard May 01 '15 at 12:36
  • @JFPicard I beg to differ. The other post might be complementary, but I don't believe it's a duplicate. – Tavo May 01 '15 at 12:39
  • I have corrected my answer below kindly check it out. I think that this will work fine. – Blip May 01 '15 at 15:05

2 Answers2

0

Ok, so here are a couple of changes to your code:

First, you need to change a bit your loop, so the elements are added to two lists, one with the primes and another one without the primes.

    List<Integer> primes = new ArrayList<>(), nonPrimes = new ArrayList<>();

    for (int i = 101; i <= userInput; i++)
    {
        Integer userInt = new Integer(i);
        String userString = userInt.toString();
        char[] userChar = userString.toCharArray();

        if (arraysAreEqual(userChar, reverseArray(userChar)))
        {
            nonPrimes.add(i);
            if(isPrime(i))
            {
                primes.add(i);
            }
        }
    }
    printArray(nonPrimes);
    System.out.println(); //Added to include a new line
    printArray(primes);

And then, a quick change to your printArray() method:

public static void printArray(List<Integer> list)
{
    for(Integer i: list) {
        System.out.print(i +", ");
    }
}

And that's it.

Tavo
  • 3,087
  • 5
  • 29
  • 44
0

From you question it is evident that you require to store the palindromes and the prime palindromes seperately. So my suggestion you could declare 2 ArrayList variables say palindrome and primePalindrome Like this:

  ArrayList<String> palindrome = new ArrayList<>();
  ArrayList<String> primePalindrome = new ArrayList<>();

then replace this if block

    if (isPrime(i) && arraysAreEqual(userChar, reverseArray(userChar)))
    {
        printArray(userChar);
        System.out.print(", ");
    }

with:

    if (arraysAreEqual(userChar, reverseArray(userChar))){
       if(isPrime(i)){
          primePalindrome.add(userString);
       }else{
          palindraome.add(userString);
       }
    }

And finally remove the function call printArray() you can write this code to print the output after the for loop.

System.out.println("The Non prime palindromes are :");
for(String str:palindrome){
   System.out.println(str);
}


System.out.println("The prime palindromes are :");
for(String str:primePalindrome){
   System.out.println(str);
}

Complete runnable code I am posting this complete runnable code just because you are saying that you are unable to understand what I am saying. I have implemented all that has been stated above. Run it and you would get your result.

import java.util.*;
import java.io.*;

public class test1{
    static ArrayList<String> palindrome = new ArrayList<>();
    static ArrayList<String> primePalindrome = new ArrayList<>();
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a number greater than 100: ");
        int userInput = input.nextInt();

        if(userInput <= 100){
            System.out.print("That number is not big enough, please enter a "
                    + "number greater than 100");
        }else{
            System.out.println("The Prime Palindrome Integer between 101"
                    + " and " + userInput + " are: ");
        }

        for (int i = 101; i <= userInput; i++){
            Integer userInt = new Integer(i);
            String userString = userInt.toString();
            char[] userChar = userString.toCharArray();
            if (arraysAreEqual(userChar, reverseArray(userChar))){
                if(isPrime(i)){
                    primePalindrome.add(userString);
                }else{
                    palindrome.add(userString);
                }
            }
        }

        System.out.println("The Non prime palindromes are :");
        for(String str : palindrome){
            System.out.println(str);
        }


        System.out.println("The prime palindromes are :");
        for(String str : primePalindrome){
            System.out.println(str);
        }
    }

    public static char[] reverseArray(char[] list){
        String reverse = new StringBuilder(new String(list)).reverse().toString();

        char[] revArray = reverse.toCharArray();
        return revArray;
    }

    public static boolean arraysAreEqual(char[] list1, char[] list2){
        if (Arrays.equals(list1, list2) )
            return true;
        else return false;
    }

    public static boolean isPrime(int intPrime){
        for (int i = 2; i < Math.sqrt(intPrime); i++){
            if (intPrime % i == 0)        
                return false;
        }
        return true;
    }

}
Blip
  • 3,061
  • 5
  • 22
  • 50
  • The output from that was "The Non prime palindromes are: " followed by "The prime palindromes are: " over and over again with the numbers at the end of each repetition. Instead of "The Non prime palindromes are: " followed by all of the numbers, and "The prime palindromes are: " followed by all of the numbers. – campbellj21 May 01 '15 at 14:46
  • @campbellj21 I am sorry this print function should be put after the `for` loop I have corrected the same in my answer. – Blip May 01 '15 at 14:56
  • now it gets stuck in an infinite loop for some reason. – campbellj21 May 01 '15 at 15:21
  • How can it get into the infinite loop? I am posting the code to change – Blip May 01 '15 at 15:29
  • Maybe I'm using the wrong term, sorry, I am quite the noob. When I go to run the program in NetBeans, it just keeps trying to run with no output. – campbellj21 May 01 '15 at 15:31
  • @campbellj21 I have posted the complete code that is directly runnable as you are saying you are unable to understand my terms. – Blip May 01 '15 at 16:02