• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

cannot read array value

I have my custom code to retrieve the data in main method from text file as below
      String strContent = BufferStor.retrieveData("C:\\Test.txt");
      String [] rows = strContent.split(genProp.getString("row.seperator"));
then there is method call main method  i am calling a method 'checkDatabases' like below where rows is single dimentional string array that came from delimited pipe separated | text file

checkDatabases(rows);

method implementation is like this

private static void checkDatabases(String[] rows) {

where I am able to read all array values
using


System.out.println("rows--->"+Arrays.deepToString(rows));

rows--->999|5555|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
998|5554|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
997|5553|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
996|5552|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH



but when I read the individual values like below

String yourHelloString = rows[0][0];
      String yourWorldString = rows[0][1];
      String yourTGoodbyeString = rows[1][0];
      String yourPlanetString = rows[1][1];

getting error as
The type of expression must be an array type but it resolved to string.

I need to read these values and send send it to query to check againest database.
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance




>>>System.out.println("rows--->"+Arrays.deepToString(rows));
is already giving following  required values to me in console.

rows--->999|5555|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
998|5554|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
997|5553|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
996|5552|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH

All i need to do is to get only first two values of each row and i need to pass them to datase query either directly or send to collection object and collect from it  whichever is simpler and easier

999 5555
998 5554
997 5553
996 5552

like below

ResultSet rs1 = stmt.executeQuery("select * from login where "  + usernameColumn + "='"+yourHelloString +"' and " + passwordColumn + "='"+yourWorldString +"'");

ResultSet rs1 = stmt.executeQuery("select * from login where "  + usernameColumn + "='"+yourTGoodbyeString +"' and " + passwordColumn + "='"+yourPlanetString +"'");


I need to put in some for loop to get and query with each and every line record.Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
0
gudii9
Asked:
gudii9
3 Solutions
 
CEHJCommented:
I have my custom code to retrieve the data in main method from text file as below
      String strContent = BufferStor.retrieveData("C:\\Test.txt");
      String [] rows = strContent.split(genProp.getString("row.seperator"));

Then that custom code is incorrect. It should be returning String[][] or List<List<String,>> (whether or not generics are being used isn't important - that's to tell you how the nested List is composed) and i'm 99% certain i've already posted code that does this
0
 
Amitkumar PSr. ConsultantCommented:
Add the following snippet in the checkDatabase() method.

for (int i=0; i<rows.length; i+=2) {

String[] cols1 = rows[i].split(genProp.getString("|"));
String[] cols2 = rows[i+1].split(genProp.getString("|"));

String yourHelloString = cols1[0];
String yourWorldString = cols1[1];
String yourTGoodbyeString = cols2[0];
String yourPlanetString = cols2[1];


// TODO ADD PRINT STATEMENTS FOR VERIFICATION



// TODO ONCE VERIFIED, ADD THE SQL STATEMENTS



}

Open in new window

0
 
Pramod KumarCommented:
String [] rows = strContent.split(genProp.getString("row.seperator"));
Above 'rows' is an single dimension array and to get an element from here you should use like:
String item1 = rows[0];
String item2 = rows[1];

But actually you are treating 'rows' as double dimension array to read as:
String yourHelloString = rows[0][0];
This is not correct.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now