Search code examples
javafor-loopmethodspalindromeperfect-numbers

My Java program is only outputting two answers, the other two aren't shown


My program should spell the inputted word backwards, follow to the next line and answer if the word inputted is a Palindrome. Then it will take the accompanying number, output if it is prime or not then output if it is perfect or not.

My output in command prompt is only showing the palindrome part and the prime-number part. I'm not quite getting why either.

Here is the full code, it is a homework assignment so you can disregard the comments.

public static void main (String[] args) throws Exception
{
    if (args.length == 0 ) // i.e  If you did not type anything after "java Lab5" on command line
    {
        System.out.println("FATAL ERROR: Must type a filename on cmd line\n" + 
                           "Like this ->   C:\\Users\\tim\\Desktop>java Lab5 words1.txt");
        System.exit(0);  //ABORT program. Make user try again with a filename this time
    }

    Scanner infile = new Scanner( new File(args[0]) );

    while ( infile.hasNext() )   
    {
        String word = infile.next(); // grab next token (word) from file

        // 1st method you must write below main:  printWordBackwards
        printWordBackwards( word );  // if word is "foobar"  your method prints "baroof"

        // 2nd method you must write below main:  isPalindrome

        if ( isPalindrome( word ) )
            System.out.println( word + " IS A PALINDROME" ); // DO NOT MODIFY/REMOVE
        else
            System.out.println( word + " NOT A PALINDROME" ); // DO NOT MODIFY/REMOVE

        // 3rd method you must write below main:  isPrime
        // NOTE WE ARE ASSUMING THAT EVERY OTHER TOKEN IN FILE IS AN INT TOKEN

        int number = infile.nextInt();  // grab next token and convert to int

        if ( isPrime( number  ) )
            System.out.println( number + " IS PRIME" ); // DO NOT MODIFY/REMOVE
        else
            System.out.println( number + " NOT PRIME" ); // DO NOT MODIFY/REMOVE            

        // 4th method you must write below main:  isPerfect
        // NOTE WE ARE ASSUMING THAT EVERY OTHER TOKEN IN FILE IS AN INT TOKEN

        if ( isPerfect( number  ) )
            System.out.println( number + " IS PERFECT" ); // DO NOT MODIFY/REMOVE
        else
            System.out.println( number + " NOT PERFECT" ); // DO NOT MODIFY/REMOVE

        System.out.println();
    } // END WHILE

    infile.close();  // WE ARE DONE WITH THE INPUT FILE./ CLOSE IT

} // END MAIN   

// WRITE YOUR FOUR METHOD DEFINITIONS DOWN HERE 

public static void printWordBackwards (String s)
{
    for (int i = s.length()-1 ; i<= 0 ; i--)
    {
        System.out.print(s.charAt(i));
    }
    System.out.println();
}

public static boolean isPalindrome (String s)
{
    int i = 0;
    int j = s.length() - 1;
    while (j > i)
    {
        if (s.charAt(i) != s.charAt(j))
        {
            return false;
        }
        ++i;
        --j;
    }
return true;
}

public static boolean isPrime (int i)
{
    for (int j = 2; j <= i/2; j++)
    {
        if (i % j == 0)
        {
            return false;
        }
    }
    return true;
}

public static boolean isPerfect (int i)
{
    for (int k = 1; i > 0; i++)
    {
        i -= k;
    }
    if (i == 0)
    {
        return true;
    }
return false;
}

}// END LAB5 CLASS


Solution

  • The idea for how to solve perfect numbers is incorrect in the original and this "fix".

    public static void printWordBackwards (String s)
    {
    //Please note that this is greater than or equal to not less than or equal to.
        //for (int i = s.length()-1 ; i<= 0 ; i--)  ORIGINAL
        for (int i = s.length()-1 ; i>= 0 ; i--)
        {
            System.out.print(s.charAt(i));
        }
        System.out.println();
    }
    public static boolean isPerfect (int i)
    {
    //This is an infinite loop.  K is always 1, and i subtracts k and adds 1 to it meaning it will never end it should be as follows below.
    // Please note that this does not mean it's a perfect answer, I simply fixed his original mistake
    //In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors,
    //for (int k = 1; i > 0; i++)  ORIGINAL
    for (int k = 1; i > 0; k++)
    {
        i -= k;
    }
    if (i == 0)
    {
        return true;
        }
    return false;
    }
    
    //More correct isPerfect using the http://en.wikipedia.org/wiki/Perfect_number document 
    //and the statement that all even perfect numbers are of the form 
    //2^(p-1)*((2^p)-1)
    // and that odd perfect numbers are either rare or don't exist.
    public static boolean betterIsPerfect(int i)
    {
        return i==Math.pow(2, i-1)*(Math.pow(2,p)-1);
    }