0

hello im trying to take my values in my text file input, and store them in an array then parse those values into usable data and then store those values in a new array. I have the reading of the file but for some reason i cant seem to gain access to the values correctly, i try to parse the values or remove elements but it doesnt seem to change anything in my output when i print the changed array.

please let me know what part im messing up on

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();
    }   
}

please include any at all sources for any help you give, thank you very much in advance. I really appreciate it.

1
  • Here it is wrong lines.get(0).replace(',', ' '); String is immutable. Put the result of the replace method back into the list e.g. lines.set(0, lines.get(0).replace(',', ' ')); Commented Oct 15, 2013 at 5:28

1 Answer 1

3

You need to assign the changes back after changing a String, as String is immutable in 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
Sign up to request clarification or add additional context in comments.

2 Comments

You my friend are truly a life savor, i cant believe that was the part i was missing. now it should be pretty easy to get the values stored in an array for us with parsing. Thank you so much
Happens to everybody, no worries. Glad to be of help!:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.