Avatar of taborrg
taborrg
 asked on

Getting a two dimension array of strings from another class.

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.
Java

Avatar of undefined
Last Comment
IanTh

8/22/2022 - Mon
IanTh

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

ASKER
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

...
IanTh

so is record 0 and record 1 the same ? as that suggests it is
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
taborrg

ASKER
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?
taborrg

ASKER
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).
CEHJ

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

Open in new window

?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
taborrg

ASKER
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]]
taborrg

ASKER
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]]
CEHJ

OK - so the array is in arryMain (or before it) so please post the code for that
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
CEHJ

If I print it within the method

What do you mean by 'within the method'?
taborrg

ASKER
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.");
            }
             
      }
taborrg

ASKER
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];
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CEHJ

  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
CEHJ

Incidentally, a much easier way would be to do

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

ASKER
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
Your help has saved me hundreds of hours of internet surfing.
fblack61
mccarl

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.
ASKER CERTIFIED SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
taborrg

ASKER
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
taborrg

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
taborrg

ASKER
I think I'm getting it -  strLine cannot be, there needs to be 11 strLines.

srtline1[]

etc.

Thanks!
taborrg

ASKER
I was treating strLine as if it were a variable instead of an object.

We learn.


Thanks!
taborrg

ASKER
Appreciate it!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
CEHJ

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

taborrg

ASKER
Awesome!

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

Can't thank you enough!
IanTh

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
taborrg

ASKER
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
IanTh

not a problem like I said sometimes its difficultto get 3000 points a month to keep my free access going thats all