首页 > 解决方案 > 大批- 获取单词在行号上的字符位置

问题描述

我有一个 arrayList ,它保存了我读入的文件的内容。我只需要帮助弄清楚让它在每一行显示该行上每个单词的位置。(稍后我将编写一个忽略 // 和其后所有内容的函数。

所需的输出:第 1 行,字符/位置 1,程序第 1 行,位置 2,二十:第 2 行,位置 3,int 等等..

当前输出读取:行号:0 行读取://此程序应打印数字 20。行号:1 行读取:程序二十:行号:2 行读取:int a; 行号:3行读取:int b;

代码:

public class Lex_functions {

    ArrayList < String > list = new ArrayList < String > ();
    int lineNum = 0;
    int charNum = 0;
    int totalLines = 0;

    void readFile() throws FileNotFoundException {
        File f = new File("ab.txt");
        FileReader fr = null;
        try {
            fr = new FileReader(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
        BufferedReader infile = new BufferedReader(fr);
        String line = "";
        boolean done = false;
        try {
            while (!done) {
                line = infile.readLine();
                if (line == null) {
                    done = true;
                } else {
                    list.add(line);
                }
            }
            infile.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        for (String item: list) {
            //System.out.println(item);

            while (lineNum <= (list.size() - 1)) {

                System.out.println("Line Number: " + lineNum + " " + "line reads: " + " " + list.get(lineNum));
                //  System.out.println(list.size());
                lineNum++;
                charNum++;

                //System.out.println();

            }

            // System.out.println(list.indexOf(4));
            //System.out.println(list[0]);

            // System.out.println("Index of Program: "+list.get(2));
        } //end readfelil 
    }


}

标签: java

解决方案


因此,我不确定您要做什么,因为您显示所需输出的方式不可读,并且您没有向我们提供要显示的文件内容。但如果我理解得很好,如果你有以下数组:

{“你好世界”},{“祝你有美好的一天”}

您要显示的内容应该是:

  • 字:你好,行号:0,字号0
  • 字:世界,行号:0,字号1
  • 字:有,行号:1,字号0
  • 字:a,行号:1,字号1
  • 字:好,行号:1,字号2
  • 字:天,行号:1,字号3

因此,首先有一种简单的方法可以将文件的所有行读入 Array List :

/**
     * Read a file and put all its content into a String List
     * @param fileName file path
     * @return
     */
    public static ArrayList<String> readFileInList(String fileName){
        List<String> lines = Collections.emptyList();
        try {
            lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
        } catch (IOException e) {
            // do something
            e.printStackTrace();
        }
        return (ArrayList<String>) lines;
    }

接下来是你循环的方式......

for (String item: list) {
            //System.out.println(item);

            while (lineNum <= (list.size() - 1)) {

                System.out.println("Line Number: " + lineNum + " " + "line reads: " + " " + list.get(lineNum));
                //  System.out.println(list.size());
                lineNum++;
                charNum++;

            }

        }

你所做的基本上是重叠 2 个循环通过相同的项目。事实是字符串是一个字符列表,所以你想要做的是遍历你的字符串。一个字符串也可以用正则表达式分成子字符串,创建一个单词列表:

/**
     * Parse the string (the line) into words. All spaces and dots are kept.
     * <br/> The following string
     * <ul>
     *     <li>"Hello, my name is Blanc! I'm a student. Can you help me? Thanks."</li>
     * </ul>
     *  will be parsed as :
     * <br/> "|Hello, |my |name |is |Blanc!| |I'm |a |student.| |Can |you |help |me?| |Thanks.|"
     * @param input the string to parse
     * @return the words of the input as an array
     * @see String#split(String)
     */
    public static String[] parseInputToWordsArray(String input) {
        return input.split("(?<= )|(?<=\\.)|(?<=\\?)|(?<=!)");
    }

接下来,您要检查数组的行及其单词列表以打印输出(未测试但应该可以):

ArrayList <String> list = new ArrayList();
        int lineNum;

        for (lineNum = 0; lineNum<list.size(); lineNum++) {
            int wordNum;
            String[] word = parseInputToWordsArray(list.get(lineNum));
            for(wordNum = 0; wordNum < word.length; wordNum++){
                System.out.println("Word : "+word[wordNum]+", Line Number : " + lineNum + ", Word Number: " + " " + wordNum);
            }

        }

希望它可以帮助


推荐阅读