2

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

Logan
  • 926
  • 1
  • 10
  • 18
oakanb2
  • 41
  • 1
  • 2
  • 6
  • Possible duplicate of [java.util.NoSuchElementException - Scanner reading user input](https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input) – Logan Jun 07 '18 at 18:50
  • @Logan each SO post I've seen with this issue the resolution is removing scanner.close() but I don't have that in my code so I don't see what the issue is. I'm also using a online IDE, could that be the issue – oakanb2 Jun 07 '18 at 19:03
  • are you able to enter input in this online IDE? – Logan Jun 07 '18 at 19:08
  • I am unable to replicate this error with the code given – GBlodgett Jun 07 '18 at 19:15
  • When you ran the `java -Xmx128M -Xms16M Palindrome` command, did the program wait for input after it printed `Enter your word`? If so, what _word_ did you type in? Did the error message appear after you hit Enter? – Mark Stewart Jun 07 '18 at 19:43
  • @MarkStewart the error message appeared as soon as I hit "Execute". This is the online IDE https://www.tutorialspoint.com/compile_java_online.php – oakanb2 Jun 07 '18 at 21:11

2 Answers2

3

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();
    }
    }
Mutaealim
  • 127
  • 11
1

I cannot post comments, since I dont have enough reputation. But the solution is that you have no input source.

The line your stacktrace refers to is:

 private void throwFor() {
    skipped = false;
    //since you are using an online tool, you dont actually have an
    //input unless you click on the stdin tab and provide an input.
    if ( (sourceClosed) && (position == buf.limit()))
        throw new NoSuchElementException();
    else
        throw new InputMismatchException();
}

Just press on the stdin tab and type something in it before executing your code in your online ide and you should not receive an exception anymore. But you should provide some sort of output that reflects your result wheather it is a palindrom :).

Barry
  • 337
  • 1
  • 2
  • 15