Hey there! any question in your mind? Ask It Now!.

Popular Categories





Error: java.util.NoSuchElementException

+2 votes
52 views
asked in Programming by mHelpMe (670 points)

i am getting this error my code is this

public class JavaHW1_1 {
private static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) throws IOException {

    String pattern ;
    String fileName = null;



    //      Method to manage user inputs 
    fileName = userInputFileName(userInput);
    pattern = userInputPattern(userInput);

    //      To find the pattern in the file
    //      findPattern();

}
private static String userInputPattern(Scanner userInput) {
    String pattern = "JollyGood";
    System.out.println(". Please enter a pattern to find in the file");

    while(userInput.hasNextLine()){
        pattern = userInput.nextLine();
        System.out.println("The pattern to be searched: "+ pattern);
    }
    userInput.close();
    return pattern;
}
private static String userInputFileName(Scanner userInput) throws IOException {
    String path = "./src";
    String files, fileName;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();

    System.out.println("Please input the desired file name:\n");
    System.out.println("Some suggestions:\n");
     for (int i = 0; i < listOfFiles.length; i++) 
      {
if (listOfFiles[i].isFile()&&listOfFiles[i].getName().toLowerCase().endsWith(".txt")) 
       {

       files = listOfFiles[i].getName();
       System.out.println(files);
          }
      }

     int userAttempt = 0;

     do{
     fileName = userInput.nextLine();

     if(fileName.toLowerCase().endsWith(".txt")){
         System.out.println("The file name entered is in correct format");
         File file = new File("./src",fileName);

         try {
file.createNewFile();
System.out.println("File is created. Please enter text to be written in the file.
 End the content with \"eof\"");
            InputOutput(file.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }

         userAttempt = 10;
     }
else
{System.out.println("Please enter correct format file with .txt extension");
         userAttempt++;}
     }while (userAttempt <10);

    return fileName;
}
private static void InputOutput(String fName) throws IOException {

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new FileWriter("./src/" + fName));
        String inputLine = null;
        do {
            inputLine=in.readLine();
            out.write(inputLine);
            out.newLine();
        } while (!inputLine.equalsIgnoreCase("aaa"));
        System.out.print("Write Successful");
    } catch(IOException e1) {
        System.out.println("Error during reading/writing");
    } finally {
        out.close();
        in.close();
    }

}
private static void findPattern() {
    // TODO Auto-generated method stub

}
}

1 Answer

+2 votes
answered by sunny (360 points)

I think you should receive the Scanner by parameter, try like this :-

public static void main(String[] args){

    Scanner scan = new Scanner (System.in);
    String pattern = userInputPattern(scan);
    String test = readSomethingElse(scan);
}

private static String readSomethingElse(Scanner scan) {
   System.out.println(". Read something else");
    return scan.nextLine();
}

private static String userInputPattern(Scanner scan) {

    String pattern = "JollyGood";
    System.out.println(". Please enter a pattern to find in the file");
    pattern = scan.nextLine();
    System.out.println("The pattern to be searched: "+ pattern);
    return pattern;
}

Related Questions

+2 votes
1 answer 72 views
+1 vote
1 answer 86 views
0 votes
0 answers 87 views
0 votes
0 answers 128 views
0 votes
1 answer 62 views
+3 votes
1 answer 614 views

Not a Member yet?

Ask to Folks Login

My Account
341 Folks are online
27 members and 314 guest online
Your feedback is highly appreciated