Resetting a .nextLine() Scanner

后端 未结 5 453
生来不讨喜
生来不讨喜 2020-12-03 17:42

I am a rank amateur when it comes to Java, so please pardon my question if it seems dumb :-P I have the following code which is designed to count the number of lines in a f

相关标签:
5条回答
  • 2020-12-03 18:09

    You will have to redeclare the Scanner. When you invoke nextLine(), the line is removed from the buffer and effectively discarded from the Scanner.

    So, essentially, there is a method to do that: it's the constructor.

    Scanner scanNumOfLines = new Scanner(myFile);
    

    There is no "counter" in the Scanner object. Instead think of it as more like a conveyor belt. The belt has no knowledge or care about what's on it. It just keeps spitting things out at you while there are items left on it. And once you take them, they're gone from it for good.

    0 讨论(0)
  • 2020-12-03 18:15
    File file = new File("StoreData.txt");
    
    Scanner reader = new Scanner(new FileInputStream(file));
    while (reader.hasNext()) {
            k++;
            reader.nextLine();
    }
    reader.close();
    reader=null;    
    //reset scanner         
    reader=new Scanner(new FileInputStream(file));
    while (reader.hasNext()) {
        System.out.println(reader.nextLine());              
    }
    
    0 讨论(0)
  • 2020-12-03 18:18

    This is impossible to do.

    The reason to not include it, is the wide range of input types it supports. One example is streams. These don't store the results after they have been passed on, so they don't support resetting.

    So the elegant way is to create a new Scanner. If you give it many custom settings, create a factory method.

    0 讨论(0)
  • 2020-12-03 18:19

    I realize this has already been answered but if I'm looking up this stuff I'm sure someone else is too, so I thought I'd contribute the way I went about solving this problem: As my problem required me to read a file a number of times based on user input, my test class has an ArrayList of scanners that reads the file, then deletes itself once it's no longer needed. The way I have it set up there's never more than 1 scanner in the ArrayList, but ArrayList is useful in adding and removing objects.

    public class TxtReader {
         private boolean PROGRAM_CONTINUES = true;
         private final int indexCount = 0;
         File newFile = new File("Reader Example.txt");
         ArrayList<Scanner> scanList = new ArrayList<Scanner>();
    
         public TxtReader(Scanner UserInput) throws FileNotFoundException {
             while(PROGRAM_CONTINUES) {
                  if (UserInput.next().equalsIgnoreCase("end")) { 
              // some arbitrary way of concluding the while loop
                      PROGRAM_CONTINUES = false;
                      break;
                  }
                  scanList.add(new Scanner(newFile));
              // DO STUFF*********************************************
                  while(scanList.get(indexCount).hasNext()) {
                     System.out.println(scanList.get(indexCount).nextLine());
                 }
              //******************************************************
                  scanList.get(indexCount).close(); 
              //always close a scanner after you're done using it
                  scanList.remove(indexCount); // removes the now-unnecessary scanner 
                  UserInput = new Scanner(System.in);
            }
        }
        public static void main(String[] args) throws FileNotFoundException {
            new TxtReader(new Scanner(System.in));
        }
    }
    
    0 讨论(0)
  • 2020-12-03 18:30

    You could use a RandomAccessFile and use the method seek() to come back to first line.

    0 讨论(0)
提交回复
热议问题