0

I have a text file that contains many similar lines to this one

System.out.println(13);

I would like to insert this into my Java code so that when I run the program, it will output 13.

Here is the program where it would just output System.out.println(13); instead of 13:

File sampleCode = new File("sample.txt");
try {
    Scanner scan = new Scanner(sampleCode);
    while(scan.hasNextLine()){
        System.out.println(scan.nextLine()); //this is where i plan to insert the lines from the .txt file
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Is it possible?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
walbag
  • 35
  • 4
  • 1
    If you're trying to compile-and-execute arbitrary Java source code, that may or may not compile with `javac`, then probably the easiest solution is to use the [JShell API](https://docs.oracle.com/en/java/javase/19/docs/api/jdk.jshell/module-summary.html). – Slaw Oct 16 '22 at 04:00
  • 1
    What are you trying to achieve? – tgdavies Oct 16 '22 at 04:30

0 Answers0