0

Hi .. Provably I'm setting this up the wrong way in Netbeans.. I open a new projetc and use the default main...then open a new empty java file and type my code , when I run this it runs fine but it doesn't ask for input !

import java.io.*;
public class Palindrome
{
     static boolean CheckPalindrome(String s, int leftSide, int rightSide)
     {
          if (rightSide <= leftSide)
               return true;
          else if (s.charAt(leftSide) != s.charAt(rightSide))
               return false; 
          else
               return CheckPalindrome(s,leftSide+1,rightSide-1);

     }
public static void main(String[] args) throws IOException
     {

          String str;
          int n;          
  
          InputStreamReader inStream = new InputStreamReader( System.in );
          BufferedReader stdin = new BufferedReader( inStream );
          
          System.out.print("Please enter any string: ");
          str = stdin.readLine(); 
  
          int lastPosition = str.length()-1; 
          boolean result = CheckPalindrome(str , 0, lastPosition);
  
          if (result) 
            System.out.println("The string \""+str+"\" is a palindrome "); 
          else          
            System.out.println("The string \""+str+"\" is not a palindrome "); 

     }

}

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by javaAddict
0

Does this print:

System.out.print("Please enter any string: ");

It should be printed at the bottom of the screen. Enter input there.

The code seems fine. If you run it from command prompt you should be fine.

0

Hi .. Provably I'm setting this up the wrong way in Netbeans.. I open a new projetc and use the default main...then open a new empty java file and type my code , when I run this it runs fine but it doesn't ask for input !

import java.io.*;
public class Palindrome
{
     static boolean CheckPalindrome(String s, int leftSide, int rightSide)
     {
          if (rightSide <= leftSide)
               return true;
          else if (s.charAt(leftSide) != s.charAt(rightSide))
               return false; 
          else
               return CheckPalindrome(s,leftSide+1,rightSide-1);

     }
public static void main(String[] args) throws IOException
     {

          String str;
          int n;          
  
          InputStreamReader inStream = new InputStreamReader( System.in );
          BufferedReader stdin = new BufferedReader( inStream );
          
          System.out.print("Please enter any string: ");
          str = stdin.readLine(); 
  
          int lastPosition = str.length()-1; 
          boolean result = CheckPalindrome(str , 0, lastPosition);
  
          if (result) 
            System.out.println("The string \""+str+"\" is a palindrome "); 
          else          
            System.out.println("The string \""+str+"\" is not a palindrome "); 

     }

}

Okay it works but how do I implement a method for example the regexp based method so it would skip blank spaces and punctuations(, . ? ;)?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.