Okay, First and foremost im new to the forum. Hi and Thx to anyone who helps :D

Anyhow, i would like to ask you guys if it is possible to separate a String with tokens one by one. Then make them appear in the jTextField with only 1 word per field.

Lets say in Customer.txt i got the following
test asdf asdf

then i would like em to appear(3 separate textfields):
TextField: test
Textfield: asdf
textfield: asdf

BufferedReader reader;
               try{
                   reader = new BufferedReader(new FileReader("Customer.txt"));
                   
                   String strLine = reader.readLine();
                   StringTokenizer st = new StringTokenizer(strLine);
                   jTextField1.read(reader, st);
                    

                   reader.close();

               }
               catch(FileNotFoundException ex){
               }
               catch(IOException ex){

               }

Above is my code to make it appear in the jTextField. But what i get instead is the whole line inside the textfield which is

TextField: test asdf asdf

instead of

TextField: Test

Help??? :D

StringTokenizer st = new StringTokenizer(strLine,/*add the delim here,i ur case,a space?*/);
StringTokenizer st = new StringTokenizer(strLine," ");

with this, you'll have 3 tokens...you might want to do a loop and use .nextToken() to access every tokens

Thank you!! Found out how to work it out!

It seems with For Loop is better than doing while loop. Is it possible to define the delim with \t or \n? Or is it just for normal Spaces.

If i want to do

Customer.txt =
Firstname Lastname Address Email
How do i get the first 2 part of the string into 1 field ?

Textfield1: John Doe
TextField2: Japan
Textfield3: Test@Testing.com

Ive tested with Split() and it seemed to work but it only works towards the end of the array

String strLine = reader.readLine() ;
                   String[] words = strLine.split(" ",3);

the first 2 textfield will get the desired values but the 3rd textfield will incorporate all the other values in the txt file...

If you're using an array, the enhanced for is a good one for this application, but you should learn the details. It can be confusing. Google "java enhanced for" and you should get a number of explanations to choose from.

A standard for loop is well-suited to iterating over an array. The standard usage is like this:

for (int i=0; i<myArray.length; i++)
{  // code to execute here }

This starts at zero and goes to the end of the array (note the i<myArray.length - you're running from 0 to length-1 to get all of the values in the array without going over the edge).

If you have an iterator, like a StringTokenizer, you'll want to use a while loop:

while (tokenizer.hasNext())
{
  String token  = tokenizer.next();
  // do something with token
}

Thank you!! Found out how to work it out!

It seems with For Loop is better than doing while loop. Is it possible to define the delim with \t or \n? Or is it just for normal Spaces.

From the API: [When called without a user-defined delimiter, ]

The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.

So it's already done for you. If you wanted to break on spaces only (not tabs, etc), you'd have to define a custom delimiter.

If i want to do

Customer.txt =
Firstname Lastname Address Email
How do i get the first 2 part of the string into 1 field ?

Textfield1: John Doe
TextField2: Japan
Textfield3: Test@Testing.com

It would be easiest to combine the two fields when you needed them combined. You could conceivably do something where you get the first two tokens and concatenate them together as you're reading them, but that complicates your processing for no real gain.
Why not just have firstname and lastname fields?

Ive tested with Split() and it seemed to work but it only works towards the end of the array

String strLine = reader.readLine() ;
                   String[] words = strLine.split(" ",3);

the first 2 textfield will get the desired values but the 3rd textfield will incorporate all the other values in the txt file...

Try it without the limiter (the 3 in your example)

This article has been dead for over six months. Start a new discussion instead.