1

在将控制权转移到程序中另一个类中的另一个方法后,我在接受来自键盘的输入时遇到了很大的麻烦。我得到的错误是:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at com.group.work.fileReadWriter.WritetoFile(fileReadWriter.java:23)
    at com.group.work.highCipher.main(highCipher.java:32)

以下是我的主要代码。我没有将其剥离,以便让您在发布此问题之前看到我尝试过的多种方式,直到问题存在的地步。

package com.group.work;

import java.io.File;
import java.util.Scanner;

/*
import fileDecrypter;
import fileEncrypter;
import fileReadWriter;
*/

public class highCipher {


public static void main(String[] args) {

fileReadWriter obj = new fileReadWriter();
int choice;
Scanner scan = new Scanner(System.in);
//fileEncrypter objd = null;

    System.out.println("What Would You Like To Do ? \n 1 - Write To File \n 2 - Read From File \n ?:");
    //String choic = scan.next();
    choice = scan.nextInt();
    //scan.nextLine();
    scan.close();
    //choice = Integer.parseInt(choic);
    switch (choice)
    {
        case 1:
        {
            obj.WritetoFile();
            break;
        }

        case 2:
        {   
            obj.ReadfromFile();
            break;
        }

        default:
        {
            System.out.println("Incorrect Value Entered");
            break;
        }
    }

这是包含从中调用的方法的类obj.WritetoFile()

package com.group.work;

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

public class fileReadWriter {
    private String input = null;
    //String Fname[]=new String[2]; 
    //String Lname[]= new String[2];
    private Scanner in = new Scanner(System.in);

    public void WritetoFile (){
    //  InputStreamReader reader = new InputStreamReader(System.in);
        //BufferedReader in = new BufferedReader(reader);

    try{

        PrintStream output = new PrintStream(new File("output.txt"));
    {
        System.out.println("please enter a full name");
        //input = in.readLine(); /* I will get errors here,
        //input = in.next();        here,
        input = in.nextLine();      here,
        /*
        for(int x=0; x<Fname.length; x++){
         Fname[x] = in.next();       and here. */

         Lname[x] = in.next();
         }

        */
    //for(int i =0;i<Fname.length;i++){
      //  String sc =" ";

        //output.println( Fname[i] +sc+ Lname[i] );
        output.println(input);
        }
        output.close();

        System.out.println ("File created successfully");

    }
    catch (IOException e) {
        System.err.println("Error: " + e.getMessage());
        }

    return;
    }   

请注意,关于 Fname 和 Lname 字符串数组,我计划将其从项目的最终构建中删除。我打算使用String input来接受并将内容写入文件。

再一次,我面临的问题是在提示用户输入全名后接受来自键盘的输入。程序跳过它,记录一个空值,并导致我的程序的其余部分无法正常工作(很明显)。

我知道nextLine()不等待用户的输入(http://answers.yahoo.com/question/index?qid=20090103175947AA5pyjq),而是返回换行符之前的所有内容,但即使使用 BufferedReader 来接受输入,即使在之后从 main 中关闭 Scanner,即使在使用不同的变量名之后,我似乎也无法回避这个问题。

请帮忙。

4

1 回答 1

1
changes to make it work

comment out:
scan.close in main 

and uncomment:

input = in.readLine()
-----------------------------------------------------------------------------------
main file
=====================
package simlpe;
import java.io.File;
import java.util.Scanner;

/*
import fileDecrypter;
import fileEncrypter;
import fileReadWriter;
*/

public class simple {


public static void main(String[] args) {

fileReadWriter obj = new fileReadWriter();
int choice;
Scanner scan = new Scanner(System.in);
//fileEncrypter objd = null;

    System.out.println("What Would You Like To Do ? \n 1 - Write To File \n 2 - Read From File \n ?:");
    //String choic = scan.next();
    choice = scan.nextInt();
    //scan.nextLine();
  //  scan.close();
    //choice = Integer.parseInt(choic);
    switch (choice)
    {
        case 1:
        {
            obj.WritetoFile();
            break;
        }

        case 2:
        {   
          obj.ReadfromFile();
            break;
        }

        default:
        {
            System.out.println("Incorrect Value Entered");
            break;
        }
    }
}
}
other file
============
package simlpe;

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

public class fileReadWriter {
    private String input = null;
    String Fname[]=new String[2]; 
    String Lname[]= new String[2];
    private Scanner in = new Scanner(System.in);

    public void WritetoFile (){
      InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(reader);

    try{

        PrintStream output = new PrintStream(new File("output.txt"));
    {
        System.out.println("please enter a full name");
        input = in.readLine();// I will get errors here,
        //input = in.next();      //  here,
      //  input = in.nextLine();     // here,

        for(int x=0; x<Fname.length; x++){
       //  Fname[x] = in.next();

       //  Lname[x] = in.next();
         }


    //for(int i =0;i<Fname.length;i++){
      //  String sc =" ";

        //output.println( Fname[i] +sc+ Lname[i] );
        output.println(input);
        }
        output.close();

        System.out.println ("File created successfully");

    }
    catch (IOException e) {
        System.err.println("Error: " + e.getMessage());
        }

    return;
    }   
}
于 2013-09-30T18:21:04.227 回答