Java 数组解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12094338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:34:08  来源:igfitidea点击:

Java Array parsing

javaarraysparsing

提问by Stephopolis

I have read a file into an array but am wondering how to parse certain values from that array.

我已将文件读入数组,但想知道如何解析该数组中的某些值。

My code:

我的代码:

        ...
        try{
             ...
             String strLine;
             String delims= "[ ]+";
             //parsing it
             ArrayList parsedit = new ArrayList();
             while((strLine=br.readLine())!=null){
                 System.out.println("Being read:");
                 System.out.println(strLine);
                 parsedit.add(strLine.split(delims));
             }
             System.out.println("Length:" + parsedit.size());
             in.close();   
             ...

The files that I am reading in are like this:

我正在阅读的文件是这样的:

a b c d e f g
1 2 3 4 5 6 7
1 4 5 6 3 5 7
1 4 6 7 3 2 5

Which makes the output like this:

这使得输出是这样的:

How many files will be input? 1
Hey, please write the full path of the input file number1! 
/home/User/Da/HA/file.doc
Being read:
a b c d e f g 
Being read:
1 2 3 4 5 6 7 
...
Length:4

I would like to parse out this data and just have the first and fifth values remaining, so that it would read like this instead:

我想解析出这些数据,只剩下第一个和第五个值,这样它就会变成这样:

a e
1 5

Does anyone have a recommendation on how to go about it? EDIT: Following some of the suggestions I have changed my code to:

有没有人有关于如何去做的建议?编辑:按照一些建议,我已将代码更改为:

public static void main(String[] args) {
        System.out.print("How many files will be input? ");
        Scanner readIn=new Scanner(System.in);
        int input=readIn.nextInt();
        int i=1;
        while(i<=input){
            System.out.println("Hey, please write the full path of the input file number" + i + "! ");
            Scanner fIn=new Scanner(System.in);
            String fileinput=fIn.nextLine();
          try{
             FileInputStream fstream=new FileInputStream(fileinput);
             DataInputStream in=new DataInputStream(fstream);
             BufferedReader br=new BufferedReader(new InputStreamReader(in));
             String strLine;
             String delims= "[ ]+";
             ArrayList parsedit = new ArrayList();
             while((strLine=br.readLine())!=null){
                 System.out.println("Being read:");
                 System.out.println(strLine);
                 parsedit.add(strLine.split(delims));
             }
             String[] splits=strLine.split(delims);
             System.out.println("Length:" + splits.length);
             in.close();   
        }
        catch(Exception e){
            System.err.println("Error: " + e.getMessage());
        }
        i++;
    }
  }
}

This doesn't give back any errors but it doesn't seem to be working all the same. Am I missing something silly? The output is this:

这不会返回任何错误,但它似乎并没有完全相同。我错过了一些愚蠢的东西吗?输出是这样的:

How many files will be input? 1
Hey, please write the full path of the input file number1! 
/home/User/Da/HA/file.doc
Being read:
a b c d e f g 
Being read:
1 2 3 4 5 6 7 
...

But despite the fact that I have a line to tell me the array length, it never gets printed, which tells me I just ended up breaking more than I fixed. Do you know what it is that I may be missing/forgetting?

但是尽管我有一行告诉我数组长度,但它永远不会被打印出来,这告诉我我最终破坏的比我修复的要多。你知道我可能错过/忘记的是什么吗?

回答by Razvan

The split()function returns an array of strings representing the tokens obtained from the original String.

split()函数返回一个字符串数组,表示从原始字符串获得的标记。

So, what you want is to keep only the first and the 5th token (splits[0] & splits[4]):

所以,你想要的是只保留第一个和第 5 个标记(splits[0] & splits[4]):

  String[] splits = strLine.split(delims); 
  //use the splits[0] & splits[4] to create the Strings you want

Regarding your update, replace this:

关于您的更新,请替换:

 while((strLine=br.readLine())!=null){
             System.out.println("Being read:");
             System.out.println(strLine);
             parsedit.add(strLine.split(delims));
 }

With this:

有了这个:

 while((strLine=br.readLine())!=null){
             System.out.println("Being read:");
             String splits[] = strLine.split(delims);
             System.out.println(splits[0]+" "+splits[4]);
             parsedit.add(splits);
 }

回答by nhahtdh

You just need to get the first and the fifth value out of the String[]returned by strLine.split(delims).

您只需要从String[]返回的 by 中获取第一个和第五个值strLine.split(delims)

(I am assuming you know what you are doing in your current code. You are assuming that each line will contains the same number of "columns", delimited by at least 1 space character. It is a fair assumption, regardless.)

(我假设您知道您在当前代码中在做什么。您假设每行将包含相同数量的“列”,由至少 1 个空格字符分隔。无论如何,这是一个公平的假设。)

回答by Anurag Ramdasan

You can try this

你可以试试这个

String[] line = s.split("\s+");

This way all your elements will be stored in a sequence in the array line. This will allow you to access whatever element you want.

这样,您的所有元素都将按顺序存储在数组中line。这将允许您访问您想要的任何元素。