黑客扫描程序类Java

黑客扫描程序类Java,java,linked-list,java.util.scanner,nosuchelementexception,Java,Linked List,Java.util.scanner,Nosuchelementexception,此代码用于打印linkedlist并在Eclipse中正常工作:- import java.util.Scanner; public class HackerAss { static Scanner scan; static Node head; static Node current; public static void main(String[] args) { int q=1,count=0; while(q==1){

此代码用于打印linkedlist并在Eclipse中正常工作:-

import java.util.Scanner;

public class HackerAss {
    static Scanner scan;
    static Node head;
    static Node current;
    public static void main(String[] args) {
        int q=1,count=0;
        while(q==1){
            scan = new Scanner(System.in);
            q = scan.nextInt();
            if(q==0)
                break;
            else{
                int element = scan.nextInt();
                if(count == 0)
                    current = new Node(element);
                else{
                    head = new Node(element);
                    head.node = current;
                    current = new Node(element);
                }
            }

            count++;
            }
        while(head!=null){
            System.out.println(head.data);
            head=head.node;
        }

        }


    static class Node{
        int data;
        Node node;
        public Node() {
            data=0;
            node=null;
        }
        public Node(int data) {
            this.data=data;
            node=null;
        }

    }}
但在HackerRank中,它显示了错误:-

线程“main”java.util.NoSuchElementException中出现异常


错误的原因和解决方法是什么。我有作业要提交。

此错误来自nextInt()。您必须使用代码中的以下条件来避免这种情况

import java.util.Scanner;

public class HackerAss {
    static Scanner scan;
    static Node head;
    static Node current;
    public static void main(String[] args) {
        int q=1,count=0;
        scan = new Scanner(System.in);
        while(q==1){

            if(scan.hasNextInt() ) {
              q = scan.nextInt();
            }
            if(q==0)
                break;
            else{
                if(scan.hasNextInt() ) {
                  int element = scan.nextInt();
                }
                if(count == 0)
                    current = new Node(element);
                else{
                    head = new Node(element);
                    head.node = current;
                    current = new Node(element);
                }
            }

            count++;
            }
        while(head!=null){
            System.out.println(head.data);
            head=head.node;
        }

        }


    static class Node{
        int data;
        Node node;
        public Node() {
            data=0;
            node=null;
        }
        public Node(int data) {
            this.data=data;
            node=null;
        }

    }}

此错误源于nextInt()。您必须使用代码中的以下条件来避免这种情况

import java.util.Scanner;

public class HackerAss {
    static Scanner scan;
    static Node head;
    static Node current;
    public static void main(String[] args) {
        int q=1,count=0;
        scan = new Scanner(System.in);
        while(q==1){

            if(scan.hasNextInt() ) {
              q = scan.nextInt();
            }
            if(q==0)
                break;
            else{
                if(scan.hasNextInt() ) {
                  int element = scan.nextInt();
                }
                if(count == 0)
                    current = new Node(element);
                else{
                    head = new Node(element);
                    head.node = current;
                    current = new Node(element);
                }
            }

            count++;
            }
        while(head!=null){
            System.out.println(head.data);
            head=head.node;
        }

        }


    static class Node{
        int data;
        Node node;
        public Node() {
            data=0;
            node=null;
        }
        public Node(int data) {
            this.data=data;
            node=null;
        }

    }}

您的问题来自代码,而不是IDE

首先,您应该定义一次扫描仪:

while(q==1){
    scan = new Scanner(System.in);
    q = scan.nextInt();  
    ...  
}
这是一个非常糟糕的主意,因为你甚至不知道流中是否有东西,你必须使用这样的扫描仪:

scan = new Scanner(System.in);
while(scan.hasNextInt()){
    q = scan.nextInt();
    ...
}
之后,
java.util.NoSuchElementException
来自两次调用nextInt()的事实:


在文档
nextInt()

首先,您应该定义一次扫描仪:

while(q==1){
    scan = new Scanner(System.in);
    q = scan.nextInt();  
    ...  
}
这是一个非常糟糕的主意,因为你甚至不知道流中是否有东西,你必须使用这样的扫描仪:

scan = new Scanner(System.in);
while(scan.hasNextInt()){
    q = scan.nextInt();
    ...
}
之后,
java.util.NoSuchElementException
来自两次调用nextInt()的事实:

在文档
nextInt()
应从
移出,而
LOOP也
扫描=新扫描仪(System.in)应在循环时从
移出