5

リンク リストから特定のノードを削除しようとしています。メソッド removeNode を呼び出そうとしていますが、ユーザー入力を取得するために呼び出すと、このエラーが発生します。これを修正する方法についてのアドバイスは大歓迎です!

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at LinkedList.removeNode(LinkedList.java:123)
    at fileIn.<init>(fileIn.java:22)
    at fileIn.main(fileIn.java:13)

LinkedList クラス:

import java.util.Scanner;

public class LinkedList {

    public LinkedListNode front;

    public LinkedList() {
        this.front = null;
    }

    public void insertBack(String data)
    {
        if(front == null){
            front = new LinkedListNode(data);
        }
        else{
            LinkedListNode newNode = new LinkedListNode(data);
            LinkedListNode current = front;

            while(current.getNext() != null){
                current = current.getNext();
            }
            current.setNext(newNode);
        }
    }//end insertBack

    public void addAfter(LinkedListNode spot, String data)
    {
        LinkedListNode newNode;

        newNode = new LinkedListNode(data);

        newNode.next = spot.next;
        spot.next = newNode;
    }//end addAfter

    public void addBefore(LinkedListNode spot, String data)
    {

    }//end addBefore

    public void deleteAfter(LinkedListNode spot)
    {
        LinkedListNode nextNode;

        nextNode = spot.next;
        spot.next = nextNode.next;
    }//end deleteAfter


    public String showList()
    {
        sortList();
        //^-- Will sort the sum but not the nodes

        int i = 0;
        String retStr = "The nodes in the list are:\n";
        LinkedListNode current = front;
        while(current != null){
            i++;
            retStr += "Node " + i + " is: " + current.getData() + " and the sum is: " + current.getSum() + "\n";
            current = current.getNext();
        }

        return retStr;
    }

    public LinkedListNode findTail()
    {
        LinkedListNode current = front;
        while(current.getNext() != null){
            current = current.getNext();
        }
        return current;
    }//end findTail

    public LinkedList sortList()
    {
        LinkedListNode current = front;
        LinkedListNode tail = null;

        while(current != null && tail != front )
        {
            LinkedListNode next = current;

            for( ; next.next != tail;  next = next.next)
            {
                if(next.sum >= next.next.sum)
                {
                    long temp = next.sum;
                    String temp2 = next.data;

                    next.sum = next.next.sum;
                    next.data = next.next.data;

                    next.next.sum = temp;
                    next.next.data = temp2;
                }
            }

            tail = next;
            current = front;
        }

        return this;
    }

    public void removeNode(){

    String searchedNode;

    Scanner in = new Scanner(System.in);
    System.out.println("Enter the name you would like to remove from the list: ");
    searchedNode = in.nextLine();
    in.close();
        LinkedListNode previous = null;
        LinkedListNode current = front;

            //while there is something in the list nodes
             while (current != null)
             {
                //if the data of current is equal to the node being searched
                if(current.data.equals(searchedNode))
                {
                  //set current to the node after current
                  current = current.next;
              //if previous is equal to null(which it is)
              if (previous == null)
                  {
                  //set previous equal to current
                  previous = current;
                  }

                  else previous.next = current;
                } else {
                  previous = current;
                  current = current.next;
            }
        } //end while
    }
}

ファイルクラス:

import java.util.Scanner;
import java.io.*;

public class fileIn
{
   LinkedListNode front;
   LinkedList myList = new LinkedList();
   String fname;

   public static void main(String[] args)
    {
       fileIn f = new fileIn();
   }//end main


   public fileIn()
   {
      getFileName();
      readFileContents();
      System.out.print(myList.showList());
      myList.removeNode();
   }//end fileIn

   public void readFileContents()
    {
        boolean looping;
        DataInputStream in;
        String line;

        /* Read input from file and process. */
        try
        {
            in = new DataInputStream(new FileInputStream(fname));

            looping = true;
            while(looping)
             {
                /* Get a line of input from the file. */
                if (null == (line = in.readLine()))
                {
                    looping = false;
                    /* Close and free up system resource. */
                    in.close();
                }//end if
                else
                {
                myList.insertBack(line);
                }//end else
            } /* End while. */
        } /* End try. */

        catch(IOException e)
        {
            System.out.println("Error " + e);
        } /* End catch. */
    }//end readFileContents

     public void getFileName()
     {
        Scanner in = new Scanner(System.in);

        System.out.println("Enter file name please.");
        fname = in.nextLine();
        in.close();
     }//end getFileName

}//end class fileIn
4

2 に答える 2

12

問題は、あなたが閉じていることですSystem.inScanner.close() 基になるストリームを閉じます)。これを行うと、閉じたままになり、入力できなくなります。通常、標準入力でこれを行うことは望ましくありません。

String searchedNode;
Scanner in = new Scanner(System.in);
System.out.println("Enter the name you would like to remove from the list: ");
searchedNode = in.nextLine();
// in.close(); // <-- don't close standard input!

また、今後の参考のために、より最小限のテスト ケースを作成するようにしてください。デバッグに役立ち、質問から多くのノイズを取り除くこともできます。:-)

于 2013-11-07T04:39:27.427 に答える
0

Javaでjava.util.NoSuchElementExceptionを解決する方法(および常識)に従って、各の前に適切な種類のメソッドを使用しhasNextますnext

あなたの特定のケースでは、次のようになります。

if (in.hasNextLine) {
    in.nextLine();
}
于 2013-11-07T04:42:08.190 に答える