Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+24 votes

Once a file runs out of tokens are you able to start getting tokens from another file?

asked in CSC211_Winter2018 by (1 point)

2 Answers

+13 votes

Yes, but it would involve creating another Scanner object that points to a new file to read from. You can use the same name for your scanner, but that might make your code more difficult to read. Here's an example:

// files and scanners sold separately
File a = new File("a.txt");
File b = new File("b.txt");
Scanner input = new Scanner(a);
// do something
// input = new Scanner(b); // -- legal, but not recommended
Scanner inputB = new Scanner(b);

answered by (1 point)
+9 votes

My thought right as I read the question right now would be to call for another file after the first while loop for the first file is over.

What I am saying is that to read the file you would

  1. Check you can read the file
  2. Continue to read the file in a loop (you could set it up so that it is an indefinite loop and continues until it reaches the end of the file)

When the loop is over it will move onto the next line of code. Perhaps you can set it up so it will read the 2nd file.

answered by (1 point)
...