Reputation: 1127
I am aware that I can use File Reader class. But I am confused on the implementation when it comes to reading separate sections of text within a line.
Each line in the file will have names (first and last name separated by an empty space), but a comma is used to delimited the UKNOWN set of full names. A semicolon is then used to to separate the section within the line. The next section is consisted of names of streets (each street name is delimited by a comma as well).
What I want to do is read the full names into an ArrayList of strings, when the semicolon is reached, the street names should be inserted into a separate ArrayList of strings.
If I could get a brief example of how to go on about the implementation, I could the entire thing on my own.
NOTE: It must read from a text file. Each line will be a separate test case of full names, and street names.
EDIT:
Here is a sample of the input: James Cooper ,John Evans,Abe Lincoln;Jackson st,No way,Aspen Way
Output: The array list of names holds = James Cooper , John Evans, Abe Lincoln (each separated by commas)
The array list of street names holds = Jackson st,No way,Aspen Way (each separated by commas)
Upvotes: 1
Views: 6944
Reputation: 36
If you don't mind introducing an extra dependency have a look at OpenCSV (http://opencsv.sourceforge.net/).
This is a pretty basic example that reads a line (called 'nextLine'), then adds each element in to the dataArray. This changes to a new line when it calls reader.readNext(). If you were sure that there are only two lines of data you could replace the 'while' statement and hard-code the names array first, call readNext() then the street names array.
//Create the ArrayList to hold data
ArrayList<String> dataArray = new ArrayList<String>();
//openCSV reader to parse the CSV file
CSVReader reader = new CSVReader(new FileReader(f));
//nextLine array contains a an entire row of data,
//Each element represents a cell in the spreadsheet.
String[] nextLine;
//Iterate though the CSV while there are more lines to read
while((nextLine = reader.readNext()) != null)
{
//Iterate through the elements on this line
for(String s : nextLine)
{
dataArray.add(s);
}
}
Source: http://opencsv.sourceforge.net/#how-to-read
-Kaz
Upvotes: 0
Reputation: 1893
The Scanner
class is good enough for all of this. You have to use the method useDelimiter()
appropriately, depending on the delimiter (i.e. separating character/string) you want to use.
Upvotes: 0
Reputation: 23465
The easiest thing to do is probably to read the entire line into a String
and then use String
's split()
a bunch of times.
String line; // Put the line in here however you're reading from the file.
String[] sections = line.split(";");
// Assuming you know there are always two sections, names and addresses:
String[] names = sections[0].split(",");
String[] addresses = sections[1].split(",");
// Convert arrays into ArrayLists if you actually need to
List<String> namesList = new ArrayList<>( Arrays.asList( names ) );
List<String> addressList = new ArrayList<>( Arrays.asList( addresses ) );
Upvotes: 3