Hi,I am reading this .CSV file and new to this.I have information in tabular form where i have names in 1row and 1column and in between cells i have numbers indicating links of those names by that purticular number. I want to retrevie those names who are having that purticular number from the correspoinding row and column.
gadisandeep 0 Newbie Poster
stultuske 1,116 Posting Maven Featured Poster
read them all in a list of objects, an object which has two variables: name and number
and then search through that list to check the name that correspond to the number
Tyrone.Wilson 0 Light Poster
I have a DataManager Class. this is how I do it (maybe there is a better way I don't know)
In my DataManager Class I have these methods (among others)
public ArrayList readAsStrings(File filename) throws FileNotFoundException, IOException {
/**returns all of the data in a file as Strings given the File object*/
ArrayList data = new ArrayList();
BufferedReader reader = new BufferedReader(new FileReader(filename));
String nextLine = reader.readLine();
if (filename.exists() && filename.canRead()) {
while (nextLine != null) {
data.add(nextLine);
nextLine = reader.readLine();
}
reader.close();//just a good idea aparently
}
return data;
}
public ArrayList extractFromCommas(String dataLine) {
//Gives back the data that is found between commas in a String
ArrayList data = new ArrayList();
String theString = "";
for (int i = 0; i < dataLine.length(); i++) {//go down the whole string
if (dataLine.charAt(i) == ',') {
if (i == 0) {
//do nothing
} else {
data.add(theString);//this means that the next comma has been reached
theString = "";//reset theString Variable
}
} else {
theString = theString + dataLine.charAt(i);//otherwise, just keep piling the chars onto the cumulative string
}
}
if (!theString.equalsIgnoreCase(""))//only if the last position is not occupied with nothing then add the end on
{
data.add(theString);
}
return data;
}
public ArrayList findString(ArrayList data, String searchString) {
//Finds a string in an arraylist of strings
ArrayList foundStrings = new ArrayList();
for (int i = 0; i < data.size(); i++) {
if (data.get(i).toString().contains(searchString)) {
foundStrings.add(data.get(i).toString());
}
}
return foundStrings;//returns null if the string is not found.
}
I would then use this in my main code:
public static void main(String[] args) throws FileNotFoundException, DocumentException, BadElementException, MalformedURLException, IOException {
// TODO code application logic here
DataManager dataMan = new DataManager();
ArrayList data = new ArrayList();
ArrayList col1 = new ArrayList();
ArrayList col2 = new ArrayList();
data = dataMan.readAsStrings(new File("Directory\\this is my file.csv"));
for(int i=0;i<data.size();i++){
ArrayList temp = new ArrayList();
temp = dataMan.extractFromCommas(data.get(i).toString());
col1.add(temp.get(0).toString());
col2.add(temp.get(1).toString());
}
String name = "What I Am Looking For";
data = dataMan.findString(col1, name);
for(int i=0;i<data.size();i++){
int index = Integer.parseInt(data.get(i).toString());
System.out.println(col1.get(index).toString()+" "+col2.get(index).toString());
}
}
I think this will work but will depend on what you want.
hope it helps in any case.
good luck.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.