Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting a two dimension array of strings from another class.

Posted on 2012-03-12
27
Medium Priority
?
391 Views
Last Modified: 2012-03-13
Hi,

The two dimensional array works OK from one class.

From class Oracle,

public static String strLine[]= new String[11]; // 11 fields - first, middle, last name, etc.
public static String[][] arryMain= new String[5][5]; // hold array of strLines
   // I can print out both records OK.
 
public static String[][] getArryMain(){
            return arryMain;
}


Getting the two dimensional array from another class,

      
            String[][] newLine = Oracle.getArryMain();
            for (int j=0;j<2; j++)  // There are only two records in this example
            {
                  System.out.print("Record Number: ");
                  System.out.println(j);
                  for (int k=0;k<11; k++)
                  {
                        System.out.println(newLine[j][k]);
                   }
            }
            
Record Number: 0
hi
M
Carver
33 Richards Street
Honolulu
...

Record Number: 1
hi
M
Carver
33 Richards Street
Honolulu
...

This show that both records have the same information.  The last record gets placed
in both positions.

Thanks.
0
Comment
Question by:taborrg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 7
  • 4
  • +1
27 Comments
 
LVL 30

Expert Comment

by:IanTh
ID: 37708947
I might be guessing here
but
try
for (int j=0;j<array.length; j++)
0
 
LVL 1

Author Comment

by:taborrg
ID: 37708969
IanTh,

Interesting test - it shows clearly that the first two records have been populated with the same information ( the data from the actual last record), and then prints out all the following records and shows null for their data - which is correct.

Record Number: 0
hi
M
Carver
33 Richards Street
Honolulu

Record Number: 1
hi
M
Carver
33 Richards Street
Honolulu

Record Number: 2
null
null
null
null

Record Number: 3
null
null
null
null

...
0
 
LVL 30

Expert Comment

by:IanTh
ID: 37709016
so is record 0 and record 1 the same ? as that suggests it is
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 1

Author Comment

by:taborrg
ID: 37709049
I think there's something wrong with the way I'm adding records to the two dimensional array.

It wind ups writing the last record to both positions.

 while (rset.next ())
                {
                      j++;
                    ... }
This loop reads in both records, but the last record gets written to both positions.

I think there's something wrong with this,

         arryMain[j] = strLine;  // each pass load new line array

Is that how to add a string[] array to a multidimensional [][] array?
0
 
LVL 1

Author Comment

by:taborrg
ID: 37709084
I added a third record and it writes the third record to all positions.

Inside the method that creates the two dimensional array, the array is fine, and prints out correctly.

Outside of that method it shows the last record in all positions that have any data (not null).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37709240
Can you please post the result of
System.out.println(java.util.Arrays.deepToString(Oracle.getArryMain()));

Open in new window

?
0
 
LVL 1

Author Comment

by:taborrg
ID: 37709283
Here it is - it shows the third record in all three locations.



[[Bob, M, Crown, 102 Orleans Ave, San Francisco, CA, 97745, 808-555-3324, bdeLuz@yahoo.com, Walmart, comments], [Bob, M, Crown, 102 Orleans Ave, San Francisco, CA, 97745, 808-555-3324, bdeLuz@yahoo.com, Walmart, comments], [Bob, M, Crown, 102 Orleans Ave, San Francisco, CA, 97745, 808-555-3324, bdeLuz@yahoo.com, Walmart, comments], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null]]
0
 
LVL 1

Author Comment

by:taborrg
ID: 37709293
If I print it within the method it shows all three records

[[Hillary, F, Carver, 22 Aardvark Avenue, Washington, DC, 10101, 800-212-3246, hcarver@gmail.com, Jamba Juice, comments], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null]]

[[hi, M, Carver, 33 Richards Street, Honolulu, HI, 96813, null, bcarver@hotmail.com, Liquor Commission, comments], [hi, M, Carver, 33 Richards Street, Honolulu, HI, 96813, null, bcarver@hotmail.com, Liquor Commission, comments], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null]]

[[Bob, M, Crown, 102 Orleans Ave, San Francisco, CA, 97745, 808-555-3324, bdeLuz@yahoo.com, Walmart, comments], [Bob, M, Crown, 102 Orleans Ave, San Francisco, CA, 97745, 808-555-3324, bdeLuz@yahoo.com, Walmart, comments], [Bob, M, Crown, 102 Orleans Ave, San Francisco, CA, 97745, 808-555-3324, bdeLuz@yahoo.com, Walmart, comments], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null]]
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37709296
OK - so the array is in arryMain (or before it) so please post the code for that
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37709308
If I print it within the method

What do you mean by 'within the method'?
0
 
LVL 1

Author Comment

by:taborrg
ID: 37709334
Within this method.  Here it shows all three records correctly.  Outside this method all the records show the same data, the last record's data (unless null, then is shows null).

      public static void sqlQuery()
      {
            try
             {
                  DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                  Connection conn = DriverManager.getConnection(dbUrl, userid, pwd);
                     Statement stmt = conn.createStatement ();
                ResultSet rset = stmt.executeQuery (strSQLQry);
                int j = -1;
               
               while (rset.next ())
               {
             
                      j++;
                      strFName = (rset.getString ("fname"));
                      strLine[0]=strFName;
                      strMName = (rset.getString ("mname"));
                      strLine[1]=strMName;
                      strLName = (rset.getString ("lname"));
                      strLine[2]=strLName;
                        strAddr = (rset.getString ("addr"));
                        strLine[3]=strAddr;
                      strCity = (rset.getString ("city"));
                      strLine[4]=strCity;
                      strState = (rset.getString ("state"));
                      strLine[5]=strState;
                      strZip = (rset.getString ("zip"));
                      strLine[6]=strZip;
                      strPhone = (rset.getString ("phone"));
                      strLine[7]=strPhone;
                      strEmail = (rset.getString ("email"));
                      strLine[8]=strEmail;
                      strCompany = (rset.getString ("cname"));
                      strLine[9]=strCompany;
                      strComments = (rset.getString ("comments"));
                      strLine[10]=strComments;
          
                      arryMain[j] = strLine;  // each pass load new line array
      
                      System.out.println(java.util.Arrays.deepToString(Oracle.getArryMain()));

          
                }
               
             //System.out.println(java.util.Arrays.deepToString(Oracle.getArryMain()));
                // Close up
                rset.close();
                stmt.close();
                conn.close();
            }
            catch (SQLException e)
            {
                  System.out.println("SQL Query Database error.");
            }
             
      }
0
 
LVL 1

Author Comment

by:taborrg
ID: 37709339
Here's where I declare the arrays.

public class Oracle  
{
      public static String[] strLine= new String[11];
      public static String[][] arryMain= new String[5][11];
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37709369
  j++;

Open in new window


You need to change that to

String[] strLine  = new String[11];
arryMain[j++] = strLine;

Open in new window


and get rid of
  arryMain[j] = strLine;  // each pass load new line array

Open in new window

further down. You are overwriting the same array in the second dimension each time
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37709388
Incidentally, a much easier way would be to do

http://technojeeves.com/joomla/index.php/free/68-resultset-to-nested-list
0
 
LVL 1

Author Comment

by:taborrg
ID: 37709415
Could you tell me where I am overwriting the array in the second dimension?

I made these changes,

String[] strLine  = new String[11];  (which is how it was, left it.)
arryMain[j++] = strLine;                (added this)
       
and deleted this - arryMain[j] = strLine;  // each pass load new line array          

and got this result:

[[Hillary, F, Carver, 22 Aardvark Avenue, Washington, DC, 10101, 800-212-3246, hcarver@gmail.com, Jamba Juice, comments], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null]]
[[hi, M, Carver, 33 Richards Street, Honolulu, HI, 96813, null, bcarver@hotmail.com, Liquor Commission, comments], [null, null, null, null, null, null, null, null, null, null, null], [hi, M, Carver, 33 Richards Street, Honolulu, HI, 96813, null, bcarver@hotmail.com, Liquor Commission, comments], [null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null]]
[[Bob, M, Crown, 102 Orleans Ave, San Francisco, CA, 97745, 808-555-3324, bdeLuz@yahoo.com, Walmart, comments], [null, null, null, null, null, null, null, null, null, null, null], [Bob, M, Crown, 102 Orleans Ave, San Francisco, CA, 97745, 808-555-3324, bdeLuz@yahoo.com, Walmart, comments], [null, null, null, null, null, null, null, null, null, null, null], [Bob, M, Crown, 102 Orleans Ave, San Francisco, CA, 97745, 808-555-3324, bdeLuz@yahoo.com, Walmart, comments]]
Record Number: 0
Bob
M
Crown
102 Orleans Ave
San Francisco
CA
97745
808-555-3324
bdeLuz@yahoo.com
Walmart
comments
Record Number: 1
null
null
null
null
null
null
null
null
null
null
null
Record Number: 2
Bob
M
Crown
102 Orleans Ave
San Francisco
CA
97745
808-555-3324
bdeLuz@yahoo.com
Walmart
comments
Record Number: 3
null
null
null
null
null
null
null
null
null
null
null
Record Number: 4
Bob
M
Crown
102 Orleans Ave
San Francisco
CA
97745
808-555-3324
bdeLuz@yahoo.com
Walmart
comments
0
 
LVL 36

Expert Comment

by:mccarl
ID: 37709422
I will also throw in my 2 cents... This is Java, an "object" oriented language, maybe you should think about using an object rather than the 2nd dimension of that array, ie. an array of objects, where the object represents each person.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 37709439
String[] strLine  = new String[11];  (which is how it was, left it.)

Open in new window


No, that's NOT how it was. You were filling

 public static String[] strLine= new String[11];

Open in new window


(the same array) for every row
0
 
LVL 1

Author Comment

by:taborrg
ID: 37709491
I made the correction:

      public static void sqlQuery()
      {
            String[] strLine  = new String[11];  // Where it belongs


Still no help.  Same results as when it was private static.

Thanks
0
 
LVL 1

Author Comment

by:taborrg
ID: 37709502
BTW - strLine gets filled with new data each time through, and then it gets added to arryMain as a new element.

Then it gets new data on the next pass, etc.
0
 
LVL 1

Author Comment

by:taborrg
ID: 37709509
I think I'm getting it -  strLine cannot be, there needs to be 11 strLines.

srtline1[]

etc.

Thanks!
0
 
LVL 1

Author Comment

by:taborrg
ID: 37709515
I was treating strLine as if it were a variable instead of an object.

We learn.


Thanks!
0
 
LVL 1

Author Closing Comment

by:taborrg
ID: 37709525
Appreciate it!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37709568
As i mentioned, you'd be better using the code at the link i posted, but your code can be made more solid and simpler by something like

    public static void sqlQuery() {
        Connection conn = null;
        ResultSet rset = null;
	Statement stmt = null;

	try {
	    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

	    conn = DriverManager.getConnection(dbUrl, userid, pwd);

	    Statement stmt = conn.createStatement();
	    rset = stmt.executeQuery(strSQLQry);

	    final int NUM_COLS = 11;
	    int row = 0;

	    while (rset.next()) {
		String[] strLine = new String[11];
		arryMain[row++] = strLine;
		for(int col = 1;col <= NUM_COLS;col++) {
		    strLine[col - 1] = rset.getString(col);
		}
	    }
	} catch (SQLException e) {
	    System.out.println("SQL Query Database error.");
	} finally {
	    if (stmt != null) {
		try {
		    stmt.close();
		} catch (SQLException e) { /* ignore */
		}
	    }

	    if (rset != null) {
		try {
		    rset.close();
		} catch (SQLException e) { /* ignore */
		}
	    }

	    if (conn != null) {
		try {
		    conn.close();
		} catch (SQLException e) { /* ignore */
		}
	    }
	}
    }

Open in new window

0
 
LVL 1

Author Comment

by:taborrg
ID: 37709601
Awesome!

My program is working with that fix  -  all records show where they are supposed to.

Can't thank you enough!
0
 
LVL 30

Expert Comment

by:IanTh
ID: 37710383
didn'y my first answer point in the correct way to fix this I am not saying I need the points this month but other months any points help I hope you understand this comment
0
 
LVL 1

Author Comment

by:taborrg
ID: 37712658
IanTh,

I'm sorry for the confusion, but your post only verified what I was already saying - namely that the same information was populating all records with any data in them.

"I might be guessing here
but
try
for (int j=0;j<array.length; j++)"

It showed the same outputs I was already working with, but added the null records to the view.  This didn't really move me towards a solution.

I very much appreciate your help, but my understanding is that posts need to provide resolutions more than amplification.

Best of luck,

Richard
0
 
LVL 30

Expert Comment

by:IanTh
ID: 37714094
not a problem like I said sometimes its difficultto get 3000 points a month to keep my free access going thats all
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

715 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