user input question

Posted by jdbeverly87 on Stack Overflow See other posts from Stack Overflow or by jdbeverly87
Published on 2010-04-19T00:51:04Z Indexed on 2010/04/19 0:53 UTC
Read the original article Hit count: 565

Filed under:
|
|
|

My program checks to test if a word or phrase is a palindrome (reads the same both backward and forward, ex "racecar"). The issue I'm having is after someone enters in "racecar" getting it to actually test. In the below code, I marked where if I type in "racecar" and run, Java returns the correct answer so I know I'm right there. But what am I missing as far as entering it into the console. I think my code is ok, but maybe I have something missing or in the wrong spot? Not really looking for a new answer unless I'm missing something, but if possible perhaps a pro at this moving my code to the correct area bc I'm stuck! `import java.util.*;

public class Palindrome {

public static void main(String[] args) { 
    String myInput; 
    Scanner in = new Scanner(System.in); 

    System.out.println("Enter a word or phrase: ");  **//this asks user for input but doesn't check for whether or not it is a palindrome**
    myInput = in.nextLine(); 
    in.close(); 

    System.out.println("You entered: " + myInput); 
}   

{
    String s="racecar";  **//I can type a word here and it works but I need**
    int i;               **//I need it to work where I ask for the input** 
    int n=s.length(); 
    String str="";  

    for(i=n-1;i>=0;i--)  
        str=str+s.charAt(i);  

    if(str.equals(s))  
        System.out.println(s+ " is a palindrome");  

    else  System.out.println(s+ " is not a palindrome"); }

}` I'm new at programming so I'm hoping what I got is ok. I know the palindrome test works I'm just needing helping having it test thru where I'm entering it into the console. Thanks

© Stack Overflow or respective owner

Related posts about input

Related posts about palindrome