-1
import java.io.IOException;
import java.io.File;
import java.nio.file.Path;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.NoSuchElementException;
public class filehandling{
    public static void main(String[] args){
        Scanner x = new Scanner(System.in);
        while(true){
            System.out.println("1. write 2. read 3. delete 4. create 5.exit");
            System.out.print("Enter choice: ");
            int ch = x.nextInt();
            if(ch==1){
                writer1 var0 = new writer1();
                var0.function1();
            }
            if(ch==2){
                reader1 var0 = new reader1();
                var0.function2();
            }
            if(ch==3){
                delete1 var0 = new delete1();
                var0.function4();
            }
            if(ch==4){
                create1 var0 = new create1();
                var0.function3();
            }
            if(ch==5){
                System.out.println("exited, thank you for using program");
                x.close();
                break;
            }
        }
    }
}
class writer1{
    void function1(){
            Scanner y = new Scanner(System.in);
            System.out.print("Input file name: ");
            String path = y.nextLine();

            File file = new File("D:\\"+path);

            System.out.print("input number of lines: ");
            Scanner a = new Scanner(System.in);
            int n = a.nextInt();
            
            Scanner z = new Scanner(System.in);
            System.out.print("input data: ");
            String data = z.nextLine();
            
            FileWriter fr = null;
            BufferedWriter br = null;
            String datawithnewline = data+System.getProperty("line.separator");
            System.out.println(datawithnewline);
            try {
                for(int i = n; i>0;i--){
                    try {
                        fr = new FileWriter(file);
                        br = new BufferedWriter(fr);
                        br.write(datawithnewline);
                    } catch (NoSuchElementException e) {
                        System.out.println("DONE ");
                    }
                    
                }
            } catch (IOException e) {
                System.out.print("error");
            }
            finally{
                try{
                    br.close();
                    fr.close();
                    y.close();
                    z.close();
                    a.close();
                }
                catch(IOException e){
                    System.out.print("Error 2");
                }
            }
        }
    }
class reader1{
    void function2(){
        Scanner y = new Scanner(System.in);
        System.out.print("Input file name: ");
        String path = y.nextLine();
        File file = new File("C:\\Users\\subra\\AppData\\Local\\Temp\\vscodesws_32946\\jdt_ws\\jdt.ls-java-project\\src"+path);
        if(file.canRead()){
            FileReader fr = null;
            BufferedReader br = null;
            try{
                fr = new FileReader(path);
                br = new BufferedReader(fr);
                int var = 0;
                while(( var=br.read())!= -1){
                    char text = (char) var;
                    System.out.print(text);
                }
            }
            catch(IOException e){
                System.out.println("Error");
            }
            finally{
                y.close();
                if (fr !=null){
                    try{
                        fr.close();
                    }
                    catch(IOException e){
                        System.out.println("Error");
                    }
                }
                if(br!=null){
                    try{
                        br.close();
                    }
                    catch(IOException e){
                        System.out.println("Error");
                    }
                }
            }
        }
    }
}
class create1{
    public void function3(){
        Scanner var1 = new Scanner(System.in);
        System.out.print("Input file name: ");
        String var2 = var1.nextLine();
        File file = new File("D:\\"+var2);
        try {
            boolean createNewfile = file.createNewFile();
            System.out.println("File created: "+createNewfile);
        } catch (IOException e) {
            System.out.println("Error");
            var1.close();
        }
    }
}
class delete1{
    public void function4(){
        Scanner y = new Scanner(System.in);
        System.out.print("Input file name");
        String path = y.nextLine();
        Path path1 = Path.of(path);
        String path2 = path1.toString();
        File file = new File(path2);
        if(file.canRead()){

            boolean delete = file.delete();
            System.out.println("DELETED FILE: "+delete);
        }
        y.close();

    }
}

every time I run this program, it always returns this error, I am actually studying file handling in java so I used this website, I am using visual studio code, I have tried putting br.write(...) part in a try and catch block inside the for loop in writer1 class, the total interaction in the terminal is

PS C:\Users\subra\AppData\Local\Temp\vscodesws_32946\jdt_ws\jdt.ls-java-project\src>  c:; cd 'c:\Users\subra\AppData\Local\Temp\vscodesws_32946\jdt_ws\jdt.ls-java-project\src'; & 'c:\Users\subra\.vscode\extensions\vscjava.vscode-java-debug-0.36.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-16.0.2\bin\java.exe' '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:50835' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\subra\AppData\Roaming\Code\User\workspaceStorage\3543e469db802eccea9e87de0109e000\redhat.java\jdt_ws\src_c37eea88\bin' 'filehandling' 
1. write 2. read 3. delete 4. create 5.exit
Enter choice: 1
Input file name: hi
input number of lines: 5
input data: i love coding
i love coding

1. write 2. read 3. delete 4. create 5.exit
Enter choice: Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at filehandling.main(filehandling.java:16)

what should I do??

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Closing a scanner object also closes the stream, meaning all other scanners used will fail. Try only using one scanner across all your classes/methods. Also read about a [mcve] – OneCricketeer Oct 17 '21 at 13:54
  • Does this answer your question? [Scanner error with nextInt()](https://stackoverflow.com/questions/12832006/scanner-error-with-nextint) – Oussama ZAGHDOUD Oct 17 '21 at 14:00
  • I know that you already posted this question. You obviously deleted and re-posted it. Nonetheless, it appears that you have learned how to improve the quality of your questions. I would like to believe that is partially due to the comments I posted in the question that you deleted. – Abra Oct 17 '21 at 14:15
  • Yes, indeed, I thought my whole code was too big to post, but later I realized – Subramani Easwaran Oct 17 '21 at 14:36

1 Answers1

0

You only should be using one Scanner across all your classes, and only closing it once

Using an example from only one of your classes (classes should be capitalized, and generally don't use numbers)

public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    Reader reader = new Reader(sc);  // your class. Don't close the Scanner in it
    int ch = -1;
    while (ch !=5) {
        ch = sc.nextInt();
        if (ch == 2) {
            reader.read();
        }
    }
    sc.close(); // only done once
}

Then update the classes to add a constructor

class Reader {
    private Scanner sc;
    public Reader(Scanner scanner) {
        this.sc = scanner;
    }

    public void read() {
        String filepath = sc.readLine();
       ... 
    } 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245