-2

I've tried different approaches to do this, and when I do this..

import java.util.*;
public class Palindrome {
 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);

  System.out.print("Enter number");
  int number = in .nextInt();
  int first = number
  int middle = 0;
  int last = first;
  boolean isPalindrome = last == first;

  if (isPalindrome) {
   System.out.print("This is a palindrome");
  } else
   System.out.print("This is not a palindrome");
 }
}

it spits out "this is a palindrome". Mind you I can't use loops. Shouldn't this work?

It works when I do this...

import java.util.*;
public class Palindrome {
 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);

  System.out.print("Enter number");
  int first = 1;
  int middle = 0;
  int last = 5;

  boolean isPalindrome = last == first;

  if (isPalindrome) {
   System.out.print("This is a palindrome");
  } else
   System.out.print("This is not a palindrome");
 }
}

It tells me if it's a palindrome or not... So, it works on my end, but not the users end.

What am I missing

Will_Panda
  • 534
  • 10
  • 26
Tillix
  • 1
  • 1
  • You should provide proper code. Also what kind of i/p you are expecting. Doing `last=first` and then `lass==first` doesn't make any sense. – R Dhaval Sep 20 '18 at 03:31
  • I'll try to make my code cleaner, but even without the boolean, it doesnt work. – Tillix Sep 20 '18 at 03:35
  • ow is this different from the question your asked befpre https://stackoverflow.com/questions/52415524/simple-palindrome-detector – Scary Wombat Sep 20 '18 at 04:06

2 Answers2

0

The problem is, you are assigning the value of the input to your variable first then you assigned the value of first to the variable last. You declared your boolean as isPalindrome = first==last (true) which will always be true in this scenario that's why you get "This is a palindrome". On your second block of code which you said "works" is because you declared first = 1 and last = 5, when you did isPalindrome = first==last (false) the value of the palindrome will always be false hence you get "This is not a palindrome" output.

I think what you need to do is rewrite your logic so you can elaborate the process you want to do. Feel free to ask a follow-up question.

javaprdgy
  • 29
  • 2
0

In the first one, the user enters a number which is assigned to first, then you set last to be a reference to first. Since the truth value of isPalindrome depends on whether last == first, it will always be true, since you coded it that way. However, this doesn't actually prove that the number is a palindrome, because for example if the user enters the number 56, the number 56056 is not a palindrome (reverse is 65065).

In the second program, first is always 1 and last is always 5, so since they are never equivalent the condition last == first will always be false.

What are you attempting to achieve with the programs? Neither one tests the user input to be a palindrome. Try writing psuedocode first, in the comments if you like.