The Artima Developer Community
Sponsored Link

Java Answers Forum
Why does do I see spaces?

4 replies on 1 page. Most recent reply: Apr 8, 2002 11:09 AM by Barry Dogger

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

Why does do I see spaces? Posted: Apr 8, 2002 6:05 AM
Reply to this message Reply
Advertisement
Here is the part of load file what seems to contain the problem:

String line;
while ( (line = bufferedReader.readLine()) != null)
{
Vector aLine = new Vector();
aLine.add(stringToVector(line));
System.out.println("what is aLine " +aLine );
data.addAll(aLine);
}

System.out.println("what is aLine " +aLine ) output:

First Lastname, Street Nr., zip code, City, phone, mail

I do not want to have the space between the elements. I want it to look like:
First Lastname,Street Nr.,zip code,City,phone,mail


Tokenizer I use :

public static Vector stringToVector(String string)
{
StringTokenizer tokenizer =
new StringTokenizer(string, ",", false);
Vector row = new Vector();
while(tokenizer.hasMoreTokens())
{
String valueOfToken = tokenizer.nextToken();
row.addElement(valueOfToken);
}
return row;
}


[[Willy Dogger, Wilgenhoek 51, 7681 RR, Vroomshoop, 0546-644539, w.dogger@home.nl]]


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Why does do I see spaces? Posted: Apr 8, 2002 6:30 AM
Reply to this message Reply
Willy,

You code does not seem too clean / clear, neither your question !
Could you reformulate ?
Also, in the code, and w/o knowing much about the thing you want to do the 4 th line seems really strange !
String line;                                        // 1
while ( (line = bufferedReader.readLine()) != null) // 2
{                                                   // 3
  Vector aLine = new Vector();                      // 4
  aLine.add(stringToVector(line));                  // 5
  System.out.println("what is aLine " +aLine );     // 6
  data.addAll(aLine);                               // 7
}                                                   // 8


Also I wonder why you did not created your own Vector class. It seems that you could take a lot more out of it when implementing your own toString() method, like what follows !

public class MyCustomVector 
  extends java.util.Vector
{
 
  
...
 
 
public String toString()
{
  synchronize(this)
  {
    Object[] o = this.toArray(); 
    // Check if this is fast enough or may be you could try to access the 
   // protected  Object[] elementData of the java.util.Vector
  } // End of synchronization
  StringBuffer buf = new StringBuffer();
 
  // This is not excatly what you want but 
  // removing the last comma should not be an issue
  //
  for (int i = 0; i < o.length; i++)
    buf.append(o[i]).append(',');
  // Please not that the forum has a problem.
  // It dislays comparison (greater/smaller) signs around the "i" 
  // They are in fact square brackets, instead !!!
  return buf.toString();
}



Rgds,

Thomas,
tsmets @ lautre . net

Barry Dogger

Posts: 7
Nickname: phoenix
Registered: Apr, 2002

Re: Why does do I see spaces? Posted: Apr 8, 2002 7:33 AM
Reply to this message Reply
Hi Thomas,

I will start with (trying to) clarifying the code.

String line; // I need a string to read a line from the file.

while ( (line = bufferedReader.readLine()) != null) {
// I need to convert the string to a Vector to
// prevent casting problems in getValueAt() method.
Vector aLine = new Vector();

// Tokenizer to convert the string to vector
aLine.add(stringToVector(line));

// I need to add the vector to a Vector
data.addAll(aLine);
}

I am trying to have a JTable which can read data from file, add rows, and save the entire table to a file.

However, somewhere in the converting of string to vector and adding this to a vector, it adds a space between every column which I do not want.

Creating my own vector class was something I did not think off. I am pretty new to Java and I am not used to override methods from existing classes, partly cause I am not sure I can do that. But I will give it a go.

Hope this clarifies somewhat.

Thanks for your answer and suggestion.

Grtx,
Barry
(I forgot to mention my name the first time. Willy was just some sample data for my JTable.)

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Why does do I see spaces? Posted: Apr 8, 2002 8:38 AM
Reply to this message Reply
And changing the tokenizer to this ?

public static Vector stringToVector(String string)
{
  // can't you give a char ',' instead of a String ","
  StringTokenizer tokenizer = new StringTokenizer(string, ",", false);
  Vector row = new Vector();
  while(tokenizer.hasMoreTokens())
  {
    String valueOfToken = tokenizer.nextToken();
    // trimming the String before adding it ?
    row.addElement(valueOfToken.trim());
  }
  return row;
}


Thomas,

could you give a look at : http://www.artima.com/forums/howtopost.html because it makes posting much easier to read !

Barry Dogger

Posts: 7
Nickname: phoenix
Registered: Apr, 2002

Re: Why does do I see spaces? Posted: Apr 8, 2002 11:09 AM
Reply to this message Reply
Hi Thomas,

Of course! I tried the trim(). Works perfect.
I'll try using a char later on.

Thanks for the tip.

I also looked at see:
http://www.artima.com/forums/howtopost.html

I am sorry. I did not know how it worked. I just came across this forum last friday.

Thanks a lot for your help.

Barry

Flat View: This topic has 4 replies on 1 page
Topic: Word Frequency Application Previous Topic   Next Topic Topic: Customized HTMLEditorKit

Sponsored Links



Google
  Web Artima.com   

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