如何在字符串数组中存储input.text,然后将值解析为单独的数组

时间:2013-10-15 05:25:32

标签: java arrays parsing text

你好我试图在我的文本文件输入中获取我的值,并将它们存储在一个数组中,然后将这些值解析为可用数据,然后将这些值存储在一个新数组中。我有文件的读取,但由于某种原因我似乎无法正确访问值,我尝试解析值或删除元素,但它似乎没有改变我的输出当我打印更改的数组。

请让我知道哪些部分搞砸了

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;


public class dataminingp1 
{

    String[] data = new String[100];
    String line;

    public void readf() throws IOException 
    {

        FileReader fr = new FileReader("C:\\input.txt");
        BufferedReader br = new BufferedReader(fr);

        int i = 0;
        while ((line = br.readLine()) != null) 
        {
            data[i] = line;
            System.out.println(data[i]);
            i++;
        }
        br.close();
        System.out.println("Data length: "+data.length);

        String[][] root;

        List<String> lines = Files.readAllLines(Paths.get("input.txt"), StandardCharsets.UTF_8);

        root = new String[lines.size()][];

        lines.removeAll(Arrays.asList("", null)); // <- remove empty lines

        for(int a =0; a<lines.size(); a++)
        {
            root[a] = lines.get(a).split(" ");
        }
        lines.get(0).replace(',', ' ');
        //int p = Integer.parseInt(root[0][0]);
        System.out.println(lines.get(0));
        //System.out.println(p);

    }

    public static void main(String[] args) throws IOException 
    {
        dataminingp1 sarray = new dataminingp1();
        sarray.readf();
    }   
}

请在所有来源中包含任何帮助,非常感谢您提前。我真的很感激。

1 个答案:

答案 0 :(得分:3)

您需要在更改字符串后重新分配更改,因为String在java中是不可变的。

String changedLine = lines.get(0).replace(',', ' '); // Assign the modified String returned by replace method to changedLine
lines.set(0, changedLine); // Set the 0th index in the lines with the changedLine
相关问题