在导入的类中调用方法后,使用Scanner接受来自键盘的输入

时间:2013-09-30 18:00:19

标签: methods keyboard java.util.scanner

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

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&amp; Lname String数组,我打算从项目的最终版本中删除它。我计划使用String input接受并写入文件。

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

我理解nextLine()不会等待用户输入(http://answers.yahoo.com/question/index?qid=20090103175947AA5pyjq),而是返回换行符之前的所有内容,但即使使用BufferedReader接受输入也是如此,即使在关闭扫描程序之后也是如此主要的,即使使用不同的变量名称,我似乎无法回避这个问题。

请帮忙。

1 个答案:

答案 0 :(得分: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;
    }   
}