• Post Reply Bookmark Topic Watch Topic
  • New Topic

JCombo Box Problem  RSS feed

 
Robert Richardson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a combo box and filled it with a vector of Strings. They are displayed with the spaces removed in the box. I can print the same vector in the command window and it displays correctly. I'm sure its' something simple but I can't track down the problem. Thanks for any suggestions.
Robert
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with the spaces removed
Do you mean the spaces between the words in the sentences? I tried a test and the word spacing seems to be preserved okay in j2se 1.5.0_06.
 
Robert Richardson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig:
Your code works well.
Here is the essence of mine:

s = new Scanner(new BufferedReader(new FileReader("threads.txt")));
s.useDelimiter("\\r");

while (s.hasNext()) {
data =s.next();
System.out.println(data);
line = line + data;
thdlist.add(data);
System.out.println(data);

A line of data in threads.txt looks like:
1/ 4- 20UNC.25020
It is displayed in the box as
1/420UNC.25020
and in the comand window like
1/ 4- 20UNC.25020

It must be the way I'm using the add method.

I'm an old dog trying to learn new tricks.
I really appreciate your help.
Robert
 
Campbell Ritchie
Marshal
Posts: 56227
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up the java.util.Scanner class in the API documentation. Look at the second paragraph after implements Iterator<String>. There it tells you how a Scanner divides up its input, using a regular expression, which you can find more details about in this part of the java tutorial. Note what the default regular expression is. What the bit about using a delimiter means is that the delimiter is missed out from the text read. If you use the next() method of java.util.Scanner, you get a token. If you want the whole line you would have to use the nextLine() method.
 
Robert Richardson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig:
My problem isn't with the Combo Box. It appears to be related to the way the scanner class works. If I read a line from the text file into a String and then check the String's length it is shorter than the it should be. System.out prints the line as it appears in the file but if I add it to the Combo Box it is displayed in the compressed form. This forum is the wrong place for this question. Thanks for your help. If I can't figure this out I'll move to the appropriate forum.
 
Campbell Ritchie
Marshal
Posts: 56227
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posted by Robert Richardson
My problem isn't with the Combo Box. It appears to be related to the way the scanner class works.
Exactly. As I said before, when you use the Scanner, it misses out the delimiter. Go through the API about Scanner and that ought to explain your problem.

It isn't reading the line shorter than it should be. It has its own way of reading the line and that way happens to be shorter than the line you have in your text file.
Try something like this:That should print out each token with --- before and after, so you can see the lengths of tokens the Scanner actually reads.
That ought to explain to you where your problem lies.
 
Campbell Ritchie
Marshal
Posts: 56227
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It obviously isn't using the "\\r" as a delimiter when separating tokens. I don't think "\\r" is actually used anywhere. Unix uses \n as a line terminator, and Windows uses \r\n. Try the readLine method instead.
 
Campbell Ritchie
Marshal
Posts: 56227
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I have misunderstood the problem. I thought at first you weren't familiar with the delimiter problem.
I still think it is the delimiter. Maybe the \\ turns it into a String reading \r rather than the return character. I have had other methods (eg String.split()) fail when they meet unfamiliar delimiters.
Try different delimiters.
Try String.format("%n") as a delimiter. Or String.format("[%n]"). Or "\r". Or "[\r]". See whether they make any difference. Those suggestions won't all work, I am sure, particularly [%n].

CR
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems to work okay

reading this comboVector.txt file.
 
Robert Richardson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig:
I'm sure ! I created my file in Excel. I should have looked at it with a Hex Editor. There must be some extra stuff in it messes with scanner and readLine.
Thanks to everyone for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!