Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split the strings in a file and read them?

I have a file with information in it. It looks like:

    Michael 19 180 Miami
    George 25 176 Washington
    William 43 188 Seattle

I want to split the lines and strings and read them. I want it to look like:

    Michael
    19
    180
    Miami
    George
    ...

i used a code like this:

    BufferedReader in = null;
    String read;
    int linenum;
    try{
        in = new BufferedReader(new FileReader("fileeditor.txt")); 
    }
    catch (FileNotFoundException e) {System.out.println("There was a problem: " + e);}
    try{
        for (linenum = 0; linenum<100; linenum++){
            read = in.readLine();
            if(read == null){} 
            else{
                String[] splited = read.split("\\s+");
                System.out.println(splited[linenum]);
           }
       }
    }
    catch (IOException e) {System.out.println("There was a problem: " + e);} 
}

What this gave me was

    Michael
    25
    188

I think its probably an issue with my for loop but I'm not very advanced in programming and I'll appreciate help. Thanks.

like image 404
Solijoli Avatar asked Sep 04 '13 07:09

Solijoli


People also ask

How do you split a string in a text file?

You can use String. split() method (in your case it's str. split("\\s+"); ).

How do I read a split file?

You can also select File → Open..., navigate to your file, and click Open. After you open your file, select "Split," and choose the format of the file (if you are splitting a . PNG file, choose "PNG") without any compression.

How do you split strings?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do you read a split string in Python?

Use open with readline() or readlines() . The former will return a line at a time, while the latter returns a list of the lines. Use split(delimiter) to split on the comma.


1 Answers

You're part way there which is great.

When reading a file, the Reader will return null when it reaches the end of the stream, meaning nothing else is available to be read. Your current approach means that you want to read at least 100 lines, but no more...this will become problematic in the future if you file size increases...it's also somewhat wasteful

Instead, we should use the fact a null value indicates the end of the file..

When you split a line, it will contain a number of elements. You are using the linenum variable to print these. The problem is, you've already read and split the line, the linenum is irrelevant for this task, as it represents the number of lines you've already read, not the part of the string you've just split.

Instead, you need to use a inner loop to display the individual split elements for each line...

For example...

BufferedReader in = null;
try {
    in = new BufferedReader(new FileReader("fileeditor.txt"));
    String read = null;
    while ((read = in.readLine()) != null) {
        String[] splited = read.split("\\s+");
        for (String part : splited) {
            System.out.println(part);
        }
    }
} catch (IOException e) {
    System.out.println("There was a problem: " + e);
    e.printStackTrace();
} finally {
    try {
        in.close();
    } catch (Exception e) {
    }
}

Also, don't forget, if you open it, you musty close it ;)

You might want to take a little more time going through Basic I/O as well ;)

like image 185
MadProgrammer Avatar answered Oct 22 '22 09:10

MadProgrammer