File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Playing with String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Playing with String " Watch "Playing with String " New topic
Author

Playing with String

divya madala
Ranch Hand

Joined: Jan 10, 2001
Posts: 61
I'm trying to print all my records.
One sample record is |03/24/2003|03/25/2003|03-B-01640-2Q|MAX S GRAY
I have it as a String and is printed now as
|03/24/2003|03/25/2003|03-B-01640-2Q|MAX S GRAY
But I need to have my output as
|03-B-01640-2Q|03/24/2003|03/25/2003|MAX S GRAY, i.e; the third field as the first one.
also, if the 2 date feilds are the same I need to print it twice
i.e;
If my two dates are the same as today 11/17/2003, then my output will be
|03-B-01640-2Q|11/17/2003|11/17/2003|MAX S GRAY
Please suggest how I can have these implemented to a string. Any help with code will be great!!!
Thanks,
Divya
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
You might try the String's ".split()" method. This breaks up a string based on a given delimiter and returns a String array of the sections. Because the pipe character (|) has special meaning, to use it in the method you'll have to specify it as "\\|". Also, since the record you are splitting has a leading pipe the first entry in the results array with be "".
You can refer to the API documentation for the exact way to invoke this ... I don't want to give too much away in case this is a homework assignment.
NOTE: This works with the "1.4.x" JDK; if you are using an older version, look a the "java.util.StringTokenizer" class for hints.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Use StringTokenizer class.
divya madala
Ranch Hand

Joined: Jan 10, 2001
Posts: 61
for(int k=0;k<4;k++)//Assuming there are 4 fields with 2 Date's
{
str3= str2.split("\\|");
}
for(int k=0;k<str3.length;k++)
{
System.out.println(str3[k]+"|");
}
This's printing only the 3rd feild, skipping first 2 date fields.
divya madala
Ranch Hand

Joined: Jan 10, 2001
Posts: 61
I've used the String Tokenizer too..
Unfortunately even that results in printing only the last field.
What I am trying to do is read the HTML page as a string

byte[] bs = p.getResponseBody();
BufferedReader reader = new BufferedReader(new
InputStreamReader(new
ByteArrayInputStream(bs)));
while ( (str = reader.readLine()) != null) {
if (str.trim().startsWith("]") ||
(str.trim().startsWith("<") ||
"".equals(str.trim()) ||
(str.trim().startsWith("{")) ||
(str.trim().startsWith("}")) ||
(str.trim().startsWith("-->")) ||
(str.trim().endsWith(">")) ||
(str.trim().endsWith(")")) ||
(str.trim().endsWith(");")) ||
(str.trim().endsWith(";")) ||
(str.trim().endsWith(".")) ||
(str.trim().endsWith("{")) ||
(str.trim().startsWith("//")) ||
str.trim().startsWith("&"))) {
}
else { //to avoid printing the duplicate fields
if (!hashTable.contains(str)) {
hashTable.put(new Integer(hashIndex), str);
hashIndex++;

}
}
}
reader.close();
//removing fields I don't need to have in my output
hashTable.remove(new Integer(0));
hashTable.remove(new Integer(1));
hashTable.remove(new Integer(2));
hashTable.remove(new Integer(4));
hashTable.remove(new Integer(6));
hashTable.remove(new Integer(7));
hashTable.remove(new Integer(9));
names = hashTable.elements();
while (names.hasMoreElements()) {
str2 = (String) names.nextElement();
//str = str.
str2 = str2.trim();
//System.out.print("|"+ str2);
str2 = "|"+ str2;
//System.out.print("|"+ str2);
for(int k=0;k<4;k++)
{
str3= str2.split("\\|");
}
}
for(int k=0;k<str3.length;k++)
{
System.out.println(str3[k]);
}
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399

This prints out: "|03-B-01640-2Q|03/24/2003|03/25/2003|MAX S GRAY" which is, according to your original post, what you are looking for. There is no need to call ".split()" inside of a loop: you split the string once.
In a *real* application you'd want to check for the correct number of array elements before doing the last print to ensure that bad data didn't cause the application to throw an exception.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Playing with String
 
Similar Threads
Problem moving columns and rows in 2D array.
Output - DIR
grant write access to folder in unix
Questions regarding the article "String Literally" written by Corey McGlone
vb.net client accessing jboss web service... Sounds easy, no?