Vel Eous
asked on
arrayList toString
I have an arrayList which is populated from a text file.
I then change certain arrayList index elements .remove() and .add(i, newString). At the end of that I am still left with an arrayList.
What I am looking to do is turn that arrayList back into a string so it can be displayed on screen in a JTextArea before being saved back to a text file.
I did try:
String str = arrayList.toString();
jtextAreaOutput.setText(st r);
But that produced:
[element1, element2, etc]
How can this be resolved ?
Thanks,
I then change certain arrayList index elements .remove() and .add(i, newString). At the end of that I am still left with an arrayList.
What I am looking to do is turn that arrayList back into a string so it can be displayed on screen in a JTextArea before being saved back to a text file.
I did try:
String str = arrayList.toString();
jtextAreaOutput.setText(st
But that produced:
[element1, element2, etc]
How can this be resolved ?
Thanks,
It all depends on what format you wish to display the contained elements. Seems like you are not satisfied with the default implementation of ArrayList. All you have to do is iterate over the list and format the retrieved elements.
For eg, if you need to display the strings as CSV
String csv = "";
for(int i=0; i < list.size();i++){
csv += (String)list.get(i)+",";
}
if(csv.length() > 1){
csv += csv.substring(0, csv.length());
}
For eg, if you need to display the strings as CSV
String csv = "";
for(int i=0; i < list.size();i++){
csv += (String)list.get(i)+",";
}
if(csv.length() > 1){
csv += csv.substring(0, csv.length());
}
try overloading the ArrayList's toString() method.
if you have edited remove(), add(), etc, try to look for a toString()
If none, then simply write one in the class.
if you have edited remove(), add(), etc, try to look for a toString()
If none, then simply write one in the class.
ASKER
OK, I attemped what you said:
public void find() {
String find = Jaypad.searchString;
String tempString = null;
int x = theFile.size();
for (int i=0; i<(x); i++) {
if (theFile.get(i).equals(fin d)) {
theFile.remove(i);
theFile.add(i, "REPLACED");
tempString = theFile.toString().replace All(",", "");
System.out.println(tempStr ing);
} else {
Jaypad.jlblStatus.setText( "Not found");
}
}
Jaypad.jtextAreaDisplay.se tText(temp String);
Jaypad.jlblStatus.setText( find);
}
^^ works fine, removes the "," from between array elements. When I attempt to replace all "[" or "]" symbols though, I get a runtime error.
public void find() {
String find = Jaypad.searchString;
String tempString = null;
int x = theFile.size();
for (int i=0; i<(x); i++) {
if (theFile.get(i).equals(fin
theFile.remove(i);
theFile.add(i, "REPLACED");
tempString = theFile.toString().replace
System.out.println(tempStr
} else {
Jaypad.jlblStatus.setText(
}
}
Jaypad.jtextAreaDisplay.se
Jaypad.jlblStatus.setText(
}
^^ works fine, removes the "," from between array elements. When I attempt to replace all "[" or "]" symbols though, I get a runtime error.
>> When I attempt to replace all "[" or "]" symbols though, I get a runtime error.
It's just a simple substring operation. Can you post the code you tried?
It's just a simple substring operation. Can you post the code you tried?
ASKER
Its the code above but replacing :
tempString = theFile.toString().replace All(",", "");
with:
tempString = theFile.toString().replace All("[", "");
tempString = theFile.toString().replace
with:
tempString = theFile.toString().replace
String newText = new StringBuilder(arrayList.to String().r eplaceAll( ",", " ")).deleteCharAt(newText.l ength() - 1).deleteCharAt(0).toStrin g();
StringBuffer sb = new StringBuffer() ;
for( String s : arrayList )
sb.append( s ) ;
I'm probably missing the point though... ;-)
for( String s : arrayList )
sb.append( s ) ;
I'm probably missing the point though... ;-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What was the exception you got and could you print its stack trace?
ASKER
I went about it a different way in the end. Used a string buffer.
You can also try StringBuilder if you are using Java 5.0 or 6.0
StringBuilder is to be preferred over StringBuffer if you can use it
>> I went about it a different way in the end. Used a string buffer.
My example used a StringBuffer... Did you go that way then?
My example used a StringBuffer... Did you go that way then?
Looks like ;)
ASKER
Sorry, not feeling well and I`m not taking things in very well at the moment. I've ended up with the following which outputs the arrayList minus all that array stuff ( [ , ] etc ):
// search & replace all method
public void findAndReplaceAll() {
String find = Jaypad.searchString;
int x = theFile.size();
Object obj = null;
StringBuilder strBuild = new StringBuilder();
for (int i=0; i<x; i++) {
if (theFile.get(i).equalsIgno reCase(fin d)) {
theFile.set(i, "REPLACED");
System.out.println(theFile );
obj = theFile.get(i);
strBuild.append(obj + " ");
} else {
obj = theFile.get(i);
strBuild.append(obj + " ");
}
}
String result = strBuild.toString();
Jaypad.jtextAreaDisplay.se tText(resu lt);
}
Thing is my array appears to be skipping over elements that match my search criteria. :/
// search & replace all method
public void findAndReplaceAll() {
String find = Jaypad.searchString;
int x = theFile.size();
Object obj = null;
StringBuilder strBuild = new StringBuilder();
for (int i=0; i<x; i++) {
if (theFile.get(i).equalsIgno
theFile.set(i, "REPLACED");
System.out.println(theFile
obj = theFile.get(i);
strBuild.append(obj + " ");
} else {
obj = theFile.get(i);
strBuild.append(obj + " ");
}
}
String result = strBuild.toString();
Jaypad.jtextAreaDisplay.se
}
Thing is my array appears to be skipping over elements that match my search criteria. :/
>> theFile.set(i, "REPLACED");
Should this not be done after: >> obj = theFile.get(i);
Should this not be done after: >> obj = theFile.get(i);
ASKER
Why though ?
I want to replace the element, then send the new element to the StringBuilder. If put obj = theFile.get(i); before I replace the element I`m just sending the old element to the StringBuilder am I not ?
I want to replace the element, then send the new element to the StringBuilder. If put obj = theFile.get(i); before I replace the element I`m just sending the old element to the StringBuilder am I not ?
I thought you wanted to append the old ones, sorry. But in your code, the new element is always the String "REPLACED" right? 'theFile' is the ArrayList?
ASKER
yes.
Not sure I deserved all the points here...
ASKER
Well I went with your solution ... ? :P
What is the output you expect and what is the output you get?
ASKER
It's cool, I resolved the issue using the StringBuilder method.
arrayList.toString().replace('[',' ').replace(']',' ').replaceAll(" ","")
ta.setText(arrayList.toStr
You want to remove the '[' and ']'