[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

cannot read array value

Posted on 2012-09-12
3
Medium Priority
?
545 Views
Last Modified: 2012-09-17
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
Comment
Question by:gudii9
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 668 total points
ID: 38391822
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
 
LVL 21

Assisted Solution

by:Amitkumar P
Amitkumar P earned 664 total points
ID: 38393600
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
 
LVL 10

Assisted Solution

by:Pramod Kumar
Pramod Kumar earned 668 total points
ID: 38394389
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month18 days, 9 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question