Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,121 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,966 people online right now. Registration is fast and FREE... Join Now!




Java code not compiling

 
Reply to this topicStart new topic

Java code not compiling

peoples06
post 10 Oct, 2008 - 03:57 PM
Post #1


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 6

I know this is probably an easy fix, but I need help on my Java homework. In it, we're supposed to find the prime, perfect, and binary palindromes of a set of numbers from zero to MAX_NUMBER. I've got the perfect and the primes down, but the binary palindromes are tripping me up. I've got it written how I think it should go, but I get an error before compiling it. The error occurs under the listPalindromes method, specifically on the "if (isPalindrome(upperBound)){" line. It is telling me 'The method isPalindrome(String) in the type PrimePerfectPalindrome is not applicable for the arguments (int).' Again, I know this is probably simple, but I haven't been able to figure it out myself. Also, if anybody knows how to get the binary representation using the modulo operation and putting it together, any help would be appreciated. Thanks for your help.

CODE

package primePerfectPalindrome;

public class PrimePerfectPalindrome {

    /** Main method */
    public static void main(String []args) {

        //Set up the maximum number
        final int MAX_NUMBER = 100;

        //List all primes to MAX_NUMBER
        listPrimes(MAX_NUMBER);

        //List all perfects to MAX_NUMBER
        listPerfects(MAX_NUMBER);

        //List all binary palindromes
        listPalindromes(MAX_NUMBER);
    }

    /** listPrimes method */
    public static void listPrimes(int upperBound) {
        System.out.println("2 is prime.");
        for (int index = 3; index <= upperBound; index += 2){
            //Test if index is prime
            if (isPrime (index)){
                System.out.printf(index + " is prime.\n");
            }
        }
    }
    /** isPrime method */
    public static boolean isPrime(int number) {
        for (int index = 2; index <= Math.sqrt(number); index ++){
            if (number % index == 0){
                return false;
            }
        }
        return true;
    }
    /** listPerfects method */
    public static void listPerfects(int upperBound) {
        for (int index = 2; index < upperBound; index ++){
            //Test if index is perfect
            if (isPerfect(index)){
                System.out.println(index + " is perfect.");
            }
        }
    }
    /**isPerfect method */
    public static boolean isPerfect(int number) {
        int sumOfDivisors = 1;
        for (int divisor = 2; divisor < number - 1; divisor ++){
            if (number % divisor == 0){
                sumOfDivisors += divisor;
            }
        }
        if (sumOfDivisors == number){
            return true;
        }
        else{
            return false;
        }        
    }
    /**listPalindromes method */
    public static void listPalindromes(int upperBound) {
        for (int index = 0; index <= upperBound; index ++){
            if (isPalindrome(upperBound)){
                System.out.println(index + " is a binary palindrome.");
            }
        }
    }
    /**isPalindrome method */
    public static boolean isPalindrome(String binary) {
        int low = 0;
        int high = binary.length () - 1;
        while (low < high) {
            if (binary.charAt(low) != binary.charAt(high))
                return false;
            low ++;
            high ++;
        }
        return true;
    }
    /**intToBinaryString method */
    public static String intToBinaryString(int number) {
        String binary = Integer.toBinaryString(number);
        return binary;

    }
}

User is offlineProfile CardPM

Go to the top of the page

Locke37
post 10 Oct, 2008 - 04:38 PM
Post #2


I'm not a thief...I prefer the term TREASURE HUNTER!

Group Icon
Joined: 20 Mar, 2008
Posts: 911



Thanked 30 times

Dream Kudos: 300
My Contributions


The reason you get that compilation error is because your isPalindrome() method wants a String as the variable passed to it. Right now, you're passing upperBound, which is an int, to it.

Simply change that if line to this...

java
if (isPalindrome(Integer.toString(upperBound)){


I think that should solve that problem. biggrin.gif

This post has been edited by Locke37: 10 Oct, 2008 - 04:41 PM
User is offlineProfile CardPM

Go to the top of the page

peoples06
post 10 Oct, 2008 - 09:47 PM
Post #3


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 6

QUOTE(Locke37 @ 10 Oct, 2008 - 05:38 PM) *

The reason you get that compilation error is because your isPalindrome() method wants a String as the variable passed to it. Right now, you're passing upperBound, which is an int, to it.

Simply change that if line to this...

java
if (isPalindrome(Integer.toString(upperBound)){


I think that should solve that problem. biggrin.gif


Hey, thanks for your help. The code compiled, but it didn't list the binary palindromes. Any advice on this problem?
User is offlineProfile CardPM

Go to the top of the page

stauffski
post 10 Oct, 2008 - 10:22 PM
Post #4


D.I.C Head

**
Joined: 3 Nov, 2007
Posts: 137



Thanked 16 times
My Contributions


Great stuff, only thing you need to change is as follows:

java

public static void listPalindromes(int upperBound) {
for (int index = 0; index <= upperBound; index ++){
if (isPalindrome(Integer.toString(upperBound))){ //<----- change "upperBound" to "index"
System.out.println(index + " is a binary palindrome.");
}
}
}


java

public static boolean isPalindrome(String binary) {
int low = 0;
int high = binary.length () - 1;
while (low < high) {
if (binary.charAt(low) != binary.charAt(high))
return false;
low ++;
high ++; //<------ change "high++" to "high--";
}
return true;
}


This post has been edited by stauffski: 10 Oct, 2008 - 10:23 PM
User is offlineProfile CardPM

Go to the top of the page

peoples06
post 11 Oct, 2008 - 12:02 PM
Post #5


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 6

Well, I've got one more problem with my code. Never knew this would be this difficult. The code compiles and works for the most part, but the palindromes it returns are decimal palindromes, not binary ones. Again, it is probably an easy fix, but I can't find it. I'm doing it like my book is telling me to. Any help would be appreciated.

CODE

package primePerfectPalindrome;

public class PrimePerfectPalindrome {

    /** Main method */
    public static void main(String []args) {

        //Set up the maximum number
        final int MAX_NUMBER = 100;

        //List all primes to MAX_NUMBER
        listPrimes(MAX_NUMBER);

        //List all perfects to MAX_NUMBER
        listPerfects(MAX_NUMBER);

        //List all binary palindromes
        listPalindromes(MAX_NUMBER);
    }

    /** listPrimes method */
    public static void listPrimes(int upperBound) {
        System.out.println("2 is prime.");
        for (int index = 3; index <= upperBound; index += 2){
            //Test if index is prime
            if (isPrime (index)){
                System.out.printf(index + " is prime.\n");
            }
        }
    }
    /** isPrime method */
    public static boolean isPrime(int number) {
        for (int index = 2; index <= Math.sqrt(number); index ++){
            if (number % index == 0){
                return false;
            }
        }
        return true;
    }
    /** listPerfects method */
    public static void listPerfects(int upperBound) {
        for (int index = 2; index < upperBound; index ++){
            //Test if index is perfect
            if (isPerfect(index)){
                System.out.println(index + " is perfect.");
            }
        }
    }
    /**isPerfect method */
    public static boolean isPerfect(int number) {
        int sumOfDivisors = 1;
        for (int divisor = 2; divisor < number - 1; divisor ++){
            if (number % divisor == 0){
                sumOfDivisors += divisor;
            }
        }
        if (sumOfDivisors == number){
            return true;
        }
        else{
            return false;
        }        
    }
    /**listPalindromes method */
    public static void listPalindromes(int upperBound) {
        for (int index = 0; index <= upperBound; index ++){
            if (isPalindrome(Integer.toString(index))){
                System.out.println(index + " is a binary palindrome.");
            }
        }
    }
    /**isPalindrome method */
    public static boolean isPalindrome(String binary) {
        int low = 0;
        int high = binary.length () - 1;
        while (low < high) {
            if (binary.charAt(low) != binary.charAt(high))
                return false;
            low ++;
            high --;
        }
        return true;
    }
    /**intToBinaryString method */
    public static String intToBinaryString(int number) {
        String binary = "";
        while (number > 0){
            if (number % 2 == 0){
                binary = 0 + binary;                
            }
            else{
                binary = 1 + binary;
            }
        }
        return binary;
    }
}


User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 11:02AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month