Search code examples
javapalindrome

java user input check palindrome and print out counting


I have a user input like this: banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james, joyce

2 questions for this: first, I would like to print with the whole line of user input. System.out.println("There are " + total + " words in " + banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james, joyce); Second, I was not able to print the last line. Please lecture me, thanks.

Code:

public static void main(String[] args)  {
    palindromeCheck();                      // test your method
}
// Part 3
/**
 * This program reads words, identifies, counts and writes all the palindromes and the total
 * palindrome count.
 */
public static void palindromeCheck(){

    // you could use any of the words below to test your method:
    // banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james, joyce

    String someWord = ""; // Stores words read from user input
    int count = 0;        // keeps track of Palindrome words only (define algorithm to count # of palindrome words
    int total = 0;        // Counts the total number of lines read from the given text file

    System.out.println(" Enter some words separated by white space");    // Ask for user input

    Scanner keyboard = new Scanner(System.in);

    // hint 1: Using keybord.next() will only return what comes before a space.
    // hint 2: Using keybord.nextLine() automatically reads the entire current line.
           // store each word in a string variable and then do your operations

    while (keyboard.hasNext()) {
        String temp = keyboard.next();
        total++;

        int Len = temp.length();

        for(int i=0 ; i < (Len/2) ; i++) {
            //debug: System.out.println("first word "+temp.charAt(i)+"  second word "+temp.charAt(Len-1-i));
            if(temp.charAt(i)== temp.charAt(Len-1-i))count++;
        }
        someWord += temp;
        System.out.println("There are " + total + " words in " + someWord);   // test
    }

    System.out.println("There are "+count +" palindromes out of "+total+" words.");

    keyboard.close();

    // x is a variable for count and y is variable total
    // #2. print “There are x palindromes out of y words”
    System.out.println("There are "+count +" palindromes out of "+total+" words.");

Solution

  • For question #1 change the variable someWords to a List<String> and for question #2 you should use a for loop instead of a while loop. Your last line was never printing because the while loop was never exiting.

    See the updated palindromeCheck method below:

    Code

    /** * This program reads words, identifies, counts and writes all the palindromes and the total * palindrome count. */ public static void palindromeCheck() {

        // you could use any of the words below to test your method:
        // banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james, joyce
    
        List<String> someWords = new ArrayList<>(); // Stores words read from user input
        int count = 0;        // keeps track of Palindrome words only (define algorithm to count # of palindrome words
        int total = 0;        // Counts the total number of lines read from the given text file
    
        System.out.println(" Enter some words separated by white space");    // Ask for user input
    
        Scanner keyboard = new Scanner(System.in);
    
        // hint 1: Using keybord.next() will only return what comes before a space.
        // hint 2: Using keybord.nextLine() automaticatlly reads the enire current line.
        // store each word in a string variable and then do your operations
    
        for (String word : keyboard.nextLine().split(" ")) {
            int length = word.length();
    
            for (int i = 0; i < (length / 2); i++) {
                //debug: System.out.println("first word "+temp.charAt(i)+"  second word "+temp.charAt(Len-1-i));
                if (word.charAt(i) == word.charAt(length - 1 - i)) count++;
            }
            someWords.add(word);
            System.out.println("There are " + someWords.size() + " words in " + someWords);   // test
        }
    
        System.out.println("There are " + count + " palindromes out of " + total + " words.");
    
        keyboard.close();
    
        // x is a variable for count and y is variable total
        // #2. print “There are x palindromes out of y words”
        System.out.println("There are " + count + " palindromes out of " + total + " words.");
    }
    

    Output

    Enter some words separated by white space
    banana sneeze radar roof kayak mine racer racecar refer james joyce
    There are 1 words in [banana]
    There are 2 words in [banana, sneeze]
    There are 3 words in [banana, sneeze, radar]
    There are 4 words in [banana, sneeze, radar, roof]
    There are 5 words in [banana, sneeze, radar, roof, kayak]
    There are 6 words in [banana, sneeze, radar, roof, kayak, mine]
    There are 7 words in [banana, sneeze, radar, roof, kayak, mine, racer]
    There are 8 words in [banana, sneeze, radar, roof, kayak, mine, racer, racecar]
    There are 9 words in [banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer]
    There are 10 words in [banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james]
    There are 11 words in [banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james, joyce]
    There are 12 palindromes out of 0 words.
    There are 12 palindromes out of 0 words.
    

    Just to note there is some issue with your logic to check if a word is a palindrome or not.