简体   繁体   English

如何从txt文件行读取以及打印字符串和整数数组

[英]How to read from txt file line and print arrays of strings and integers

I am reading from a text file a specific line defined by a user input with if(nameLine.contains(inputFileName)){ I am trying to print an array of names and array of numbers from that line. 我正在从文本文件中读取由用户输入使用if(nameLine.contains(inputFileName)){定义的特定行。我正在尝试从该行打印名称数组和数字数组。 Could anyone help me out with how to do so? 谁能帮我解决这个问题? Do I have to create 2 different methods or can I do it all in main? 我必须创建2种不同的方法还是可以全部完成? I only know how to work with one Array in one code with while loop , but I am not sure how to have it all combined. 我只知道如何通过while loop在一个代码中使用一个Array ,但是我不确定如何将它们组合在一起。 Thank you for your help! 谢谢您的帮助!

My code is not finished, but this is how it looks so far 我的代码还没有完成,但是到目前为止是这样

public static void main(String[] args) throws IOException {

    Scanner in = new Scanner(System.in);
    System.out.print("Enter the State name: ");
    String inputFileName = in.next();
    String names = "pplnames17.txt";

    List<String> namearr = new ArrayList<String>();

    File namesFile = new File(names);
    Scanner stateSearch = new Scanner (namesFile);

    //print title
    String line0 = Files.readAllLines(Paths.get("pplnames17.txt")).get(0);
    System.out.printf("%s in %s", line0, inputFileName);

    while(stateSearch.hasNextLine()){
        String nameLine = stateSearch.nextLine();
        if(nameLine.contains(inputFileName)){
            System.out.printf("\n%s",nameLine);
        }
    }

This is a partial of my Array code for String , which I had it after a while loop 这是我的String Array代码的一部分,在while loop之后就拥有了

    while(stateSearch.hasNextLine()){
    namearr.add(stateSearch.nextLine());
}
String[] arr = namearr.toArray(new String [0]);
System.out.println(arr);

Whilst this isn't the greatest solution, here is a potential solution that you could build upon/tidy up. 虽然这不是最大的解决方案,但这里是您可以构建/整理的潜在解决方案。

Basically, read in your file, loop through each line, if the line contains the user input, split each string on that line, check if text or numeric, and add to the relevant array. 基本上,读入文件,遍历每一行,如果该行包含用户输入,则在该行上拆分每个string ,检查文本还是数字,然后添加到相关数组中。

public static void main(String[] args) throws FileNotFoundException {
        List<String> nameArr = new ArrayList<>();
        List<Double> intArr = new ArrayList<>();
        System.out.print("Enter the State name: ");
        Scanner in = new Scanner(System.in);
        String inputFileName = in.next();

        Scanner sc = new Scanner(new File("pplnames17.txt"));

        while (sc.hasNextLine()) {
            String nameLine = sc.nextLine();
            if (nameLine.contains(inputFileName)) {
                String[] inputs = nameLine.split(" ");
                for (String input : inputs) {
                    if (isNumeric(input)) {
                        intArr.add(Double.parseDouble(input));
                    } else {
                        nameArr.add(input);
                    }
                }
            }
        }
        if (!nameArr.isEmpty()) {
            System.out.println(nameArr);
        } else {
            System.out.println("No names found");
        }
        if (!intArr.isEmpty()) {
            System.out.println(intArr);
        } else {
            System.out.println("No numbers found");
        }
    }

    public static boolean isNumeric(String str) {
        try {
            double d = Double.parseDouble(str);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

I tested it and my text file was like this: 我测试了一下,我的文本文件是这样的:

abc def ghi 12 55 jkl mno pqr abc def ghi 12 55 jkl mno pqr

joe jack mary 1 2 james 4 乔·杰克·玛丽1 2詹姆斯4

paul jim kim bob 45 othername 保罗·吉姆·金·鲍勃45

bill yu will gill 455 Paulo 比尔·尤·吉尔455 Paulo

User input of mary gave an output of: mary用户输入给出了以下输出:

[joe, jack, mary, james] [乔,杰克,玛丽,詹姆斯]

[1.0, 2.0, 4.0] [1.0、2.0、4.0]

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

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