简体   繁体   English

查找一个单词并返回该单词的特定值

[英]Find a word and return specific value of the word

I have a file with this content:我有一个包含以下内容的文件:

1.10.100.1        1000.0
1.10.100.2        2000.0
1.10.100.3        2000.0
1.10.500.4        1000.0

i wrote the function that find the specific string in the file:我写了 function 在文件中找到特定字符串:

public double searchInBalance(String depositNumber) {
    try {
        String[] words = null;
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String string;
        String inputBalanceToFind = depositNumber;
        while ((string = bufferedReader.readLine()) != null) {
            words = string.split("        ");
            for (String word : words) {
                if (word.equals(inputBalanceToFind)) {
                    System.out.println("string :" + string);
                }
            }
        }
        fileReader.close();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error in searchInBalance");
    }
    return 1;
}

i want to make a function that when find the left value of the file return the right value(the double value) back but have no idea how to do that please help me我想做一个 function ,当找到文件的左值时返回右值(双值),但不知道该怎么做,请帮帮我

  public double searchInBalance(String depositNumber){

        try {
            String[] words = null;
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String string;
            String inputBalanceToFind = depositNumber;
            while ((string = bufferedReader.readLine()) != null) {
                words = string.split("        ");

                for (int i = 1; i < words.length; i += 2) {
                    if (words[i].equals(inputBalanceToFind)) {
                       System.out.println(words[i]+" - "+words[i - 1]);
                    }
                }

            }
            fileReader.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error in searchInBalance");
        }
    }

Just make a for loop with a counter to work with the indexes of the Array.只需创建一个带有计数器的 for 循环来处理数组的索引。 But this is not a good programming style and you should try to improve your solution:D但这不是一种好的编程风格,您应该尝试改进您的解决方案:D

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM