Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings and arrays

 
Clive van Hilten
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks
I've searched the Saloon postings and can't seem to find an answer to this problem.
I have a multidimensional array created as follows:
String accountValues[][] = coordinator.getAvailableValues("/RTHISTORY/coAccount");
This array prints out something like this:
Current account
9911-90876 //this is the bank account number
USD //currency reference
I need to drop the 'USD' from the output. I thought of doing this by casting the array to a String, getting the length of the String using the length() method, then using the substring(int beginIndex, int endIndex) method to create a new String.
My problem is that I cannot convert the array to a String.
Please could someone come up with a useful suggestion(s).
Thanks.
Clive
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The array defined is a String array, I think you don�t have to convert its values to string
Could you put a bigger code snippet showing how do you print out those values?
 
Clive van Hilten
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is, even though the array is a *String* array, I cannot use the length() or substring() methods available to the String class. If I try
int stringLength = accountValues.length();
System.out.println("stringLength is " + stringLength);
I get this error message: 'Method length() not found in class java.lang.Object' . The reason for this error message is that accountValues is an array, and the length() method is not available to all Objects per se, only to objects of the String class.
[ April 08, 2002: Message edited by: Clive van Hilten ]
[ April 08, 2002: Message edited by: Clive van Hilten ]
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clive
What you are getting is basically an array of String arrays. Each element of accountValues is an array of Strings.
You need to access individual Strings contained in one of the secodary arrays. The structure looks like this:
accountValues[0]: {"string1", "string2", string3"}
accountValues[1]: {"string4", "string5", string6"}
accountValues[2]: {"string7", "string8", string9"}
accountValues[3]: {"string10", "string11", string12"}
So the string at accountValues[2][0] would be 'string7'.
Once you've accessed an individual String you can assign it to a varialbe and then use any of the standard String methods on it.
If each of arrays contains some type of account information and if each array is the same then you can just loop through each of the elements of accountValues and then access the appropriate element of the subArray and do what you need to do. If they aren't the same then you have to look at each element of each array and see if the String you're looking for is in it.
Sorry if that was too much of a basic explaination..
[ April 08, 2002: Message edited by: Dave Vick ]
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this example:
public class myClass {
public static void main (String [] args){
// array inicialization
String myArray[][] = {{"1","2","3"},
{"4","5","6"}};
// for referencing one position in this array
// having myArray[index1][index2]
// we have this coords respectively:
// (0,0) (0,1) (0,2)
// (1,0) (1,1) (1,2)

// let�s print out the sixth element�s length()
System.out.println(String.valueOf(myArray[1][2].length()));
// let�s print out the sixth element�s value
System.out.println(myArray[1][2]);
}
}
[ April 08, 2002: Message edited by: Sigfred Zamo ]
 
Clive van Hilten
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Dave, this is exactly what I was after.
Best wishes,
Clive.
 
Clive van Hilten
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks also to Sigfred - I see what you are getting at ...
Clive
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic