Split() String Method for BufferedReader Class

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 860 Views - Last Post: 19 September 2020 - 04:03 PM Rate Topic: -----

#1 Field Dog   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 26-March 16

Split() String Method for BufferedReader Class

Posted 12 September 2020 - 03:10 PM

I would like to make the program below more compact. The purpose of the program is to output a single column of latitudes followed by a single column of longitudes.

import java.io.*;
public class ReadTextFile
{
    public static void main(String[] args)throws Exception
    {
        File file = new File("C:\\Users\\Mark\\Documents\\U.S. Cities Lat Long CSV File.txt");

        BufferedReader br1 = new BufferedReader(new FileReader(file));
        BufferedReader br2 = new BufferedReader(new FileReader(file));

        String latitude;
        String longitude;

        while ((latitude = br1.readLine()) != null) {
            latitude = latitude.split(",")[0];
            System.out.println(latitude);
        }

        while ((longitude = br2.readLine()) != null) {
            longitude = longitude.split(",")[1];
            System.out.println(longitude);
        }
    }
}

/*
Output:

34.155834
42.933334
42.095554
38.846668
41.392502
-119.202789
-76.566666
-79.238609
-91.948059
-81.534447
*/



Is This A Good Question/Topic? 0
  • +

Replies To: Split() String Method for BufferedReader Class

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 840
  • View blog
  • Posts: 6,448
  • Joined: 25-December 13

Re: Split() String Method for BufferedReader Class

Posted 12 September 2020 - 05:17 PM

Can you save the longitude values in a list so that the file only needs to be read one time?
Read a line, split, print lat, save longitude. When done reading print the longitudes.
Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 17,003
  • Joined: 20-September 08

Re: Split() String Method for BufferedReader Class

Posted 12 September 2020 - 05:21 PM

This is odd. Why are you using two Readers? If there's lines of LAT,LONG then you need one, and split into the two values
Was This Post Helpful? 0
  • +
  • -

#4 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2418
  • View blog
  • Posts: 5,061
  • Joined: 11-December 07

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 04:01 AM

Oh, this is a cool question. You've obviously noticed that there is some duplication going on but also that it's tricky to deal with.

I'm not going to change the way your program works, just help you to restructure it. If the file is small enough to fit in memory then NormR and g00se have some great suggestions above.

First, let's move the variables to where they are used

import java.io.*;
public class ReadTextFile
{
    public static void main(String[] args)throws Exception
    {
        File file = new File("../../scratch/file.txt");

        BufferedReader br1 = new BufferedReader(new FileReader(file));
        String latitude;
        while ((latitude = br1.readLine()) != null) {
            latitude = latitude.split(",")[0];
            System.out.println(latitude);
        }

        BufferedReader br2 = new BufferedReader(new FileReader(file));
        String longitude;
        while ((longitude = br2.readLine()) != null) {
            longitude = longitude.split(",")[1];
            System.out.println(longitude);
        }
    }
}


Now we can see that there are two almost identical blocks. Let's see how close we can make them by renaming a few variables:

import java.io.*;
public class ReadTextFile
{
    public static void main(String[] args)throws Exception
    {
        File file = new File("../../scratch/file.txt");
        
        BufferedReader reader;
        String line;
        
        reader = new BufferedReader(new FileReader(file));
        while ((line = reader.readLine()) != null) {
            line = line.split(",")[0];
            System.out.println(line);
        }

        reader = new BufferedReader(new FileReader(file));
        while ((line = reader.readLine()) != null) {
            line = line.split(",")[1];
            System.out.println(line);
        }
    }
}


Now the only difference between the two blocks is the index after the split. Maybe if we can turn the number into a variable we can make them identical.

import java.io.*;
public class ReadTextFile
{
    public static void main(String[] args)throws Exception
    {
        File file = new File("../../scratch/file.txt");
        
        BufferedReader reader;
        String line;
        int i = 0;
        
        reader = new BufferedReader(new FileReader(file));
        while ((line = reader.readLine()) != null) {
            line = line.split(",")[i];
            System.out.println(line);
        }
        i++;

        reader = new BufferedReader(new FileReader(file));
        while ((line = reader.readLine()) != null) {
            line = line.split(",")[i];
            System.out.println(line);
        }
        i++;
    }
}


Perfect. Now they are identical. You may have noticed that the second i++ isn't necessary. It's just there to keep these blocks identical.

Two common strategies to deal with duplicate code are to use a loop or to extract a method. I'm going to use a loop for this example.

import java.io.*;
public class ReadTextFile
{
    public static void main(String[] args)throws Exception
    {
        File file = new File("../../scratch/file.txt");
        
        BufferedReader reader;
        String line;
        int i = 0;
        
        while(i < 2) {
	        reader = new BufferedReader(new FileReader(file));
	        while ((line = reader.readLine()) != null) {
	            line = line.split(",")[i];
	            System.out.println(line);
	        }
	        i++;
        }
    }
}


Perfect. Now that the duplication is removed, I'm going to clean it up.I'll change the while loop to a for loop. I'll move variable declarations to the smallest scope possible and I'll use try with resources for the reader.

import java.io.*;
public class ReadTextFile
{
    public static void main(String[] args)throws Exception
    {
        File file = new File("../../scratch/file.txt");
        
        for(int i = 0 ;i < 2; i++) {
        	try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
	        	String line;
		        while ((line = reader.readLine()) != null) {
		            line = line.split(",")[i];
		            System.out.println(line);
		        }
        	}
        }
    }
}

Was This Post Helpful? 1
  • +
  • -

#5 Field Dog   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 26-March 16

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 04:55 AM

@ cfoley

Is it common practice to move variables to where they are used? I've always placed all my variables together so I can keep track of them.

This post has been edited by Field Dog: 13 September 2020 - 04:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 Field Dog   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 26-March 16

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 05:30 AM

View Postg00se, on 12 September 2020 - 05:21 PM, said:

Why are you using two Readers?


I don't know how to split a line in order to read the text to the left of the comma and to the right of the comma simultaneously. I used two BufferedReaders for readability. Does the word Buffered in BufferedReader refer to slowing down the data stream?
Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 840
  • View blog
  • Posts: 6,448
  • Joined: 25-December 13

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 05:32 AM

Quote

how to split a line in order to read the text to the left of the comma and to the right of the comma simultaneously

Look at the array indexes (line 15 and line 20) your code uses: [0] for before the , and [1] for after it. split() returns an array that your code is indexing into for the tokens scanned by the method from the input record.

This post has been edited by NormR: 13 September 2020 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 17,003
  • Joined: 20-September 08

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 05:52 AM

Quote

Does the word Buffered in BufferedReader refer to slowing down the data stream?

No ;) It means that preprocessing occurs in a memory buffer (8KiB iirc)

btw, how do you know to which city the lat/long data belong?

This post has been edited by g00se: 13 September 2020 - 05:53 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#9 Field Dog   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 26-March 16

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 06:06 AM

[quote name='NormR' date='13 September 2020 - 05:32 AM' timestamp='1600000369' post='2424556']

Quote

split() returns an array that your code is indexing into for the tokens scanned by the method from the input record.


Help me with the terminology please. The array is indicated by [0] or [1]. The indexing is the integer inside the []. The tokens are 3 4 . 1 5 5 8 3 4 , - 1 1 9 . 2 0 2 7 8 9 (first line of the input record). The input record is the File. All correct?
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 840
  • View blog
  • Posts: 6,448
  • Joined: 25-December 13

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 06:09 AM

Partly correct. The contents of the input record is in the variable: latitude in this line:
    latitude = br1.readLine()  // read input record


Quote

The tokens are 3 4 . 1 5 5 8 3 4 , - 1 1 9 . 2 0 2 7 8 9

The tokens (colored in red) are the Strings delimiter by the ,

This post has been edited by NormR: 13 September 2020 - 06:13 AM

Was This Post Helpful? 1
  • +
  • -

#11 Field Dog   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 26-March 16

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 06:40 AM

View Postg00se, on 13 September 2020 - 05:52 AM, said:

Quote

Does the word Buffered in BufferedReader refer to slowing down the data stream?

btw, how do you know to which city the lat/long data belong?


Good question! I was just using data similar to what I'm actually going to use. Instead of using cities, I will be using lakes that I check for water elevations. Ultimately, the lat/long data will be converted into a KML file for use with Google Maps. The data will simply create pins on the map. There's no need to know which lake the lat/long data belongs to.
Was This Post Helpful? 0
  • +
  • -

#12 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 17,003
  • Joined: 20-September 08

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 06:57 AM

You don't want to use a Google API directly with the data?

Because if not, your intention to produce a kml file can be done by simply appending the Z coordinate to your existing text and using that for a <coordinates> element. IOW it doesn't need parsing

This post has been edited by g00se: 13 September 2020 - 07:04 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#13 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2418
  • View blog
  • Posts: 5,061
  • Joined: 11-December 07

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 09:22 AM

Is it common practice to move variables to where they are used? I've always placed all my variables together so I can keep track of them.

Yes. It becomes more important in larger programs where you have hundreds or thousands of variables. Declaring each variable in the scope (i.e. loop, method or class) where it is used is an important strategy for managing them. Even in this example, moving the variables to the smallest scope possible made it easier to restructure.

I don't know how to split a line in order to read the text to the left of the comma and to the right of the comma simultaneously.

There should be no magic in programming. I recommend adding some "logging" to your program to act as a learning tool. The following might be overkill but you can add as much or as little detail as you need when exploring:

import java.io.*;
public class ReadTextFile
{
    public static void main(String[] args)throws Exception
    {
        File file = new File("../../scratch/file.txt");
        
        for(int i = 0 ;i < 2; i++) {
        	System.out.println("i = " + i);
        	try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
	        	String line;
		        while ((line = reader.readLine()) != null) {
		        	System.out.println("The whole line is: " + line);
		        	System.out.println("It can be split into this many parts: " + line.split(",").length);
		        	System.out.println("The first part is: " + line.split(",")[0]);
		        	System.out.println("The second part is: " + line.split(",")[1]);
		            line = line.split(",")[i];
		        	System.out.println("The part selected for output is: " + line);
		            System.out.println(line);
		        }
        	}
        }
    }
}


Does the word Buffered in BufferedReader refer to slowing down the data stream?

Quite the opposite, it will speed it up! Although if your file is small, you won't be able to measure the difference. Using a buffer means that the program can read entire chunks of the file into memory at a time rather than one byte at a time.
Was This Post Helpful? 1
  • +
  • -

#14 Field Dog   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 26-March 16

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 09:24 AM

View Postg00se, on 13 September 2020 - 06:57 AM, said:

You don't want to use a Google API directly with the data?

I have no idea how to do that. I'm taking this step-by-step, learning as I go. Thanks for the info!
Was This Post Helpful? 0
  • +
  • -

#15 Field Dog   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 26-March 16

Re: Split() String Method for BufferedReader Class

Posted 13 September 2020 - 09:46 AM

View Postcfoley, on 13 September 2020 - 09:22 AM, said:

I recommend adding some "logging" to your program to act as a learning tool.

I'm already using "logging", thanks. I need to learn how to use the debugging tools in the JetBrains IDE. I don't know how to set breakpoints, for example. In the JetBrains IDE, is there a way to "single step" through the code? IOW, run the code line-by-line?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2