简体   繁体   English

从文本文件中读取索引和字符串 Java

[英]Read index and strings from text file Java

Suppose i have a text file that has a number code and a string label on each line:假设我有一个文本文件,每行都有一个数字代码和一个字符串 label:

0102 Apple Banana Code
0203 Pear Watch Time
0405 Java Computer Pen
0607 Car Bike Drive

How could i print the sum of the non zero numbers in the front and the first two words of each line?我如何打印前面非零数字的总和以及每行的前两个单词? Assuming that the numbers are always 4 digits and words are separated by a single space from the number and in between the words.假设数字始终是 4 位数字,并且单词与数字和单词之间由一个空格分隔。 For example I would get:例如我会得到:

3 Apple Banana 
5 Pear Watch 
9 Java Computer 
13 Car Bike 

I have tried to initialize this but i have no idea where to go from here我试图初始化它,但我不知道从这里到哪里去 go

FileReader fr = new FileReader(fileName);
BufferedReader inFile = new BufferedReader(fr);
String[][] words = new String[100][];
String line;
int size = 0;
while((line = inFile.readLine()) != null) {
    words[size++] = line.split(" ");
}
inFile.close();

You are close.你很接近。 Refer to below code.参考下面的代码。 Explanations appear after the code.解释出现在代码之后。

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Tester {
    private static void handleLine(String line) {
        String[] parts = line.split(" ");
        int num = handleNumber(parts[0]);
        System.out.printf("%d %s %s%n", num, parts[1], parts[2]);
    }

    private static int handleNumber(String number) {
        char[] digits = number.toCharArray();
        int total = 0;
        for (char digit : digits) {
            total += digit - '0';
        }
        return total;
    }

    public static void main(String[] args) {
        Path path = Path.of("tragedy.txt");
        try (BufferedReader inFile = Files.newBufferedReader(path)) {
            String line = inFile.readLine();
            while (line != null) {
                handleLine(line);
                line = inFile.readLine();
            }
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
    }
}
  • I use class Files to create a BufferedReader but it is not required.我使用 class Files创建BufferedReader但这不是必需的。 Your code for obtaining a BufferedReader is correct.您获取BufferedReader的代码是正确的。
  • I also use try-with-resources to ensure that the file is closed.我还使用try-with-resources来确保文件已关闭。
  • You asked how to print the file lines after manipulating them slightly so that is what the above code does.您询问了如何在稍微操作文件行后打印它们,这就是上面代码的作用。
  • char is actually a number so to get the actual number value from the char , I subtract the value of the 0 character. char实际上是一个数字,因此要从char中获取实际数值,我减去0字符的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java tokenize从文本文件中读取的字符串 - java tokenize strings read from a text file 如何从文本文件中的 Java 中的字符串数组打印一个元素(索引)? - How to print one element (index) from Array of Strings in Java from a text file? 如何从java中的文件中读取字符串和数字? - How to read strings and numbers from file in java? Java使用来自不同语言的字符串读取文件 - Java read file with strings from different languages JAVA 从网络服务器读取文件和多个字符串 - JAVA Read file from webserver and multiple strings 从文本文件中读取Java - Java read from a text file 在从文本文件读取字符串行之前,在Java中使用BufferedReader读取一行int - Using BufferedReader in java to read a line of ints before reading lines of Strings from a text file java - 如何在java编程中从包含整数和字符串的文本文件中读取整数? - How do I read integers from a text file containing integers and strings in java programming? Java如何从具有多个字符串和双精度值的文本文件中读取一行? - Java how to read a line from a text file that has multiple strings and double values? 用Java编写程序以从用户读取多个字符串并与文本文件进行比较 - Writing a program in Java to read in multiple strings from user and compare to text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM