线程"main"中的异常java.util.Scanner.throwFor(Scanner.java:862)上的java.util.NoSuchElementException [英] Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862)

查看:248
本文介绍了线程"main"中的异常java.util.Scanner.throwFor(Scanner.java:862)上的java.util.NoSuchElementException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不知道可能是什么问题.这是我得到的错误:

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)

$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)

这是代码:

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())

代码可以写为:

    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();
    }
    }

这篇关于线程"main"中的异常java.util.Scanner.throwFor(Scanner.java:862)上的java.util.NoSuchElementException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆