I really don't see what the problem could be. This is the error I'm getting:
$javac Palindrome.java
$java -Xmx128M -Xms16M Palindrome
Enter your word
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Palindrome.main(Palindrome.java:28)
This is the code:
import java.io.*;
import java.util.Scanner;
import java.util.*;
import java.lang.StringBuffer;
// Java program to illustrate checking of a string
// if its palindrome or not using reverse function
public class Palindrome
{
public static void checkPalindrome(String s)
{
// reverse the given String
String reverse = new StringBuffer(s).reverse().toString();
// check whether the string is palindrome or not
if (s.equals(reverse))
System.out.println("Yes");
else
System.out.println("No");
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your word");
String output = scanner.next();
}
}
I ask for the word and then get the input to check if it is a palindrome
In online editor this problem occurs in input. Try writing before getting inputs:
if(sc.hasNext())
The code can be written as:
public class Palindrome
{
public static void checkPalindrome(String s)
{
// reverse the given String
String reverse = new StringBuffer(s).reverse().toString();
// check whether the string is palindrome or not
if (s.equals(reverse))
System.out.println("Yes");
else
System.out.println("No");
}
public static void main (String[] args) throws java.lang.Exception
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your word");
if(sc.hasNext())
String output = scanner.next();
}
}