Read the first number in a line, move on to next line without reading the rest of the line until end of file

huangapple go评论68阅读模式
英文:

Read the first number in a line, move on to next line without reading the rest of the line until end of file

问题

我正在从一个txt文件中读取内容。它包含如下内容...

2 // 两
3
40 // 哦,看,四十

目前输出看起来是这样的...

2
//

3
40
//

看,
四十

我不想要那样。我只想要整数,所以输出应该是...

2
3
40

我想要只读取每行开头的整数,忽略掉行的其余部分,然后移到下一行。我会一直这样做,直到达到文件的末尾。最终,所有这些整数将放入一个数组中。

try {
    File program = new File("file.txt");
    Scanner myReader = new Scanner(program);
    while (myReader.hasNextLine()) {
        int data = myReader.nextInt();
        System.out.println(data);
    }
    myReader.close();
} catch (FileNotFoundException e) {
    System.out.println("文件未找到。");
    e.printStackTrace();
}

我尝试过使用BufferedReader,但我认为它不能实现我想要的功能,所以我选择了Scanner。如果有关这里的尝试,请看下面的代码。

try {
    BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
    String line;
    while((line = reader.readLine()) != null) {
        // 将每行的内容拆分成单词,然后获取第一个单词作为整数
        String[] words = line.split(" ");
        if (words.length > 0) {
            int data = Integer.parseInt(words[0]);
            System.out.println(data);
        }
    }
    reader.close();
} catch (IOException e) {
    System.out.println("文件未找到。");
    e.printStackTrace();
}

这两段代码可以帮助你只读取每行开头的整数并将其打印出来。

英文:

I am reading from a txt file. It has stuff in it like...

2 // Two
3
40 // oh look, forty

Currently the output looks like this...

2
//
Two
3
40
//
oh
look,
forty

I don't want that. I just want the integers, so the output should be...

2
3
40

I want to read just the integer at the beginning of the line, ignore the rest of the line then move to the next line. I will keep doing this until I reach the end of the file. Ultimately all these integers will go into an array.

	try {
		File program = new File("file.txt");
		Scanner myReader = new Scanner(program);
		while (myReader.hasNextLine()) {
			String data = myReader.next();
			System.out.println(data);
			
		}
		myReader.close();
	} catch (FileNotFoundException e) {
		System.out.println("File not Found.");
		e.printStackTrace();
	}

I have tried BufferedReader but I think it can't do what I want it to do, so I went with a Scanner. If it matters here is my attempt with that.

	try {
		BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
		String line;
		while((line = reader.()) != null)
			System.out.println(line);
		reader.close();
	} catch (IOException e) {
		System.out.println("File not Found.");
		e.printStackTrace();
	} 

答案1

得分: 1

只考虑你想要在文本行中找到的第一个数字,我将使用 Scanner.nextInt

try {
    File program = new File("file.txt");
    Scanner myReader = new Scanner(program);
    while (myReader.hasNextLine()) {
        Integer data = myReader.nextInt();
        myReader.nextLine(); // 避免将7作为nextInt读取,例如:"5 // seven 7"
        System.out.println(data);
    }
    myReader.close();
} catch (FileNotFoundException e) {
    System.out.println("文件未找到。");
    e.printStackTrace();
}
英文:

Considering you want only the first number found in a line of text, I'll use Scanner.nextInt.

try {
    File program = new File("file.txt");
    Scanner myReader = new Scanner(program);
    while (myReader.hasNextLine()) {
        Integer data = myReader.nextInt();
        myReader.nextLine(); // avoid reading 7 as nextInt. eg. "5 // seven 7"
        System.out.println(data);
    }
    myReader.close();
} catch (FileNotFoundException e) {
    System.out.println("File not Found.");
    e.printStackTrace();
}

huangapple
  • 本文由 发表于 2023年2月16日 04:05:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定