This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Trying hard to make a basic String Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying hard to make a basic String Class" Watch "Trying hard to make a basic String Class" New topic
Author

Trying hard to make a basic String Class

Burak Ahmet
Greenhorn

Joined: Feb 06, 2004
Posts: 5
Hi all,
I am stuck with my csc project and need home help from the hood...This is how it goes:
-PROF wants us to write a string class-I know java already has one but he need one from us that uses singly linked list data structure.. so far so good , ha..
-I sat and actually wrote some code..
-Here the Problem comes into stage
-I can only enter one char at a time and I hated it.. I want to be able to write a whole lot more in one input and then manipulate it
-For example if this sentence is something that I put in <<<<I shoul be able to do some of these: add one character(S) at a given position, delete char(S) find a given substring etc.. Things you can do with the regular string class ..Anyway, right now somebody uses my code can enter
a
b
c
d
e
f
and for example add "s" to 2 node or delete one of the chars and I can make the whole thing added together and even do a search on it I am looking for some inspiration so that I can make it a phony string class
gotto go bed now..
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You have given a pretty good description on what you are trying to do, and even mentioned that you wrote some of your own code, but I am unclear about what your question is. What exactly are you having problems with? Does your code compile? What are the compiler errors, if any? If it compiles, what are the results? Are they what you expected? If not, how do the results differ from what you want?
The more details you can give us, the better we will be able to help you. Also, most of the people here expect you to SHOW that you have done some work, not just say that you have.
Please post some of your code (be sure to use the code tags -- there is a button below the "Add Reply" button to help you do this). Ask some specific questions about what problems you have encountered and we will be glad to help you along the way.
Good luck
Layne


Java API Documentation
The Java Tutorial
Burak Ahmet
Greenhorn

Joined: Feb 06, 2004
Posts: 5
here is one of my classes:
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ March 11, 2004: Message edited by: Dirk Schreckmann ]
Burak Ahmet
Greenhorn

Joined: Feb 06, 2004
Posts: 5
here is another one:
[ edited to format code and to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ March 11, 2004: Message edited by: Dirk Schreckmann ]
Burak Ahmet
Greenhorn

Joined: Feb 06, 2004
Posts: 5
OK I ve posted two of my classes, I also have a class that contains the main method and all.. It compiles and it does most of the things I want. My problem is that I can only enter one char at a time with my current code. I am wondering if there is a way to improve this code so that I can enter:
I am done ( all at once and then let's say delete "am" or add a char or substring in a place that I want)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Add a method to your class (and a constructor while you're at it that calls that method) that takes a String as an argument and breaks it up into its component chars which it then adds to the linked list at the correct position using the methods you now use for single characters.


42
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I'm looking at the code where you read input:

I think this is where you need to make changes in order to input more than one char before you press enter. However, I'm unsure about some of the details of this code. First of all, I don't know what SavitchIn is. I guess that it's a class you were given to do input operations for you. I am also curious about the readLineNonwhiteChar() method. How does it work? What does it do? Typically, a method's behavior is defined by pre-conditions and post-conditions. In other words, there are usually a set of conditions that should be met before the method is called and there are also a set of conditions that are defined after the function call. From what I see here, the method returns a char. This is one of its post-conditions. If you don't know these details, you will need to ask your teacher or someone else who can provide them.
You may also want to find the other methods that Savitch provides for you. Perhaps there is one that reads more than one char at a time?
I hope this helps you in the right direction. If not, please add some more information to answer the above questions.
Good luck!
Layne
Burak Ahmet
Greenhorn

Joined: Feb 06, 2004
Posts: 5
Well, you are right at your guess. SavitchIn is a simle console input class. I was using it and when I go back and checked the code for it I saw the method that I was using includes some "string" stuff in it. And this is sort of controversial to what I am trying to do isn' t it. Instead of using Java's handy String class I am trying to make a true string class. Well, at least at a basic level... I am really cofused now... I think I am going back to this data structures book that I have.. I might get some inspirations
By the way, I checked the Savitch code and yes, it actually have some method that reads more than one char at a time But is it any help, I don't know..

{
/**
Reads a line of text and returns that line as a String
value. The end of a line must be indicated either by a
new-line character '\n' or by a carriage return '\r'
followed by a new-line character '\n'. (Almost all systems
do this automatically. So you need not worry about this
detail.) Neither the '\n', nor the '\r' if present, are
part of the string returned. This will read the rest of a
line if the line is already partially read.
*/
public static String readLine( )
{
char nextChar;
String result = "";
boolean done = false;
while (!done)
{
nextChar = readChar( );
if (nextChar == '\n')
done = true;
else if (nextChar == '\r')
{
//Do nothing.
//Next loop iteration will detect '\n'.
}
else
result = result + nextChar;
}
return result;
}
 
jQuery in Action, 2nd edition
 
subject: Trying hard to make a basic String Class
 
Similar Threads
Sun Cirtification
string editor
ClassCastException ?
Check out this multi-level variable parser!!!
hibernate's real world value