String to Object

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 23
Reputation: endsamsara is an unknown quantity at this point 
Solved Threads: 0
endsamsara endsamsara is offline Offline
Newbie Poster

String to Object

 
0
  #1
Jul 3rd, 2007
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

  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: String to Object

 
0
  #2
Jul 3rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 14
Reputation: deng_cen is an unknown quantity at this point 
Solved Threads: 4
deng_cen deng_cen is offline Offline
Newbie Poster

Re: String to Object

 
-1
  #3
Jul 4th, 2007
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());
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: String to Object

 
0
  #4
Jul 5th, 2007
Originally Posted by endsamsara View 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

  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.

Originally Posted by deng_cen View Post
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.. ?!
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,501
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: String to Object

 
0
  #5
Jul 5th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC