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

Recommended Answers

All 4 Replies

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.

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());
}
}

commented: No code tags. Example does little to demonstrate the question. +0
commented: Get a life, do your own homework! -2
commented: Please be more descriptive and post code using code tags. +24

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<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.. ?!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.