I'm having great trouble with accepting input from the keyboard after transferring control to another method located in another class in my program. The error I get is:
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)
The following is my code in my main. I've not stripped it down so as to allow you to see the multiple ways I've tried before posting this question, up to the point where the problem exists.
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;
}
}
This is the class containing the method that was called from 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;
}
Note that, in reference to the Fname & Lname String array, I plan to remove it from the final build of the project. I plan to use the String input
to accept and write things to the file.
Once more, the problem I face is accepting input from the keyboard in the line after prompting the user to enter a full name. The program skips over it, recording a null value, and causing the rest of my program to not work properly (clearly).
I understand that nextLine()
does not wait for the user's input (http://answers.yahoo.com/question/index?qid=20090103175947AA5pyjq) , but returns everything before the newline character, but even with using BufferedReader to accept input instead, even after closing the Scanner from main, even after using different variable names, I can't seem to evade this problem.
Please help.