2013-09-30 27 views
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接受輸入代替,甚至從主要關閉掃描儀,即使使用不同的變量後後名字,我似乎無法迴避這個問題。

請幫忙。

回答

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; 
    } 
} 
+0

哇。你能向我解釋爲什麼和這是如何工作的? 此外,我必須使用'掃描儀'接受來自鍵盤的輸入?除了'BufferedReader'之外,沒有別的辦法嗎? 我真的很喜歡java,除了這個非常小的部分。我希望它像'cin >>變量一樣簡單。即使'scanf(「%d」);'也不是那麼糟糕。嘆。 – theonlyross

+0

其實你的問題有其答案。掃描儀和緩衝讀取器是2種不同的方法來讀取input.to看到他們之間的差異看到:http://stackoverflow.com/questions/2231369/scanner-vs-bufferedreader .so回答你的問題.1)不需要使用任何掃描儀你可以使用緩衝閱讀器。2)掃描儀是另一種選擇,除了緩衝式閱讀器 –

+0

和java很容易,以及一個簡單的例子System.out.println(「enter name」);掃描儀掃描=新掃描儀(System.in); String s = scan.nextLine(); –