Unable to run Palindrome program in Java [duplicate] - java

This question already has answers here:
Check string for palindrome
(42 answers)
Closed 2 years ago.
Here's my Java code to find a palindrome of a string. It shows o/p as "Palindrome" even though the i/p that I've entered is not. Can anyone please help.
String a = sc.next();
char b[] = a.toCharArray();
char d[] = b;
int size = a.length();
int beg=0,end=size-1;
while(beg<=end)
{
char temp = b[beg];
b[beg] = b[end];
b[end] = temp;
beg++;
end--;
}
if(d.equals(b))
{
System.out.print("Palindrome");
}
else
System.out.print("Not a Palindrome");

There are a few problems with your implementation. First of all -
while(beg<=end)
{
char temp = b[beg];
b[beg] = b[end];
b[end] = temp;
beg++;
end--;
}
Debug carefully and see do you really have d[] as the reverse of b[] at the end of this while loop
Secondly,
if(d.equals(b))
{
System.out.print("Palindrome");
}
This is not the correct way to compare if all the array elements are same in both of the array. If you look into the implementation or try out with some sample array you will be able to see it yourself.
To check palindrome, very simple approach is to reverse the string using StringBuilder and check if it's equal to the original string -
Scanner sc = new Scanner(System.in);
String a = sc.next();
String aRev = new StringBuilder(a).reverse().toString();
if (a.equals(aRev)) {
System.out.print("Palindrome");
} else {
System.out.print("Not a Palindrome");
}
Another better approach is to run a loop from beginning to middle of the string and keep an index from end to middle. Then check both forward index and backward index.
Scanner sc = new Scanner(System.in);
String a = sc.next();
boolean palindrome = true;
for (int i = 0; i < a.length() / 2; i++) {
if (a.charAt(i) != a.charAt(a.length() - i - 1)) {
palindrome = false;
break;
}
}
System.out.println(palindrome ? "Palindrome" : "Not Palindrome");

With
char d[] = b;
you create the alias d for b. So, in essence, b and d refer to the same char-Array. There is no difference beween modifying b and modifying d. Therefore, the comparison d.equals(b) will always yield true.

Try this code, it works as expected.
public class Main {
public static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
// The following condition checks if the decreasing index
// is larger than the increasing one,
// which happens at the middle of the string.
// This is done, because there is no need to iterate over,
// the entire string.
while(j > i){ // Non-matching character found, return early.
if(str.charAt(i) != str.charAt(j)){
return false;
}
j--; // Ending index decreases.
i++; // Starting index increases.
}
// If the loop finishes, without returning all characters of the string match.
return true;
}
public static void main(String[] args) {
String string = "Non-palindrome";
String palindrome = "WoW";
System.out.println("String " + string + " is palindrome: " + isPalindrome(string));
System.out.println("String " + palindrome + " is palindrome: " + isPalindrome(palindrome));
}
}
When run, the above code outputs:
String Non-palindrome is palindrome: false
String WoW is palindrome: true

Related

palindrome program not working as expected

I know it's very puny thing for experts here but I want to check why my palindrome program is not working as expected, it shows as palindrome to every number or string i enter, can you please look into it where the issue is, please.
actually i'm trying to create this program on my own and not checking any ready made method for it so asking here. please help.
here's my program
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
String k = sc.next();
int s = k.length()/2;
boolean b = true;
while(s>0){
for (int i = 0; i<=s; i++) {
if(k.charAt(i)==k.charAt(s)){
b = true;
}
}
if (b)
s--;
else
System.out.println("exit");
}
if(b)
System.out.println("palindrome");
}
}
s is the midpoint, and you are modifying it in your loop. Also, you never set b to false in any condition. Fixing those two bugs, should give you something like
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
String k = sc.next();
int s = k.length() / 2;
boolean b = true; // <-- Default to true
for (int i = 0; i <= s; i++) { // <-- Only need one loop.
if (k.charAt(i) != k.charAt(k.length() - i - 1)) {
b = false; // <-- Only need to update when it isn't a palindrome.
break; // <-- terminate the loop.
}
}
if (b) { // <-- Use braces. Even when optional.
System.out.println("palindrome");
}
You might be better off writing a simple method to do the check and call that method with your input.
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
String str = sc.next();
System.out.printf("'%s' %s%n", str, (isPalindrome(str) ? "is " : "is not ") + "a palindrome.");
For inputs of radar and abcdcbc prints
'radar' is a palindrome.
'abcdcbc' is not a palindrome.
The method
public static boolean isPalindrome(String str) {
int len = str.length();
for (int i = 0; i < len / 2; i++) {
if (str.charAt(i) != str.charAt(len - i - 1)) {
return false; // return as soon as characters don't match
}
}
// if the program gets this far, the string must be a palindrome
return true;
}

Flipping binary numbers using recursion

I am working on a project that the user enters an odd binary number ex: 10101 and I am supposed to have a recursive method that will flip the 1's and 0's ex: 101 = 010. For some reason my code is getting rid of the leading zero making the number an even number which crashes the program. Any and all help is appreciated, below is my Recursive class that is supposed to do the work of flipping the binary digits.
public class Recursive2 {
// int digit;
String temp;
public Recursive2(int d){
//digit = d;
temp = recursive(Integer.toString(d));
//System.out.print(recursive(temp));
}
public String toString(){
return temp;
}
public String recursive(String a){
String tempStr = "";
if(a.length() % 2 == 0){
System.out.println("Even number");
return "";
}
else if(a.length() == 1){
if(a.equals("1")){
tempStr = "0";
// tempStr += d;
}
else if(a.equals("0")){
tempStr= "1";
}
//System.out.println("Flipped d to" + tempStr);
}
else{
tempStr += new Recursive2(Integer.parseInt(a.substring(0,1))).toString();
tempStr += new Recursive2(Integer.parseInt(a.substring(1, a.length() - 1))).toString();
tempStr += new Recursive2(Integer.parseInt(a.substring(a.length()-1, a.length()))).toString();
}
return tempStr;
}
Here is my main class that tests the recursion.
public static void main(String[] args){
String num;
Scanner in = new Scanner(System.in);
System.out.println("Please enter a Binary number with an odd amount of digits: ");
num = in.nextLine();
System.out.println(num);
int num2 = Integer.parseInt(num);
System.out.println(num2);
Recursive2 test = new Recursive2(num2);
System.out.println(test);
}
You made several mistakes:
Deciding if the number is odd or even should be done before calling recursive method. Currently, your code will stop after at most one invocation, because you check the number of bits.
There is no need to parse int and making it String again. The entire process can be done entirely with strings.
Constructing the result should consist of appending the result of recursive invocation without the last bit to the string representing the inverse of the last bit.
The base case should be a situation when the string is empty, not when the string has exactly one digit.
Fixing these mistakes will produce a correct implementation.
The current algorithm is beyond repair.
Here's a recursive algorithm that's very simple and that will work:
if the string is empty, return it
if the first char is 0, return "1" + recurse(s.substring(1))
otherwise (the first char is 1), return "0" + recurse(s.substring(1))
Leading zeros are ignored because you're converting the input binary number to integer. For an integer leading zeros don't mean anything. You should be working with String only.
Also Integer.parseInt(num); would assume that you want to parse a decimal number not binary. If you do want to parse a binary number then you'll have to use another overloaded method Integer.parseInt(String s, int radix)
However as I said, because of the leading zeros you should work only with the Strings.
Integer.parseInt(i) converts the String to its Integer value.
However the Integer value of 010 is 10, leading 0's are dropped.
Work with the input as a String to avoid this.
Use StringBuilder to avoid creating new String or you can use char array
public static void main(String[] args) {
int no = 1234; // some no
String in = Integer.toBinaryString(no);
System.out.println("Original " + in);
System.out.println("Flipped " + flipBits(in));
}
public static String flipBits(String in) {
if (in.length() % 2 == 0)
return "even length";
return new String(flipBits(in.toCharArray(), in.length() - 1));
}
private static char[] flipBits(char[] ch, int index) {
if (index < 0) {
return ch;
} else {
ch[index] = Character.forDigit(49 - ch[index], 2); //flip
return flipBits(ch, index - 1);
}
}
output
Original 10011010010
Flipped 01100101101
The problem is that you are converting to integer values, which will throw away leading zeros. Just deal with strings:
public static void main(String[] args){
String num;
Scanner in = new Scanner(System.in);
System.out.println("Please enter a Binary number with an odd amount of digits: ");
num = in.nextLine();
System.out.println(num);
String flipped;
if (num.length() % 2 == 0) {
flipped = "Even number";
else {
flipped = Recursive2.recursive(num);
}
System.out.println(flipped);
}
And the Recursive2 class can just have static methods:
public class Recursive2 {
private static String flip(char c) {
if (c == '0') return "1";
return "0"; // assumes characters are all 0 or 1
}
public static String recursive(String a) {
STring tempStr;
if (a.length() == 0) {
tempStr = "";
} else {
tempStr = flip(a.charAt(0)) + recursive(a.substring(1));
}
return tempStr;
}
}
You might want to throw an exception if you detect a character other than 0 or 1.
EDIT: Based on your comment, the recursive method can be written as:
public static String recursive(String a) {
String tempStr = flip(a.charAt(0));
final int len = a.length();
if (len > 1) {
tempStr += recursive(a.substring(1, len - 1)) + flip(a.charAt(len - 1));
}
return tempStr;
}
This assumes that the argument is an odd-length string. That check should be done outside the recursive method, since if it is true once, it is true at every subsequent recursion.
I'd advise you to instead, remove one digit from the String that the user has imputed, and convert it (from the 1 to 0 vise versa), then use sub-string as the pass for the recursive function with one less.
Hint-> the base case would be a.length() == 0; because if you remove one digit of the string you will eventually have the length be 0.
Good Luck!

Check if charAt are the same (case sensitive)

Im have to write a method to check if a word is a palindrome. There is probably a easier way then I have it but this is just based off what I have learned so far. My method works except if there is a capital letters compared to a lowercase.
Edit: wasn't very clear. My method returns that a capital and lower case letter are the same. But I would like it to say they are different
public static void printPalindrome(Scanner kb) {
System.out.print("Type one or more words: ");
String s = kb.nextLine();
int count = 0;
for(int i = 0; i < s.length();i++) {
char a = s.charAt(i);
char b = s.charAt(s.length()-(i+1));
if (a==b) {
count ++;
} else {
count = count;
}
}
if (count == s.length()) {
System.out.print(s + " is a palindrome!");
} else {
System.out.print(s + " is not a palindrome.");
}
}
I'd recommend a slightly different approach, I'd reverse the string using StringBuilder#reverse and then compare the two strings using String#equalsIgnoreCase
String s = kb.nextLine();
StringBuilder sb = new StringBuilder(s).reverse();
if (s.equalsIgnoreCase(sb.toString())) {
...
} else {
...
}
You can solve your problem by converting the input String to upper case :
String s = kb.nextLine().toUpperCase();
Or if you wish to preserve the case of the original String, create a new String and test if it's a palindrome.
String s = kb.nextLine();
String u = s.toUpperCase();
int count = 0;
for(int i = 0; i < u.length();i++) {
char a = u.charAt(i);
char b = u.charAt(u.length()-(i+1));
if (a==b) {
count ++;
} else {
count = count;
}
}
i think you can do it with its ascii values
look this picture
then you shoul convert your char to ascii
char character = 'a';
int ascii = (int) character;
then compare the integers

How do you check if a string is a palindrome in java?

I am trying to write a program that checks if a string is a palindrome and so far I know I am on the right path but when I enter my code it keeps on running for ever. I don't know what the problem is and would like help finding out the solution. In my program I want the user to enter word or words in the method Printpalindrome and then the program should know if the string is a palindrome or not.
Here is my code:
...
Scanner console = new Scanner (System.in);
String str = console.next();
Printpalindrome(console, str);
}
public static void Printpalindrome(Scanner console, String str) {
Scanner in = new Scanner(System.in);
String original, reverse = "";
str = in.nextLine();
int length = str.length();
for ( int i = length - 1; i >= 0; i-- ) {
reverse = reverse + str.charAt(i);
}
if (str.equals(reverse))
System.out.println("Entered string is a palindrome.");
}
}
Because of this line:
n = in.nextLine();
your program is waiting for a second input, but you already got one before entering the function.
Remove this line and it works.
Here's your program, cleaned (and tested) :
public static void main(String[] args){
Scanner console = new Scanner (System.in);
String n = console.next();
Printpalindrome(n);
}
public static void Printpalindrome(String n){
String reverse = "";
for ( int i = n.length() - 1; i >= 0; i-- ) {
reverse = reverse + n.charAt(i);
System.out.println("re:"+reverse);
}
if (n.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is NOT a palindrome.");
}
Of course, this isn't the best algorithm, but you already know there are many QA on SO with faster solutions (hint: don't build a string, just compare chars).
Remove
Scanner in = new Scanner(System.in);
and
n = in.nextLine();
from Printpalindrome function
and it should work.
This can be implemented in a far more efficient manner:
boolean isPalindrom(String s){
if (s == null /* || s.length == 0 ?*/) {
return false;
}
int i = 0, j = s.length() - 1;
while(i < j) {
if(s.charAt(i++) != s.charAt(j--)) {
return false;
}
}
return true;
}
The argument for PrintPalindrom is ignored. You read another value with `in.nextLine()'. Which is the reason for your issues.
Ur code with some correction:-
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
I tried your code and what i observed was that :
first of all you are making a string to enter on the line 2 of your code:
String n=console.next();
next the the program again goes to waiting when this line gets executed:
n = in.nextLine();
actually this particular line is also expecting an input so that is why the program halt at this point of time.
If you enter your String to be checked for palindrome at this point of time you would get the desired result .
But I would rather prefer you to delete the line
n = in.nextLine();
because, with this, you would have to enter two words which are ambiguous.

Hangman in Java throws ArrayIndexOutOfBounds

My programs throws StringIndexOutOfBoundsException at this segment of code: temp1 = temp.replace('-', temp.charAt(p)); I'm trying to get the index of the same letter (after comparing inputted letter and word) and removing the '-' to show that the user has guessed correctly.** **I've been trying for hours to no avail. I think the problem lies in my loops. Thanks for the answers :) if I violated anything, please forgive me.
run:
-----
Enter a letter:
a
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range:
3
at java.lang.String.charAt(String.java:658)
at Hangman.main(Hangman.java:34)
Java Result: 1
import java.util.Scanner;
public class Hangman {
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
String word = "Sample";
String temp = null;
String temp1 = null;
String letter = null;
int n;
int m=0;
int p = 0;
for (n = 0; n<word.length(); n++){
temp = word.replaceAll(word, "-"); //replaces the String word with "-" and prints
System.out.print(temp);
}
while (m<=5){ //the player can only guess incorrectly 5 times
System.out.println("\nEnter a letter:");
letter = sc.nextLine();
letter.toLowerCase();
if (word.contains(letter) == true){
p = word.indexOf(letter);
temp1 = temp.replace('-', temp.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
System.out.print(temp1);
}
else {
System.out.print("\nMissed: "+letter); //if not, Missed: +the given letter
m++; //to count for incorrect guesses
}
System.out.print(temp1);
}
System.out.println("Game Over.");
}
}
When you do this:
temp = word.replaceAll(word, "-");
...you are setting temp to be just "-", and not (for example) "----". To see why, consider if word is "hello"; then this line looks like:
temp = "hello".replaceAll("hello", "-");
So then later you are assuming that temp is as long as word is, because you find an index in word and try to access that character in temp. But temp is only one character long, hence the exception.
p = word.indexOf(letter);
temp1 = temp.replace('-', temp.charAt(p));
Try this one.....
This will solve your problem...!!
package beans;
import java.util.Scanner;
public class Hangman {
public static String replace(String str, int index, char replace){
if(str==null){
return str;
}else if(index<0 || index>=str.length()){
return str;
}
char[] chars = str.toCharArray();
chars[index] = replace;
return String.valueOf(chars);
}
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
String word = "Sample";
String temp = "";
String letter = null;
int n;
int m=0;
int p = 0;
for (n = 0; n<word.length(); n++){
temp = temp + word.replaceAll(word, "-"); //replaces the String word with "-" and prints
}
System.out.print(temp);
while (m <= 5){ //the player can only guess incorrectly 5 times
System.out.println("\nEnter a letter:");
letter = sc.nextLine();
letter.toLowerCase();
if (word.contains(letter) == true){
p = word.indexOf(letter);
temp = replace(temp, p , word.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
System.out.println(temp);
}
else {
System.out.print("\nMissed: "+letter); //if not, Missed: +the given letter
m++; //to count for incorrect guesses
}
}
System.out.println("Game Over.");
}
}
You shoud check documentation for replaceAll() method.Cause you are using it wrong.
replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
You putting whole string into regex parameter
If you do myString.replaceAll("\\.","-"); (use double backslash to specify regex) will replace any character beside newline with "-" check into regex. Regullar expressions
if (word.contains(letter) == true){
p = word.indexOf(letter);
temp1 = temp.replace('-', temp.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
System.out.print(temp1);
}
the word.indexOf(letter); return index of letter if that latter is present in string otherwise -1. that's why you are getting Exception.

Categories