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评论105阅读模式
英文:

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

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

  1. try {
  2. File program = new File("file.txt");
  3. Scanner myReader = new Scanner(program);
  4. while (myReader.hasNextLine()) {
  5. int data = myReader.nextInt();
  6. System.out.println(data);
  7. }
  8. myReader.close();
  9. } catch (FileNotFoundException e) {
  10. System.out.println("文件未找到。");
  11. e.printStackTrace();
  12. }

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

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

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

英文:

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

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

Currently the output looks like this...

  1. 2
  2. //
  3. Two
  4. 3
  5. 40
  6. //
  7. oh
  8. look,
  9. forty

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

  1. 2
  2. 3
  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.

  1. try {
  2. File program = new File("file.txt");
  3. Scanner myReader = new Scanner(program);
  4. while (myReader.hasNextLine()) {
  5. String data = myReader.next();
  6. System.out.println(data);
  7. }
  8. myReader.close();
  9. } catch (FileNotFoundException e) {
  10. System.out.println("File not Found.");
  11. e.printStackTrace();
  12. }

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.

  1. try {
  2. BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
  3. String line;
  4. while((line = reader.()) != null)
  5. System.out.println(line);
  6. reader.close();
  7. } catch (IOException e) {
  8. System.out.println("File not Found.");
  9. e.printStackTrace();
  10. }

答案1

得分: 1

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

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

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

  1. try {
  2. File program = new File("file.txt");
  3. Scanner myReader = new Scanner(program);
  4. while (myReader.hasNextLine()) {
  5. Integer data = myReader.nextInt();
  6. myReader.nextLine(); // avoid reading 7 as nextInt. eg. "5 // seven 7"
  7. System.out.println(data);
  8. }
  9. myReader.close();
  10. } catch (FileNotFoundException e) {
  11. System.out.println("File not Found.");
  12. e.printStackTrace();
  13. }

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:

确定