-1

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

Noivern Evo
  • 111
  • 3
  • 11
  • Do you get any errors / exceptions? – PM 77-1 Feb 15 '15 at 05:12
  • in the printWordBackwards you are using less than or equal to. It should be greater than or equal to. – Edward Jezisek Feb 15 '15 at 05:13
  • 2
    Best part: `// END LAB5 CLASS` – David Ehrmann Feb 15 '15 at 05:18
  • Nope, no errors/exceptions. The printed word backwards now works, thanks a bunch Edward! The Perfect number still doesn't show up however, so that is still an issue. – Noivern Evo Feb 15 '15 at 05:18
  • The loop in your `isPerfect` method is not written correctly, and the program is in an infinite loop. – ajb Feb 15 '15 at 05:20
  • Also, unless your instructor has given you a totally different definition of "perfect number" than I'm used to, I think you've chosen an incorrect method of determining whether a number is perfect. Your `isPerfect` method, when fixed according to Edward's answer, actually tests for triangular numbers, not perfect numbers. – ajb Feb 15 '15 at 05:30

2 Answers2

3

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);
}
Edward Jezisek
  • 522
  • 4
  • 11
  • 2
    Note: the loop in your answer is slightly different from the loop in the original question, which confuses the issue. – ajb Feb 15 '15 at 05:23
  • The loop in my answer is correct while the loop in the original question is incorrect which fixes the issue. – Edward Jezisek Feb 15 '15 at 05:23
  • Sorry, that's not clear from your answer--especially since you have a comment above your correct loop that says it's an infinite loop. I'm pretty confused by it. – ajb Feb 15 '15 at 05:24
  • 1
    @EdwardJezisek I think that what ajb is implying: though your fix is valid, since it looks very close to the original code - it might be difficult to spot the modifications. A bit of explanation might help here ;) – Nir Alfasi Feb 15 '15 at 05:25
  • 1
    Actually, I'm not convinced the loop is correct, anyway, since it appears to be testing whether a number is of the form 1+2+...+m, but a "perfect number" is quite a different thing. But I understand you were providing a corrected version of the OP's (probably incorrect) algorithm. – ajb Feb 15 '15 at 05:26
  • That's true. I didn't look up what a perfect number is. Good catch. I'll put a comment in my answer. – Edward Jezisek Feb 15 '15 at 05:29
1
for (int i = s.length()-1 ; i<= 0 ; i--)
{
    System.out.print(s.charAt(i));
}

What does this loop do? I know, I'm asking a question, not answering, but I'm trying to get you to help yourself.

David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
  • I fixed it, I had i set to LESS THAN or equal to zero when it should have been set to greater than. Thanks for the help :) – Noivern Evo Feb 15 '15 at 05:39