944,089 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 19392
  • Java RSS
Jul 3rd, 2007
0

String to Object

Expand Post »
Hi, How do i create a class called Text
that creates an object from a string
and that deletes the first word of the text

have no idea

I was starting like

Java Syntax (Toggle Plain Text)
  1.  
  2. public class Text{
  3. public static void main(String[] args)
  4. {
  5. String str = "this is a little text and i have to delete the first word this ";
  6. }
  7.  
  8. }

But really i have no idea

Please any help is gratefull
Similar Threads
Reputation Points: 8
Solved Threads: 1
Light Poster
endsamsara is offline Offline
36 posts
since Mar 2007
Jul 3rd, 2007
0

Re: String to Object

first of all: in what kind of object would you like your String object to turn? since a String on itself is an object.
in order to delete the first word: go check out the api's on the indexOf( ) method and the substring( ) method.
this is about all you'll need to make your class do the trick.
Reputation Points: 938
Solved Threads: 356
Posting Maven
stultuske is offline Offline
2,525 posts
since Jan 2007
Jul 4th, 2007
-1

Re: String to Object

hi:
you can see my code whether help you
package com.structure;
import java.util.ArrayList;
import java.util.List;
public class Text {
String statement;
List<Character> listchar = new ArrayList<Character>();
public Text(String statement) {
super();
this.statement = statement;
exchangeList();
}
private void exchangeList() {
for (int i = 0; i < statement.length(); i++) {
listchar.add(statement.charAt(i));
}
}
public String deleteIndex(int index) {
String temporary="";
listchar.remove(index);
for (Character c : listchar) {
temporary += c;
}
return statement = temporary;
}
public String getStatement() {
return statement;
}
public void setStatement(String statement) {
this.statement = statement;
}
public static void main(String args[]) {
Text text = new Text("this is deng statement");
System.out.println(text.getStatement());
text.deleteIndex(4);
System.out.println(text.getStatement());
}
}
Reputation Points: 30
Solved Threads: 4
Newbie Poster
deng_cen is offline Offline
14 posts
since Apr 2007
Jul 5th, 2007
0

Re: String to Object

Click to Expand / Collapse  Quote originally posted by endsamsara ...
Hi, How do i create a class called Text
that creates an object from a string
and that deletes the first word of the text

have no idea

I was starting like

Java Syntax (Toggle Plain Text)
  1. public class Text{
  2. public static void main(String[] args)
  3. {
  4. String str = "this is a little text and i have to delete the first word this ";
  5. }
  6.  
  7. }
But really i have no idea

Please any help is gratefull
Anyway, try this:
Extend existing String class in say MyString.
In MyString's constructor
- take a String as argument.
- Cut out the first word and pass on the rest to base class.

Click to Expand / Collapse  Quote originally posted by deng_cen ...
hi:
you can see my code whether help you
package com.structure;
import java.util.ArrayList;
import java.util.List;
public class Text {
String statement;
List<Character> listchar = new ArrayList<Character>();
public Text(String statement) {
super();
this.statement = statement;
exchangeList();
}
..................
public static void main(String args[]) {
Text text = new Text("this is deng statement");
System.out.println(text.getStatement());
text.deleteIndex(4);
System.out.println(text.getStatement());
}
}
On to your 14th post and you still haven't figured out the magic of code tags.. ?!
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jul 5th, 2007
0

Re: String to Object

Stultuske already provided all the info needed to complete your assignment. Homework has a purpose.

deng_sen, you really need to use the code tags to make your code more readable.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Change Tab Order
Next Thread in Java Forum Timeline: Executing Shell script from Java code.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC