Palindrome won't output correcly - java

When I compile the program is comes up with the error exception in thread "main"
I I don't know why. I'm trying to create a program that allows the user to input a word and then output whether or not the word is a palindrome
import java.util.*;
public class Palindrome{
public static void main( String[] args ){
String word=getWord();
boolean w=isPalindrome(word);
if(w==true)
System.out.println(word + " is a palindrome");
else
System.out.println(word + " is not a palindrome");
}
public static String getWord(){
Scanner keyboard = new Scanner( System.in );
String word;
System.out.print("Enter a word: ");
word=keyboard.nextLine();
return word;
}
public static boolean isPalindrome(String word){
int y=word.length();
for (int i = 0; i < y; i++) {
if (word.charAt(i) != word.charAt(y-i)
return false;
}
return true;
}
}

Line if (word.charAt(i) != word.charAt(y-i) is obviously missing right round bracket.
if (word.charAt(i) != word.charAt(y-i)) is correct.

The if statement, presumably on that line, lacks a closing paren

Related

to check whether a string is palindrome or not using java

import java.util.Scanner;
public class palindrome{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String rev;
for(int i=str.length()-1,k=0;i>=0 && k<str.length();i--,k++)
{
rev.charAt(k) = str.charAt(i);
}
if(rev==str)
System.out.println("string is palidrome");
else
System.out.println("string is not palindrome");
}
}
what is wrong with this code?
note: error is showing at the following line of code
rev.charAt(k)=str.charAt(i);
.charAt(k) doesn't return a location, it only tells you what character is there.
Sample code for palindrome program
import java.util.*;
class PalindromeExample2
{
public static void main(String args[])
{
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner(System.in);
System.out.println("Enter a string/number 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/number is a palindrome.");
else
System.out.println("Entered string/number isn't a palindrome.");
}
}

Make the program the you if the word you've inputed in the console is a palindrome or not in Java

I've been trying to make this program work, have the person write a word in the console and make the console have an output saying if that word is or isn't a palindrome.
package sct;
import java.util.Scanner;
public class Assignment3Problem4 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a a string: ");
String DAWORD = reader.nextLine();
reader.close();
int n = DAWORD.length();
boolean isPalindrome = true;
for (int i = 0; i < n; ++i) {
if (DAWORD.charAt(i) != DAWORD.charAt(n - i - 1)) {
isPalindrome = false;
break;
}
if (isPalindrome)
System.out.println("This word is a palindrome");
else
System.out.println("This word is not a palindrome");
}
}
}
Sadly, this didn't work and the console doesn't let me input a string, can someone find out why and fix the code?
You shouldn't print whether the word is a palindrome (or not) until after the for loop. That's the only issue I had running the posted code.
Scanner reader = new Scanner(System.in);
System.out.println("Enter a a string: ");
String DAWORD = reader.nextLine();
reader.close();
int n = DAWORD.length();
boolean isPalindrome = true;
for (int i = 0; i < n; ++i) {
if (DAWORD.charAt(i) != DAWORD.charAt(n - i - 1)) {
isPalindrome = false;
break;
}
}
if (isPalindrome)
System.out.println("This word is a palindrome");
else
System.out.println("This word is not a palindrome");
You might want to remove reader.close() (as that also closes System.in).
Another approach you could consider is putting your input into a StringBuilder object, reverse it, then check if it equals your input.
Something like:
import java.util.Scanner;
public class StackOverflow {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a string: ");
String DAWORD = reader.nextLine();
reader.close();
if (new StringBuilder(DAWORD).reverse().toString().contentEquals(DAWORD))
System.out.println("This word is a palindrome");
else
System.out.println("This word is not a palindrome");
}
}
Result:
Enter a string:
racecar
This word is a palindrome
You can also use an external apache lang library from here https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.7. Then you can use StringUtils.reverse() method which reverses a string and then compare reversed string with the normal one , like so:
import org.apache.commons.lang3.StringUtils;
public class Main4 {
public static void main(String[] args) {
String str = "kajak";
String str2 =StringUtils.reverse(str);
if (str2.equals(str)){
System.out.println("It's a palidrom");
}
else{
System.out.println("Not a palidorm");
}
}
}

Java palindrome until EOF

I have a program that is supposed to ask the user for a number and it will determine whether it is a palindrome or not. It's supposed to keep asking for numbers until EOF is input - So far it asks for the number twice and doesn't seem to be doing the while loop correctly.
Any insight is appreciated
import java.util.Scanner;
public class PalindromeEOF
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to check if it is a palindrome:");
String num = scanner.nextLine();
String reverse = "";
while (scanner.hasNextLine())
{
for ( int i = 0; i<num.length(); i++ )
{
reverse = num.charAt(i) + reverse;
}
if (num.equals(reverse))
{
System.out.println("\nEntered number IS a palindrome.");
}
else
{
System.out.println("\nEntered number is NOT a palindrome.");
}
System.out.println("\nEnter a number to check if it is a palindrome:");
num = scanner.nextLine();
reverse = "";
}
System.out.println("\nProgram ended on request");
}
}
This worked for me; unless you need num or reverse outside the while loop it should work.
import java.util.Scanner;
public class PalindromeEOF
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to check if it is a palindrome:");
while (scanner.hasNextLine())
{
String num = scanner.nextLine();
String reverse = "";
for ( int i = 0; i<num.length(); i++ )
{
reverse = num.charAt(i) + reverse;
}
if (num.equals(reverse))
{
System.out.println("\nEntered number IS a palindrome.");
}
else
{
System.out.println("\nEntered number is NOT a palindrome.");
}
System.out.println("\nEnter a number to check if it is a palindrome:");
}
System.out.println("\nProgram ended on request");
}
}
I would separate the palindrome test into its' own method. You could do that in a one line method like
public static boolean isPalindrome(String str) {
return new StringBuilder(str).reverse().toString().equals(str);
}
but I would prefer to iterate the first half of the characters and compare them to the second half in reverse like
public static boolean isPalindrome(String str) {
if (str == null) {
return false;
}
char[] chars = str.toCharArray();
for (int i = 0; i * 2 <= chars.length; i++) {
if (chars[i] != chars[chars.length - i - 1]) {
return false;
}
}
return true;
}
Then your main can invoke that in an infinite loop (terminating on the lack of input) like
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter a number to check if it is a palindrome:");
if (!scanner.hasNextLine()) {
break;
}
String num = scanner.nextLine();
if (isPalindrome(num)) {
System.out.printf("%s is a palindrome%n", num);
} else {
System.out.printf("%s is NOT a palindrome%n", num);
}
}
System.out.println("Program ended on request");
}

The number occurrence of a character is to be counted in a given line string. Can anyone point out where the error is? The code compiles correctly

The idea is to count the number of occurrence of a given character that a user inputs in a line of string that also is imputed by the user. The idea is to use recursion in Java rather than some sort of loop. The code compiles correctly and runs correctly. But the result does not give a correct answer (does not count the asked character correctly in a given line of string.) Can anyone point out?
import java.util.Scanner;
public class numberOfLetters {
public static int letterCounter(String line, String x)
{
if(line.isEmpty())
{
return 0;
}
else
{
if(line.charAt(0) == 'x')
{
return 1 + letterCounter(line.substring(1), x);
}
else
{
return 0 + letterCounter(line.substring(1), x);
}
}
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a line of a string >_ ");
String inputLine = keyboard.nextLine();
System.out.println("Enter the character to count >_ ");
String charac = keyboard.nextLine();
int count = letterCounter(inputLine, charac);
System.out.println("The number of '"+ charac + "' in the input '"+ inputLine + "' is "+ count);
keyboard.close();
}
}
You're testing for 'x' instead of the variable:
if(line.charAt(0) == 'x')
you need:
if(line.charAt(0) == x)
Also pass x in as a char instead of String.
You need to check character against character x
import java.util.Scanner;
public class LetterCounter {
public static int letterCounter(String line, char x)
{
if (line.isEmpty()) return 0;
if(line.charAt(0) == x) {
return 1 + letterCounter(line.substring(1), x);
}
else {
return 0 + letterCounter(line.substring(1), x);
}
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a line of a string >_ ");
String inputLine = keyboard.nextLine();
System.out.println("Enter the character to count >_ ");
String charac = keyboard.nextLine();
char c = charac.charAt(0);
int count = letterCounter(inputLine, c);
System.out.println("The number of '"+ c + "' in the input '"+ inputLine + "' is "+ count);
keyboard.close();
}
}
You say you want to count the number of occurrences of characters, but your "x" parameter is a string. I'd recommend you use "char" to fit with your purpose. If you want "x" to be a string indeed, then your algorithm would need to be somewhat more elaborated and deal with substrings of varying size.
Another issue, you are checking in your "if" statement against the constant 'x', not your parameter.
proposed code (don't have Java on this machine, so untested):
public static int letterCounter(String line, char x)
{
if(line.isEmpty())
{
return 0;
}
else
{
if(line.charAt(0) == x)
{
return 1 + letterCounter(line.substring(1), x);
}
else
{
return 0 + letterCounter(line.substring(1), x);
}
}
}
A simpler solution:
//Input validation should be performed in main
//Invoke from main
int count = letterCounter(inputLine, charac.charAt(0));
public static int letterCounter(String line, char x) {
int counter = 0;
for (char c : line.toCharArray())
if (c == x)
counter++;
return counter;
}

palindrome - how to make main method call the boolean method?

I am trying to get user input and determine if the word is a palindrome or not. The main method should be where all of the print statements are placed.
package help;
import java.util.Scanner;
public class Help {
public static Scanner user_input;
public static void main(String[] args, Iterable<String> lines) {
System.out.print("Enter a word: ");
user_input=new Scanner(System.in);
}
public static boolean istPalindrom(char[] word) {
int i1 = 0;
int i2 = word.length - 1;
while (i2 > i1) {
if (word[i1] != word[i2]) {
return false;
}
++i1;
--i2;
}
return true;
}
}
I want it to say
if true then System.out.println(The word is a palindrome.)
else System.out.println(The word is not a palindrome.)
I am not sure how to go about this.
Add following lines into main:
if (isPalindrome(user_input))
System.out.println ("The word is a palindrome.");
else
System.out.println("The word is not a palindrome.");
Note: Try to make your indentation better in order to make code easily understandable and for visual purposes :)
Your palindrome test looks good once you fix the indentation, but it's named oddly in English. You have a Scanner, so you can get lines of input. Then call toCharArray() on the String to get the char[] like
public static void main(String[] args) {
System.out.print("Enter a word: ");
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
String line = input.nextLine();
if (isPalindrome(line.toCharArray())) {
System.out.printf("The word %s is a palindrome.%n", line);
} else {
System.out.printf("The word %s is not a palindrome.%n", line);
}
}
}
public static boolean isPalindrome(char[] word) {
int i1 = 0;
int i2 = word.length - 1;
while (i2 > i1) {
if (word[i1] != word[i2]) {
return false;
}
++i1;
--i2;
}
return true;
}
This solution might work:
public static void main(String[] args, Iterable<String> lines) {
user_input = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = user_input.nextLine();
if(istPalindrome(word.toCharArray()){
System.out.println("The word is a palindrome");
}else{
System.out.println("The word is not a palindrome");
}
}
The code for palindrome as simple as this if you want to use it !
public static boolean isPalindrom(String word) {
if(new StringBuilder(word).reverse().toString().equals(word)){
System.out.println("The word is a palindrome.");
} else {
System.out.println("The word is not a palindrome.");
}
}

Categories

Resources