Read a particular data corresponding to a row & column in CSV
I need some help. I want to implement a code where-in I wish to read data from a csv file.
Sample CSV file (details.csv) :
id,name,age
1,bh,23
2,nit,24
I want to create such a method in java to read this CSV that I pass the sheetname, columnname and rowname as parameters. Where ever there is a match of this combination, the corresponding data is picked.
Example :- I want to pass details.csv,name & 1. I should get the output as bh.
Could you please help me with this?
I tried with the following code but it is returning null value :-
public static String searchCsvLine(String filename, String searchString, String rowname) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ( (line = br.readLine()) != null )
String values = line.split(",");
if(values.equals(searchString))
resultRow = line;
break;
br.close();
return resultRow;
java csv selenium automation
add a comment |
I need some help. I want to implement a code where-in I wish to read data from a csv file.
Sample CSV file (details.csv) :
id,name,age
1,bh,23
2,nit,24
I want to create such a method in java to read this CSV that I pass the sheetname, columnname and rowname as parameters. Where ever there is a match of this combination, the corresponding data is picked.
Example :- I want to pass details.csv,name & 1. I should get the output as bh.
Could you please help me with this?
I tried with the following code but it is returning null value :-
public static String searchCsvLine(String filename, String searchString, String rowname) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ( (line = br.readLine()) != null )
String values = line.split(",");
if(values.equals(searchString))
resultRow = line;
break;
br.close();
return resultRow;
java csv selenium automation
values.equals(searchString)
- you are comparing an array with a String
– Kartik
Nov 16 '18 at 7:02
could you help me to resolve this
– Bhoomika Datta
Nov 16 '18 at 7:11
@BhoomikaDatta - You can use if condition like this, considering name is at fixed index on each lineif(values[1].equals(searchString)) resultRow = line; break;
– Amit Jain
Nov 16 '18 at 7:36
It still returns a null value
– Bhoomika Datta
Nov 16 '18 at 7:46
Convert array to list like by List<String> list = Arrays.asList(values); then use list.contains(searchString) instead
– Trung NT Nguyen
Nov 16 '18 at 8:49
add a comment |
I need some help. I want to implement a code where-in I wish to read data from a csv file.
Sample CSV file (details.csv) :
id,name,age
1,bh,23
2,nit,24
I want to create such a method in java to read this CSV that I pass the sheetname, columnname and rowname as parameters. Where ever there is a match of this combination, the corresponding data is picked.
Example :- I want to pass details.csv,name & 1. I should get the output as bh.
Could you please help me with this?
I tried with the following code but it is returning null value :-
public static String searchCsvLine(String filename, String searchString, String rowname) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ( (line = br.readLine()) != null )
String values = line.split(",");
if(values.equals(searchString))
resultRow = line;
break;
br.close();
return resultRow;
java csv selenium automation
I need some help. I want to implement a code where-in I wish to read data from a csv file.
Sample CSV file (details.csv) :
id,name,age
1,bh,23
2,nit,24
I want to create such a method in java to read this CSV that I pass the sheetname, columnname and rowname as parameters. Where ever there is a match of this combination, the corresponding data is picked.
Example :- I want to pass details.csv,name & 1. I should get the output as bh.
Could you please help me with this?
I tried with the following code but it is returning null value :-
public static String searchCsvLine(String filename, String searchString, String rowname) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ( (line = br.readLine()) != null )
String values = line.split(",");
if(values.equals(searchString))
resultRow = line;
break;
br.close();
return resultRow;
java csv selenium automation
java csv selenium automation
edited Nov 16 '18 at 7:01
Kartik
4,45731537
4,45731537
asked Nov 16 '18 at 7:00
Bhoomika DattaBhoomika Datta
133
133
values.equals(searchString)
- you are comparing an array with a String
– Kartik
Nov 16 '18 at 7:02
could you help me to resolve this
– Bhoomika Datta
Nov 16 '18 at 7:11
@BhoomikaDatta - You can use if condition like this, considering name is at fixed index on each lineif(values[1].equals(searchString)) resultRow = line; break;
– Amit Jain
Nov 16 '18 at 7:36
It still returns a null value
– Bhoomika Datta
Nov 16 '18 at 7:46
Convert array to list like by List<String> list = Arrays.asList(values); then use list.contains(searchString) instead
– Trung NT Nguyen
Nov 16 '18 at 8:49
add a comment |
values.equals(searchString)
- you are comparing an array with a String
– Kartik
Nov 16 '18 at 7:02
could you help me to resolve this
– Bhoomika Datta
Nov 16 '18 at 7:11
@BhoomikaDatta - You can use if condition like this, considering name is at fixed index on each lineif(values[1].equals(searchString)) resultRow = line; break;
– Amit Jain
Nov 16 '18 at 7:36
It still returns a null value
– Bhoomika Datta
Nov 16 '18 at 7:46
Convert array to list like by List<String> list = Arrays.asList(values); then use list.contains(searchString) instead
– Trung NT Nguyen
Nov 16 '18 at 8:49
values.equals(searchString)
- you are comparing an array with a String– Kartik
Nov 16 '18 at 7:02
values.equals(searchString)
- you are comparing an array with a String– Kartik
Nov 16 '18 at 7:02
could you help me to resolve this
– Bhoomika Datta
Nov 16 '18 at 7:11
could you help me to resolve this
– Bhoomika Datta
Nov 16 '18 at 7:11
@BhoomikaDatta - You can use if condition like this, considering name is at fixed index on each line
if(values[1].equals(searchString)) resultRow = line; break;
– Amit Jain
Nov 16 '18 at 7:36
@BhoomikaDatta - You can use if condition like this, considering name is at fixed index on each line
if(values[1].equals(searchString)) resultRow = line; break;
– Amit Jain
Nov 16 '18 at 7:36
It still returns a null value
– Bhoomika Datta
Nov 16 '18 at 7:46
It still returns a null value
– Bhoomika Datta
Nov 16 '18 at 7:46
Convert array to list like by List<String> list = Arrays.asList(values); then use list.contains(searchString) instead
– Trung NT Nguyen
Nov 16 '18 at 8:49
Convert array to list like by List<String> list = Arrays.asList(values); then use list.contains(searchString) instead
– Trung NT Nguyen
Nov 16 '18 at 8:49
add a comment |
2 Answers
2
active
oldest
votes
You should parse the first line of csv file to get column names.
public class Test
public static String searchCsvLine(String filename, String columnName, String rowName) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
// parse first line as column names
String line = br.readLine();
String columns = line.split(",");
int columnIndex = -1;
for (int i = 0; i < columns.length; i++)
if (columnName.equals(columns[i]))
columnIndex = i;
break;
// read lines to match row
while ((line = br.readLine()) != null)
String values = line.split(",");
if (rowName.equals(values[0]))
// return value at columnIndex
resultRow = values[columnIndex];
break;
br.close();
return resultRow;
public static void main(String args) throws IOException
String name = searchCsvLine("details.cvs", "name", "1");
System.out.println("name is " + name);
Thanks Kerie.. This worked for me..!!
– Bhoomika Datta
Nov 19 '18 at 4:21
add a comment |
Try below :
public static String searchCsvLine(String filename, String searchString,String rowname) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
int searchStringIndex=0;
while ((line = br.readLine()) != null)
String values = line.split(",");
//get index of searchString
for (int i = 0; i < values.length; i++)
if(values[i].equalsIgnoreCase(searchString))
searchStringIndex=i;
break;
//get required rowname
for (int i = 0; i < Integer.parseInt(rowname); i++)
line=br.readLine();
//split the line found by rowname and get value as per searchString index (given column)
values = line.split(",");
resultRow=values[searchStringIndex];
break;
br.close();
System.out.println(resultRow);
return resultRow;
}
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332909%2fread-a-particular-data-corresponding-to-a-row-column-in-csv%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should parse the first line of csv file to get column names.
public class Test
public static String searchCsvLine(String filename, String columnName, String rowName) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
// parse first line as column names
String line = br.readLine();
String columns = line.split(",");
int columnIndex = -1;
for (int i = 0; i < columns.length; i++)
if (columnName.equals(columns[i]))
columnIndex = i;
break;
// read lines to match row
while ((line = br.readLine()) != null)
String values = line.split(",");
if (rowName.equals(values[0]))
// return value at columnIndex
resultRow = values[columnIndex];
break;
br.close();
return resultRow;
public static void main(String args) throws IOException
String name = searchCsvLine("details.cvs", "name", "1");
System.out.println("name is " + name);
Thanks Kerie.. This worked for me..!!
– Bhoomika Datta
Nov 19 '18 at 4:21
add a comment |
You should parse the first line of csv file to get column names.
public class Test
public static String searchCsvLine(String filename, String columnName, String rowName) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
// parse first line as column names
String line = br.readLine();
String columns = line.split(",");
int columnIndex = -1;
for (int i = 0; i < columns.length; i++)
if (columnName.equals(columns[i]))
columnIndex = i;
break;
// read lines to match row
while ((line = br.readLine()) != null)
String values = line.split(",");
if (rowName.equals(values[0]))
// return value at columnIndex
resultRow = values[columnIndex];
break;
br.close();
return resultRow;
public static void main(String args) throws IOException
String name = searchCsvLine("details.cvs", "name", "1");
System.out.println("name is " + name);
Thanks Kerie.. This worked for me..!!
– Bhoomika Datta
Nov 19 '18 at 4:21
add a comment |
You should parse the first line of csv file to get column names.
public class Test
public static String searchCsvLine(String filename, String columnName, String rowName) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
// parse first line as column names
String line = br.readLine();
String columns = line.split(",");
int columnIndex = -1;
for (int i = 0; i < columns.length; i++)
if (columnName.equals(columns[i]))
columnIndex = i;
break;
// read lines to match row
while ((line = br.readLine()) != null)
String values = line.split(",");
if (rowName.equals(values[0]))
// return value at columnIndex
resultRow = values[columnIndex];
break;
br.close();
return resultRow;
public static void main(String args) throws IOException
String name = searchCsvLine("details.cvs", "name", "1");
System.out.println("name is " + name);
You should parse the first line of csv file to get column names.
public class Test
public static String searchCsvLine(String filename, String columnName, String rowName) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
// parse first line as column names
String line = br.readLine();
String columns = line.split(",");
int columnIndex = -1;
for (int i = 0; i < columns.length; i++)
if (columnName.equals(columns[i]))
columnIndex = i;
break;
// read lines to match row
while ((line = br.readLine()) != null)
String values = line.split(",");
if (rowName.equals(values[0]))
// return value at columnIndex
resultRow = values[columnIndex];
break;
br.close();
return resultRow;
public static void main(String args) throws IOException
String name = searchCsvLine("details.cvs", "name", "1");
System.out.println("name is " + name);
answered Nov 16 '18 at 8:28
KerieKerie
1015
1015
Thanks Kerie.. This worked for me..!!
– Bhoomika Datta
Nov 19 '18 at 4:21
add a comment |
Thanks Kerie.. This worked for me..!!
– Bhoomika Datta
Nov 19 '18 at 4:21
Thanks Kerie.. This worked for me..!!
– Bhoomika Datta
Nov 19 '18 at 4:21
Thanks Kerie.. This worked for me..!!
– Bhoomika Datta
Nov 19 '18 at 4:21
add a comment |
Try below :
public static String searchCsvLine(String filename, String searchString,String rowname) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
int searchStringIndex=0;
while ((line = br.readLine()) != null)
String values = line.split(",");
//get index of searchString
for (int i = 0; i < values.length; i++)
if(values[i].equalsIgnoreCase(searchString))
searchStringIndex=i;
break;
//get required rowname
for (int i = 0; i < Integer.parseInt(rowname); i++)
line=br.readLine();
//split the line found by rowname and get value as per searchString index (given column)
values = line.split(",");
resultRow=values[searchStringIndex];
break;
br.close();
System.out.println(resultRow);
return resultRow;
}
add a comment |
Try below :
public static String searchCsvLine(String filename, String searchString,String rowname) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
int searchStringIndex=0;
while ((line = br.readLine()) != null)
String values = line.split(",");
//get index of searchString
for (int i = 0; i < values.length; i++)
if(values[i].equalsIgnoreCase(searchString))
searchStringIndex=i;
break;
//get required rowname
for (int i = 0; i < Integer.parseInt(rowname); i++)
line=br.readLine();
//split the line found by rowname and get value as per searchString index (given column)
values = line.split(",");
resultRow=values[searchStringIndex];
break;
br.close();
System.out.println(resultRow);
return resultRow;
}
add a comment |
Try below :
public static String searchCsvLine(String filename, String searchString,String rowname) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
int searchStringIndex=0;
while ((line = br.readLine()) != null)
String values = line.split(",");
//get index of searchString
for (int i = 0; i < values.length; i++)
if(values[i].equalsIgnoreCase(searchString))
searchStringIndex=i;
break;
//get required rowname
for (int i = 0; i < Integer.parseInt(rowname); i++)
line=br.readLine();
//split the line found by rowname and get value as per searchString index (given column)
values = line.split(",");
resultRow=values[searchStringIndex];
break;
br.close();
System.out.println(resultRow);
return resultRow;
}
Try below :
public static String searchCsvLine(String filename, String searchString,String rowname) throws IOException
String resultRow = null;
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
int searchStringIndex=0;
while ((line = br.readLine()) != null)
String values = line.split(",");
//get index of searchString
for (int i = 0; i < values.length; i++)
if(values[i].equalsIgnoreCase(searchString))
searchStringIndex=i;
break;
//get required rowname
for (int i = 0; i < Integer.parseInt(rowname); i++)
line=br.readLine();
//split the line found by rowname and get value as per searchString index (given column)
values = line.split(",");
resultRow=values[searchStringIndex];
break;
br.close();
System.out.println(resultRow);
return resultRow;
}
answered Nov 16 '18 at 11:40
qaautodevqaautodev
19127
19127
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332909%2fread-a-particular-data-corresponding-to-a-row-column-in-csv%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
values.equals(searchString)
- you are comparing an array with a String– Kartik
Nov 16 '18 at 7:02
could you help me to resolve this
– Bhoomika Datta
Nov 16 '18 at 7:11
@BhoomikaDatta - You can use if condition like this, considering name is at fixed index on each line
if(values[1].equals(searchString)) resultRow = line; break;
– Amit Jain
Nov 16 '18 at 7:36
It still returns a null value
– Bhoomika Datta
Nov 16 '18 at 7:46
Convert array to list like by List<String> list = Arrays.asList(values); then use list.contains(searchString) instead
– Trung NT Nguyen
Nov 16 '18 at 8:49