简体   繁体   English

如何使用输入(java)从具有名称的文件中搜索信息?

[英]How can I search informations from file with names using input (java)?

(I copy-pasted the whole code, but you just have to look at line 28 to 45) We have to input the surname and the firstname to get the number of votes that the actual person got. (我复制粘贴了整个代码,但您只需要看第28至45行)我们就必须输入姓和名来获得实际人员的投票数。 I wrote a code and if I type in the input the first row surname firstname it works nicely(I get 19, which on is the correct number), but every time when I try to write another name in the input field it doesn't work. 我写了一个代码,如果我在输入中输入第一行的名字,firstname可以很好地工作(我得到19,这是正确的数字),但是每次当我尝试在输入字段中写另一个名字时,它都不会工作。

Inside the file there are 5 cloumns, (1st and the 5th is unnecessary in this problem) 2.cloumn we have votes that a person got 3.column we have surnames and obviously in the 4.cloumn we have first names. 在文件内有5个cloumns,(在这个问题上第1和第5个是不必要的)2.cloumn我们有一个人选票3.column我们有姓,显然在4.cloumn中我们有名字。

kepviselok.txt kepviselok.txt

expected and actual output 预期和实际产出

    package erettsegi2;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {
        var lista = Files.readAllLines(Paths.get("kepviselok.txt"));
        var listak = new ArrayList<Lista>();
        var input = new Scanner(System.in);

        for (var valaszt : lista) {
            String[] split = valaszt.split(" ");
            var kerSzam = Integer.parseInt(split[0]);
            var szavazottSzam = Integer.parseInt(split[1]);
            var nev = split[2] + " " + split[3];
            var part = split[4].equals("-") ? "Independent " : split[4];

            listak.add(new Lista(kerSzam, szavazottSzam, nev, part));
        }
        System.out.println("2.");
        System.out.println("Number of Representatives on Election: " + listak.size());

        System.out.println("3.\nPlease write a surname!");
        var surname = input.nextLine();
        System.out.println("Please write a first name!");
        var firstname = input.nextLine();

        for (var i = 0; i < listak.size();i++) {
            var actualCandidate = listak.get(i);

            if (actualCandidate.name.equals(surname + " " + firstname)) {
                System.out.println(actualCandidate.name);
                System.out.println("Number of votes " + actualCandidate.votes);
                break;
            } else {
                System.out.println("No candidate with this name");
                break;
            }
        }

    }

    static class Lista {
        int sorszam;
        int votes;
        String name;
        String part;

        public Lista(int sorszam, int szavazatokSzama, String nev, String part) {
            this.sorszam = sorszam;
            this.votes = szavazatokSzama;
            this.name = nev;
            this.part = part;
        }

    }
}

Take a look on your code, in the last cycle you have condition if-else and this condition will finish the cycle if first candidate name not equals ( else executed, and you have break inside it) or this condition will finish the cycle because you found candidate in the first line ( if block executed). 看一下代码,在最后一个循环中,您具有条件if-else并且如果第一个候选名称不相等( else执行,并且您在其中break了),则此条件将结束该循环, else ,由于您在第一行中找到候选对象( if执行了块)。 It means that you are checking only first entry of your list, then cycle finishes because if or else block executed. 这意味着你只检查列表的第一个条目,然后循环结束,因为if还是else块执行。

So, correct solution will be move print of "No candidate with this name" outside of cycle if you haven't found candidate (I added boolean candidateFound variable for this purpose), because only after check of whole list you can state that: 因此,如果您没有找到候选者,正确的解决方案将是在周期外移动"No candidate with this name""No candidate with this name"打印( "No candidate with this name"我添加了boolean candidateFound变量),因为只有在检查了整个列表之后,您才能声明:

boolean candidateFound = false;
for (var i = 0; i < listak.size(); i++) {
    var actualCandidate = listak.get(i);

    if (actualCandidate.name.equals(surname + " " + firstname)) {
        System.out.println(actualCandidate.name);
        System.out.println("Number of votes " + actualCandidate.votes);

        candidateFound = true;
        break;
    }
}

if (!candidateFound) {
    System.out.println("No candidate with this name");
}

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

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