This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Playing with String

 
divya madala
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use StringTokenizer class.
 
divya madala
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic