Results 1 to 3 of 3
  1. #1
    snobbysteven is offline Member
    Join Date
    Oct 2012
    Rep Power

    Angry how to use Scanner when taking individual Characters

    so my issue is I have to take in a user input and determine if it is a Palindrome(the word is the same spelled backwards) or not. I want to just have the user put in each character individually and those will go into my array and then i can reverse the array output into a different variable and then test if the two words are the same or not. I know text=keyboard.nextchar[] doesnt work. So does anyone know of a way for me to put Char into my array?

    Java Code:
    import java.util.Scanner;
    public class Palindrome
      char[] text = new char[80]; 
      public void getInput()
        Scanner keyboard = new Scanner(;
        System.out.println("Please enter your word one character at a time, place a period when done");
        for(int index = 0; index < text.length; index++)
        text = keyboard.nextchar[];

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: how to use Scanner when taking individual Characters

    You can do it by matching a pattern. The pattern "." matches a single character. Just check out the Scanner API documentation. However, an easier way (in my opinion) is to simply read in a lines of text and append them together. Then just iterate over the total length with a for loop using the charAt() method. For efficiency I would use StringBuilder. But you can also use just String. You could also convert the String to a char array and iterate over that.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: how to use Scanner when taking individual Characters

    In your code you refer to the user's input as a word. Do you intend it all to be on one line?

    If so, one way of proceeding would be to read the word as a single line String and then turn the string into an array of char. There is a String method that does this conversion.

    [Edit] Note that you can't (easily and in a portable way) read individual characters from the console. Neither can you respond to keystrokes or similar. Having the user end their input with a period (rather than hitting <enter>) is just making things difficult for yourself by adding some input that you are going to have to ignore.
    Last edited by pbrockway2; 03-26-2013 at 11:56 PM.

Similar Threads

  1. Using operations on individual programs
    By The2b in forum New To Java
    Replies: 5
    Last Post: 03-29-2012, 11:20 AM
  2. Controlling individual threads
    By youngstorm in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-03-2010, 10:37 PM
  3. * vs. individual package name?
    By XmisterIS in forum New To Java
    Replies: 3
    Last Post: 09-01-2010, 12:19 PM
  4. Scanner reading accented characters
    By Phenomena in forum New To Java
    Replies: 2
    Last Post: 04-29-2010, 04:06 PM
  5. Taking args consisting of a scanner input
    By Implode in forum New To Java
    Replies: 1
    Last Post: 09-27-2009, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts