Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with keyboard.next()

807601May 3 2008 — edited May 4 2008
OK, I've read just about every post on this page concerning this issue and have not been able to fix it, so any help would be greatly appreciated. The entire program works, but I can't seem to get the loop to work. If I change the final statement inside the loop from answer = keyboard.next(); to answer = "y"; it seems to work like a charm and repeat indefinitely. However, as soon as I try to get it to read user input the loop exits and the program closes. Please help!!!

import java.util.*;


public class PalindromeTest

    {
        public static void main(String[] args)


            {

            Scanner keyboard = new Scanner(System.in);
            String answer;
            answer= "y";
            System.out.println("This program will test to see if a word is a Palindrome.");
            System.out.println("This program will ignore case and all white space,");
            System.out.println("All entries should be followed by a period (.)!");



             do {

                    System.out.println();
                    System.out.println("Please input a string followed by a (.):");



                    String str;
                    String strTwo;
                    keyboard.useDelimiter("\\.");
                    str = keyboard.next();
                    str = str.replaceAll("\\s", "");
                    strTwo = (ReverseString.reverseIt(str));

                    System.out.println("String one is: " + str);
                    System.out.println("String one reversed is: " +strTwo);
                    System.out.println();

                    if (str.equalsIgnoreCase(strTwo))
                        {System.out.println(" ~~ The String IS a Palendrome ~~");}
                    else
                        {System.out.println("~~ The String IS NOT a Palendrome ~~");}

                    System.out.println();

                    //Prompts user to continue using program

                    System.out.println("Would you like to test another word?");
                    System.out.println("Please Enter Y or N:");
                    answer = keyboard.next();
                    
                }

                while (answer.equalsIgnoreCase("y"));







            }
    }


class ReverseString

    {

        public static String reverseIt(String source)

          {
            int i, len = source.length();
            StringBuffer dest = new StringBuffer(len);

            for (i = (len - 1); i >= 0; i--)
            dest.append(source.charAt(i));
            return dest.toString();
           }
     }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 1 2008
Added on May 3 2008
11 comments
273 views