The Artima Developer Community
Sponsored Link

Java Answers Forum
How do i prevent [] from appearing in output when reading vector data?

4 replies on 1 page. Most recent reply: Apr 8, 2002 6:36 AM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Barry Dogger

Posts: 7
Nickname: phoenix
Registered: Apr, 2002

How do i prevent [] from appearing in output when reading vector data? Posted: Apr 4, 2002 6:42 AM
Reply to this message Reply
Advertisement
Here's the code:

public void saveFile(File file)
{
try
{
PrintWriter out = new PrintWriter(
new FileOutputStream(file),true);
Vector line = null;
int i =0;
while (i < data.size())
{
line = (Vector)data.get(i);
System.out.println("What is line ? " +line);
i++;
System.out.println("Before write ");
out.println(line);
System.out.println("After write ");
}
out.close();
}
Here is the result of the line:System.out.println("What is line ? " +line);

[Name , adres , Zip code, City , Phone , email]

I do not want to write the [ ] to my output file, because they appear in my table when i read the file again.

Thanks in advance.

regards,
Barry


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: How do i prevent [] from appearing in output when reading vector data? Posted: Apr 4, 2002 7:22 AM
Reply to this message Reply
YOu have two options.
1. Before writing your vector content to file/database strip the brackets as follows.
 Vector v = new Vector ( );
  // add element s here
  ..................
  
 // Get the string representation of vector with []
  String str = v.toString()
  
// Now stript the [ ]
 str = str.substring(1, str.length() -1 );
 
// Now store str in database and str won't have []

2. Create your own Vector class as MVector in following example. Override toString method of MVector and return the string representation of Vector stripping the brackets.
Choose one of the above methods, which suits your requirement.
Thanks
Kishori
//////////////////////////////Test.java /////
import java.util.* ;
 
class MVector extends Vector {
	public String toString() {
		String str = super.toString() ;
		str = str.substring ( 1, str.length() - 1 ) ;
		return str;
	}
}
 
class Test {	
 
	public static void main ( String[] args ) throws Exception {
		MVector v = new MVector() ;
		
		v.add ( "Kishori" ) ;
		v.add ( "Shetty" ) ;
		v.add ( "Hello" ) ;
		
		System.out.println ( v ) ;
}
}

Barry Dogger

Posts: 7
Nickname: phoenix
Registered: Apr, 2002

Re: How do i prevent [] from appearing in output when reading vector data? Posted: Apr 4, 2002 11:42 PM
Reply to this message Reply
Hi Kishori,

Thanks a lot for your suggestion. I tried them both and I got both examples working. Thanks a lot.

Barry

Barry Dogger

Posts: 7
Nickname: phoenix
Registered: Apr, 2002

Re: How do i prevent [] from appearing in output when reading vector data? Posted: Apr 8, 2002 2:21 AM
Reply to this message Reply
New problem occurs.

line = (Vector)data.get(i);
results in:
[Name, Adress, Zip, City, Phone, Mail]

Removing [ and ] works just fine.
But between every tow columns, it adds a space. So after saving and reloading, 2 spaces occur.

How can I remove the spaces?

Thanks in advance!
Barry

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: How do i prevent [] from appearing in output when reading vector data? Posted: Apr 8, 2002 6:36 AM
Reply to this message Reply
The toString() of vector class embeds a comma and a space between two elements. If you want to take out that space and leave comma there then you have two options.
1. When you are overriding toString() method of Vector class, loop thru all elements and then construct the string as follows.
class MVector extends Vector {
	public String toString() {
		StringBuffer sb = new StringBuffer();
		int total = this.size() ;
		for (int i = 0; i < total ; i++) {
		    sb.append( String.valueOf ( this.get( i ) ) ) ;
		    if ( i < total - 1 ) {
				// Append only a comma
				sb.append(",");
			}
		}
				
		return sb.toString();
	}
}


2. Write a method in your class thaat takes a Vector as an argument and returns its string representation in the format you want as:
public String getString ( Vector v ) {
		if ( v == null ) {
			return "null" ;
		}
		
		StringBuffer sb = new StringBuffer();
		
		// Get the total no of elements in vector
		int total = v.size() ;
		
		// Loop thru all elements
		for ( int i = 0; i < total ; i++ ) {
			
			// Append the element in string buffer
		    sb.append( String.valueOf ( v.get(i))) ;
			
		    if ( i < total - 1 ) {
				// Append only a comma
				sb.append(",");
			}
		}
				
		return sb.toString();		
		
	}

Flat View: This topic has 4 replies on 1 page
Topic: i need help please Previous Topic   Next Topic Topic: GUI help needed

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use