*
The moose likes Beginning Java and the fly likes How to form a new string out of several old strings excluding some??....... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to form a new string out of several old strings excluding some??......." Watch "How to form a new string out of several old strings excluding some??......." New topic
Author

How to form a new string out of several old strings excluding some??.......

C Ho
Greenhorn

Joined: Dec 15, 2002
Posts: 16
Hello,

I have a slight problem. I have many appointments, in the form ^time%location?description , inside a schedule, in the form:

^14%Paul's house?Group Meeting
^9%Nutty Professor's Office?Office Hours
^12%MegaBits?Lunch

I need to write a method to cancel the appointment scheduled at the given time, if it exists and return true if an appointment was canceled, and false otherwise.

Here is my thinking process shown in some steps:

1) make a StringTokenizer and a temporary string
2) loop while tokens still exist
3) check if the appointment exists, if not return false.
4) use an if statement to check if the substring of the token indicating time is equal to the time passed through the parameter (isBooked checks to see if they have the same time, if they do, then apptment must be cancelled)
-if true: dont add it to the temp string
-if false: add the token to the temp string
5)loop ends, return true

Here is the code that I attempted to do so far, I tried lots of things and I am quite frustrated at this point, I know it doesnt work. No arrays allowed, or else my life would be so much easier. Any tips, hints, suggestions would be GREATLY appreciated.


[ November 08, 2004: Message edited by: C Ho ]
sorna latha
Greenhorn

Joined: Nov 08, 2004
Posts: 3
Hai Hope this code can solve your pblm.

import java.util.*;

class trysolve
{

public static void main(String args[])
{

// This is your input string

String st = "^14%Paul's house?Group Meeting,^9%Nutty Professor's Office?Office Hours,^12%MegaBits?Lunch ";

// Split that for each commas

StringTokenizer str = new StringTokenizer(st, ",");

String result = "";

String token;

while(str.hasMoreTokens())
{

/* ---------------Got each token found the length and discarded the ^ char ----*/

token = str.nextToken();

int length = token.length();

for(int i=1; i<length; i++)

if((token.charAt(i) == '%')||(token.charAt(i) == '?'))

result += ' ';

else

result += token.charAt(i);

result += "\n";


}

System.out.println(result);
}
}


What i did here is:

1) I have used comma as my delimiter and tokenized the string

2) I extracted each token and tried to remove the character ^ and store the remaining in the result

3) Whenever i encountered with % or ? i just appended a ' ' to the result

Regards
Sorna.V
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97
Hi,


I've read this message as; tokens ^string%string?string == a record. If a ^string == apptStart then remove this record. (remove ^string%string?string from the main string.

If this is the case then also have a look below...

Good luck!

[ November 09, 2004: Message edited by: jimmy die ]

Jimmy Die
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Use StringBuffer instead of String ..
to avoid performance issues.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
sorna latha
Greenhorn

Joined: Nov 08, 2004
Posts: 3
Sorry only read half the question. Hope this is fine. A modified version. Just go thru


InputString : "^14%Paul's house?Group Meeting,^9%Nutty Professor's Office?Office Hours,^12%MegaBits?Lunch ;


method1 :

Allows us to align the input string in this format:

14 Paul's house Group Meeting
9 Nutty Professor's Office Office Hours
12 MegaBits Lunch

method2 :

Checks for the time input and displays the output.

If time input is 9 output is :

14 Paul's house Group Meeting
12 MegaBits Lunch

/*

^14%Paul's house?Group Meeting,^9%Nutty Professor's Office?Office Hours,^12%MegaBits?Lunch

*/

/* To write a java Pgm that splits the above into a proper format */

import java.util.*;
import java.io.*;

class appointment
{
String result,st,finalresult="";


String method1()
{
// This is your input string

st = "^14%Paul's house?Group Meeting,^9%Nutty Professor's Office?Office Hours,^12%MegaBits?Lunch ";

// Split that for each commas

StringTokenizer str = new StringTokenizer(st, ",");

String result = "";

String token;

while(str.hasMoreTokens())
{

/* ---------------Got each token found the length and discarded the ^ char ----*/

token = str.nextToken();

int length = token.length();

String temp = "";



for(int i=1; i<length; i++)

if((token.charAt(i) == '%')||(token.charAt(i) == '?'))

result += ' ';

else

result += token.charAt(i);

result += "\n";


}
return(result);
}


void method2(int t)
{

String strstore;

int i,intstore;

String res = method1();


StringTokenizer st1 = new StringTokenizer(res,"\n");

while(st1.hasMoreTokens())
{
String stringtry = st1.nextToken();

for( i=0;stringtry.charAt(i)!=' ';i++);

strstore = stringtry.substring(0,i);

intstore = Integer.parseInt(strstore);

if(intstore != t)

finalresult += stringtry + "\n";
}

System.out.println(finalresult);
}


}




class trysolve
{

public static void main(String args[])
{

// This is your input string

String st = "^14%Paul's house?Group Meeting,^9%Nutty Professor's Office?Office Hours,^12%MegaBits?Lunch ";

int time = 10;

appointment ap1 = new appointment();

ap1.method2(time);

}
}
C Ho
Greenhorn

Joined: Dec 15, 2002
Posts: 16
Hi guys,

Thanks for all the suggestions and input, I am currently going through all the code to see how it works and whether it helps accomplish my task or not. I might have not been clear enough in my first post, I'll reclarify.

the schedule will actually be in this form, containing many appointments:

^time%location?description\n^time%location?description\n^time%location?description

^time%location?description\n is one appointment.

There are no commas separating. The only delimiters are ^ % ? and \n (no \n for the last line).

What it's supposed to do is first see if the time, integer appStart, matches with the given time. If it matches, then the appointment at that specific time, or shall I say, the whole line "^atthatspecifictime%location?description\n" will be removed. The only way I can think of doing this is by readding all the tokens into a temp string and then assigning the temp string (updated string without that specific line) into the schedule string.

So, for example, I have this String:

schedule = "^14%Paul's house?Group Meeting\n^9%Nutty Professor's Office?Office Hours\n^12%MegaBits?Lunch"

If I call the cancelmethod, say cancelAppointment(9), I need to take out the appointment specifically at 9, then my new schedule string will be like "^14%Paul's house?Group Meeting\n^12%MegaBits?Lunch".

And since I wrote a method earlier to check if the appointment exists at a given time, I can use that to see if the appointment exists. Using the example above, if there exists an appointment at 9, I want to cancel it, or "remove it", then return true, if it does not exist, there is nothing to cancel, so I simply return false.

C Ho
Greenhorn

Joined: Dec 15, 2002
Posts: 16
Tried doing some more, confused at this point..

Need to somehow add all the tokens to one string excluding ones that I dont want..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to form a new string out of several old strings excluding some??.......
 
Similar Threads
Getting exception with buffered loop...
boolean remove(Appointment appt)
StringTokenizer
empty tokens in String Tokenizer
Help with printing appointments