JavaScript

Moderators: None (Apply to moderate this forum)
Number of threads: 2061
Number of posts: 5164

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Java Palindrome Program Posted by Azhar123 on 25 Jan 2005 at 5:34 PM
Hi! Just started using Java not to long ago and have a question for a program

I'm suppoosed to check to see if an entered String is a palindrome or not..I have created the checking method, but do not know if it is correct. I also have to still create the constructer method. Here is what I have so far:


// Lab16ast.java
// This is the student version of the Lab16a assignment.


import java.io.*;


public class Lab16a
{
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string ===>> ");
Palindrome p=new Palindrome(input.readLine());
p.displayData();


}
}


class Palindrome
{
private String s;
private boolean palindrome;

private boolean isPal()
{

int start = 0;
int end = s.length() - 1;
while (start <= end)
{
if (s.charAt(start) != s.charAt(end))
{
return false;
}
start++;
end--;
}

return true;

}

public Palindrome(String s)
{


}

public void displayData()
{
System.out.println(s);
System.out.println(palindrome);

}


}


Report
Re: Java Palindrome Program Posted by zibadian on 26 Jan 2005 at 12:37 AM
: Hi! Just started using Java not to long ago and have a question for a program
:
: I'm suppoosed to check to see if an entered String is a palindrome or not..I have created the checking method, but do not know if it is correct. I also have to still create the constructer method. Here is what I have so far:
:
:
: // Lab16ast.java
: // This is the student version of the Lab16a assignment.
:
:
: import java.io.*;
:
:
: public class Lab16a
: {
: public static void main (String args[]) throws IOException
: {
: BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
: System.out.println("Enter a string ===>> ");
: Palindrome p=new Palindrome(input.readLine());
: p.displayData();
:
:
: }
: }
:
:
: class Palindrome
: {
: private String s;
: private boolean palindrome;
:
: private boolean isPal()
: {
:
: int start = 0;
: int end = s.length() - 1;
: while (start <= end)
: {
: if (s.charAt(start) != s.charAt(end))
: {
: return false;
: }
: start++;
: end--;
: }
:
: return true;
:
: }
:
: public Palindrome(String s)
: {
:
:
: }
:
: public void displayData()
: {
: System.out.println(s);
: System.out.println(palindrome);
:
: }
:
:
: }
:
:
:
Although this is not really the right board for Java (Java and Javascript are two completely different languages) and I am not a Java programmer, I might see a problem in the isPal() method. The return value might always be true, if Java does not break out the method upon setting the return value to false.
Report
Re: Java Palindrome Program Posted by chiProgrammer on 26 Jan 2005 at 7:18 PM
Hey try this
import java.io.*; 
public class Palindrome
{
    private String palin; 
    public Palindrome()
    {
        palin = "";    
    }
    public Palindrome(String str)
    {
        palin = str; 
    }
    public static boolean testPalindrome(String s)
    {
        String a = s.toLowerCase( ); 
        String b = ""; 
        for (int x = 0; x < a.length ( ); x++)
        {
            char c = a.charAt(x); 
            if (c >='a' && c <= 'z')
            {
                b += c; 
            }
        }
        String d = ""; 
        for (int y = b.length()-1; y >= 0; y--)
        {
            d += b.charAt(y); 
        }
        return (b.equals(d)); 
    }
    
    public static void main(String []args)
    throws IOException
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
        System.out.println("\tWelcome to the Palindrome Tester!!!\t\n Enter a word Phrase or String\n Press  q or type quit to stop the program at anytime");  
        String ing = ""; 
        do{
            System.out.println("Input?"); 
            ing = in.readLine(); 
            if (testPalindrome(ing) == true)
            {
                System.out.println("\t"+ing+" is a palindrome"); 
            }
            else
            {
                System.out.println("\t"+ing+" is not a palindrome"); 
            }
        }while (!ing.equalsIgnoreCase("q") && 
                !ing.equalsIgnoreCase("quit")); 
        
     }
 }
   


: : Hi! Just started using Java not to long ago and have a question for a program
: :
: : I'm suppoosed to check to see if an entered String is a palindrome or not..I have created the checking method, but do not know if it is correct. I also have to still create the constructer method. Here is what I have so far:
: :
: :
: : // Lab16ast.java
: : // This is the student version of the Lab16a assignment.
: :
: :
: : import java.io.*;
: :
: :
: : public class Lab16a
: : {
: : public static void main (String args[]) throws IOException
: : {
: : BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
: : System.out.println("Enter a string ===>> ");
: : Palindrome p=new Palindrome(input.readLine());
: : p.displayData();
: :
: :
: : }
: : }
: :
: :
: : class Palindrome
: : {
: : private String s;
: : private boolean palindrome;
: :
: : private boolean isPal()
: : {
: :
: : int start = 0;
: : int end = s.length() - 1;
: : while (start <= end)
: : {
: : if (s.charAt(start) != s.charAt(end))
: : {
: : return false;
: : }
: : start++;
: : end--;
: : }
: :
: : return true;
: :
: : }
: :
: : public Palindrome(String s)
: : {
: :
: :
: : }
: :
: : public void displayData()
: : {
: : System.out.println(s);
: : System.out.println(palindrome);
: :
: : }
: :
: :
: : }
: :
: :
: :
: Although this is not really the right board for Java (Java and Javascript are two completely different languages) and I am not a Java programmer, I might see a problem in the isPal() method. The return value might always be true, if Java does not break out the method upon setting the return value to false.
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.