简体   繁体   中英

How can I get my scanner to read the next line in the file?

I am printing out the contents of a txt file while skipping over any numbers that are in the file.

The file I am using looks like this:

one two 3 three four

five six 7 eight

I have tried using input2.next() or input2.nextline() after System.out.print(token), but I either get an error or it doesn't read the next line accurately.

import java.util.*;
import java.io.*;


public class ScannerClass {


public static void main(String[] args) throws FileNotFoundException {


    System.out.print("Enter file name and extension: ");

    Scanner input = new Scanner(System.in);

    File file = new File(input.next());

    Scanner input2 = new Scanner(file);


    String token;

//this will print out the first line in my txt file, I am having trouble
//reading the next line in the file!

    while ((token = input2.findInLine("[\\p{Alpha}\\p{javaWhitespace}]+")) != null) {
        System.out.print(token);


    }

  }
}

The output is:

one two three four

What I would like to see is the whole txt file less any numbers such as:

one two three four

five six eight

One main problem with your reg exp is that it matches only one part of the line first before the digit and then after the digit while the findInLine somehow advances the line counter.

So here is a different solution using your reg exp pattern but I have separated the reading from the file from the matching logic

Pattern p = java.util.regex.Pattern.compile("[\\p{Alpha}\\p{javaWhitespace}]+");
while (input2.hasNextLine()) {
    String line = input2.nextLine();
    Matcher m = p.matcher(line);
    while (m.find()) {
        System.out.print(m.group()); //Prints each found group
    }
    System.out.println();
}

You can add this RegEx;

import java.util.*;
import java.io.*;

public class ScannerClass {
    public static void main(String[] args) throws FileNotFoundException {
       System.out.print("Enter file name and extension: ");
       Scanner reader = new Scanner(System.in);
       reader = new Scanner(new File(reader.next()));

       Pattern p = Pattern.compile("[a-zA-Z_]+");
       Matcher m = null;

       while (reader.hasNext()){
          String nextLine = reader.nextLine();
          m = p.matcher(nextLine);
          while(m.find()) {
              System.out.printf("%s ",m.group());
          }
          System.out.println();
      }
   }
}

May not be the most optimum but it'll work. Each loop iteration will split the current line into an array of strings delimited by a number ( \\\\d+ ), then each array element (the alphabet words and whitespace in this case) will be streamed and joined into a single string.

while (input2.hasNextLine()) {
    String[] nonNumbers = input2.nextLine().split("\\d+");
    System.out.println(Arrays.stream(nonNumbers).collect(Collectors.joining()));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM