Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

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.
0
taborrg
Asked:
taborrg
  • 15
  • 7
  • 4
  • +1
1 Solution
 
IanThCommented:
I might be guessing here
but
try
for (int j=0;j<array.length; j++)
0
 
taborrgAuthor Commented:
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
 
IanThCommented:
so is record 0 and record 1 the same ? as that suggests it is
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
taborrgAuthor Commented:
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
 
taborrgAuthor Commented:
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
 
CEHJCommented:
Can you please post the result of
System.out.println(java.util.Arrays.deepToString(Oracle.getArryMain()));

Open in new window

?
0
 
taborrgAuthor Commented:
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
 
taborrgAuthor Commented:
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
 
CEHJCommented:
OK - so the array is in arryMain (or before it) so please post the code for that
0
 
CEHJCommented:
If I print it within the method

What do you mean by 'within the method'?
0
 
taborrgAuthor Commented:
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
 
taborrgAuthor Commented:
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
 
CEHJCommented:
  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
 
CEHJCommented:
Incidentally, a much easier way would be to do

http://technojeeves.com/joomla/index.php/free/68-resultset-to-nested-list
0
 
taborrgAuthor Commented:
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
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
 
CEHJCommented:
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
 
taborrgAuthor Commented:
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
 
taborrgAuthor Commented:
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
 
taborrgAuthor Commented:
I think I'm getting it -  strLine cannot be, there needs to be 11 strLines.

srtline1[]

etc.

Thanks!
0
 
taborrgAuthor Commented:
I was treating strLine as if it were a variable instead of an object.

We learn.


Thanks!
0
 
taborrgAuthor Commented:
Appreciate it!
0
 
CEHJCommented:
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
 
taborrgAuthor Commented:
Awesome!

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

Can't thank you enough!
0
 
IanThCommented:
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
 
taborrgAuthor Commented:
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
 
IanThCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 15
  • 7
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now