[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to append a space for missing word

Posted on 2006-06-04
15
Medium Priority
?
266 Views
Last Modified: 2010-08-05
String aString = "word1,,word3";
StringTokenizer parser1 = new StringTokenizer(aString , ",");
                 
while (parser1.hasMoreTokens()) {
     /****** How to append an space for missing word2?      
}
0
Comment
Question by:mayachoy
12 Comments
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 172 total points
ID: 16830201
Use Split function on string as it uses less resource and here is the code you need.

       String aString = "word1,,word3";
        String[] parser1 = aString.split(",");
                       
        for(int i=0;i<parser1.length;i++)
        {
            if(parser1[i]=="")
                parser1[i]=" ";
            System.out.println("word"+parser1[i]);
             
        }

Thank You.
0
 

Author Comment

by:mayachoy
ID: 16830212
the problem is my JRUN can't support split() function. is there any alternate way?
tks
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 16830497
Actually, there is a simple way but it may be very flexible

            String aString = "word1,,word3";
            aString = aString.replace(",,", ", ,");
            System.out.println(aString);

I still strongly suggest you to use split() as recommended by shivaspk

David
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:suprapto45
ID: 16830498
Sorry, typo

>>"it may be very flexible"
should be
it may NOT be very flexible

David
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 164 total points
ID: 16830504
           String aString = "word1,,,,word3";
            
            while (aString.indexOf(",,") > -1) {
                  aString = aString.replace(",,", ", ,");
            }
            
            System.out.println(aString);
0
 

Author Comment

by:mayachoy
ID: 16830568
how abt...if i read frm text file...
eg (original) : a      b      c
eg (error)    : b     c

while ((line = inputFile.readLine()) != null) {
   StringTokenizer strtok = new StringTokenizer(line,"\t",true);
   // How to i check the b is missing and i want to replace it with a space if b is missing? to become (""     b      c)
}


0
 

Author Comment

by:mayachoy
ID: 16830592
i have jus find out my JRUN is ver 3.1
it seem doent support slipt() and replace ()

how do i upgrade the version or is there any alternate way to avoid this upgrade?
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 16830631
Basically,

It may be the version of jdk and not the JRun that you need to upgrade. I am not very familiar with JRun so can't really help you.

Do you know what version of jdk you are using now?

David
0
 

Author Comment

by:mayachoy
ID: 16830643
i have tried to use dos to check the version

c:\java -version-----------(can't find)
d:\java -version------------(can't find)

and i go to the window explorer also cannot see the jdk** folder

0
 
LVL 16

Expert Comment

by:suprapto45
ID: 16830647
It might be that the jdk is embedded in the JRun. Try to search it in JRun installation folder. I will try to google it

David
0
 
LVL 3

Assisted Solution

by:runa_paathak
runa_paathak earned 164 total points
ID: 16830729
Use the following constructor of StringTokenizer:

StringTokenizer(String str, String delim, boolean returnDelims)

This will return you the delimiter too when you call parser1.nextToken(). So modifying your example code:

String aString = "word1,,word3";
StringTokenizer parser1 = new StringTokenizer(aString , ",", true);
               
while (parser1.hasMoreTokens()) {
     /****** How to append an space for missing word2?    
    System.out.println(parser1.nextToken());
}

the output would be:

word1
,
,
word3

You can have your logic in such a way that if you encounter 2 delimiters (one after the other), you know that the token at that position was missing.

0
 
LVL 2

Expert Comment

by:jmadda
ID: 16839305
This will allow you to match on the series of characters and replace them with another series of charaters.

String aString = "word1,,word3";
String bString = aString.replaceAll(",,", ", ,");
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
Last month Marc Laliberte, WatchGuard’s Senior Threat Analyst, contributed reviewed the three major email authentication anti-phishing technology standards: SPF, DKIM, and DMARC. Learn more in part 2 of the series originally posted in Cyber Defense …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question