如何使用扫描仪浏览文件并取出第一行两行,只留下单词字符串而不是数字


2
3
1 2 3 4 5 5
3 4 5 6 7 5
4 5 1 2 3 5 6 7 8 9
2
10 11 15 61 42 14 112 34
12 13 11 61 42 10 34 12 14 112

我目前有这个,该文件适用于上面的集合。

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    Scanner scan = new Scanner(new File("numbers.txt"));
    int totalSets = scan.nextInt();
    int nest = scan.nextInt();
    scan.nextLine();
    while(scan.hasNextLine() && totalSets-->=0) {
    Set<String> lines = new HashSet<String>();
    lines.add(scan.nextLine());
    System.out.println(lines);}

    while(scan.hasNextLine() && nest-->=1) {
        Set<String> lines = new HashSet<String>();
        lines.add(scan.nextLine());
        System.out.println(lines);}

所以在我的代码中,它一直在集合中返回 2。我想知道如果有人可以帮助我弄清楚,为什么会发生这种情况。

我认为您正在寻找此代码.

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        String n = scanner.nextLine();
        for(int i =0;i<Integer.parseInt(n);i++){
            String a= scanner.nextLine();
            for(int z =0; z < Integer.parseInt(a); z++){
                System.out.println(scanner.nextLine());
            }
        }
    }
}

最新更新