Pass A Text File to Scanner, Then Read It, in Java

428
0
SHARE

The Scanner is super useful for taking user inputs. However, you might want to pass a text file into it.

I’m going to show you how!

There are some important things…

  • Import
    • Scanner
    • File IO
    • File not found exception
  • Try/Catch statements around Scanner using file
    • File might not exist and break everything so …
      • TRY IT, then
      • if not exist, CATCH THE ERROR
  • WHILE LOOP
    • Only do next thing if it exists
    • Otherwise, you get null pointer exception

Until next time, Josh.