954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String to Object

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

public class Text{
    public static void main(String[] args)
    {
       String str = "this is a little text and i have to delete  the first word this ";
    }
    
}


But really i have no idea

Please any help is gratefull

endsamsara
Light Poster
36 posts since Mar 2007
Reputation Points: 8
Solved Threads: 1
 

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.

stultuske
Posting Sensei
3,120 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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 listchar = new ArrayList();
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());
}
}

deng_cen
Newbie Poster
14 posts since Apr 2007
Reputation Points: 30
Solved Threads: 4
 

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

public class Text{
    public static void main(String[] args)
    {
       String str = "this is a little text and i have to delete  the first word this ";
    }
    
}

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.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 listchar = new ArrayList();
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.. ?!

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

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.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You