Solved

Getting a two dimension array of strings from another class.

Posted on 2012-03-12
27
354 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
  • 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 35

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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now