有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Array<String>获取单词在行号上的字符位置

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

期望输出: 第1行,字符/位置1,程序 第1行,位置2,20: 第2行,位置3,内部 等等

电流输出为: 行号:0行读取://此程序应打印数字20。 行号:1行读作:程序20: 行号: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 
    }


}

共 (1) 个答案

  1. # 1 楼答案

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

    {“你好,世界”},{“祝你愉快”}

    要显示的内容应该是:

    • 字数:0
    • 单词:world,行号:0,单词号1
    • 单词:Have,行号:1,单词号0
    • 单词a,行号1,单词1
    • 单词:好,行号:1,单词号2
    • 字数:天,行号:1,字数3

    首先,有一种简单的方法可以将文件的所有行读取到数组列表中:

    /**
         * 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++;
    
                }
    
            }
    

    你要做的基本上是在同一个项目中叠加两个循环。事实上,字符串是一个字符列表,所以您要做的是遍历字符串。 字符串也可以用正则表达式划分为子字符串,从而创建一个单词列表:

    /**
         * 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);
                }
    
            }
    

    希望能有所帮助