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 配列を参照して、プロジェクトの最終ビルドから削除する予定であることに注意してください。String inputを使用して、ファイルを受け入れて書き込む予定です。

繰り返しますが、私が直面している問題は、ユーザーにフルネームを入力するように求めた後、ラインでキーボードからの入力を受け入れることです。プログラムはそれをスキップし、null 値を記録し、プログラムの残りの部分が (明らかに) 正しく動作しなくなります。

ユーザーの入力( http://answers.yahoo.com/question/index?qid=20090103175947AA5pyjq )をnextLine()待たず、改行文字の前にすべてを返しますが、代わりに BufferedReader を使用して入力を受け入れても、異なる変数名を使用した後でも、メインからスキャナーを閉じても、この問題を回避できないようです。

助けてください。

4

1 に答える 1