[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

replace words

Posted on 2009-04-22
8
Medium Priority
?
220 Views
Last Modified: 2013-11-23
Hi there
When i compile the following it is giving me an error in line as i need it to be able to replace the words in the sentence.
however the errors i get are formatting errors. could you please advise if the code below is more formatting errors or if there is a problem with the code.



class Words{
	String sent = "I have a black cat";
 
		int countWords(String sent){
		String[] sentence= sent.split(" ");
		System.out.println(sentence.length);
		return sentence.length;
 
	}
}
 
class AverageWords{
	String sent = "I have a black cat";
 
	void avgWords(String sent){
			Words x= new Words ();
		    System.out.println (sent.length()/x.countWords(sent));
 
			String[] sentence= sent.length("");
		    int total = 0 ;
		    int size = 0 ;
		    size = sentence.length;
		    for (int i = 0 ; i<size; i++) {
		    	total += sentence [i].length();
		    }
 
		    int avg = total/size;
 
	}
}
 
class ReplaceWord{
	String sent = "I have a black cat";
	String replaced = sentence.replaceAll ("cat", "dog");
 
	void repWord(String sent) {
		Word y= new Word();
		System.out.println("Replacing dog for cat");
		System.out.println("replaced");
		System.out.println();
	}
}
 
class Printer {
 
	public static void main (String[] args) {
		Words w= new Words ();
		int x = w.countWords(w.sent);
		AverageWords aw =  new AverageWords ();
		aw.avgWords(aw.sent);
	}
 
 
}

Open in new window

0
Comment
Question by:Sdurrani
  • 3
  • 3
  • 2
8 Comments
 
LVL 16

Assisted Solution

by:imladris
imladris earned 800 total points
ID: 24208080
Well, I see some compile errors. This line:

                  String[] sentence= sent.length("");

is a problem, because the length method for a String variable does not take an argument. In fact that whole line seems odd. The first part (String[] sentence) seems to suggest that you want to initialize an array (with new), but the second part seems to be evaluating to an integer. I'm not clear on what was intended here. Maybe: "String[] sentence=new String[sent.length()];".

Also in the ReplaceWord class, the following line:

      String replaced = sentence.replaceAll ("cat", "dog");

is a problem, since there is no variable named sentence in the ReplaceWord class. There is one in the Word class, but to access that would require a reference to some specific Word object.

Also, in ReplaceWord the following line:

            Word y= new Word();

appears to be referencing the first class in the file. If so, it is missspelled. The first class is named in the plural: Words.

I'd call these compile errors, rather than formatting errors.

Did I, at least partially, answer the question?

0
 

Author Comment

by:Sdurrani
ID: 24208894
Here I worked on the code and changed it basically  but still getting some compiling errors

class StringWork{
 
public static void main(String[] args)
    {
 
        StringWork sw = new StringWork();
        String sentence = "I have a black cat.";
        String longText = "how are you? It is nice to see you here! Have a nice day. I will see you tomorrow.";
 
 
        System.out.println("The test sentence is:");
        System.out.println(sentence);
        System.out.println();
 
        System.out.println("The test long text is:");
        System.out.println(longText);
        System.out.println();
 
 
        float avg =sw.calculateAverageWordSize(sentence);
        System.out.print("Found average word size: ");
        System.out.println(avg);
        System.out.println();
 
 
        System.out.println("Finding shortest and longest words in the sentence.");
        sw.printShortestAndLongest(sentence);
        System.out.println();
 
        System.out.println("Finding shortest and longest words in the longer text.");
        sw.ShortestAndLongest(longText);
        System.out.println();
 
 
        String replaced = sentence.replaceAll("cat", "dog");
        System.out.println("Replacing dog for cat:");
        System.out.println(replaced);
        System.out.println();
 
 
        int numSentences = sw.countSentences(longText);
 
        System.out.println("Finding the number of sentences in this text:");
        System.out.println(longText);
        System.out.print("=======\nsentences found: ");
        System.out.println(numSentences);
        System.out.println();
    }
 
 
}

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1200 total points
ID: 24209041
Try this to get it to compile.  You will then have to implement the methods according to logic you need.
class StringWork {
 
    public static void main(String[] args) {
 
        StringWork sw = new StringWork();
        String sentence = "I have a black cat.";
        String longText = "how are you? It is nice to see you here! Have a nice day. I will see you tomorrow.";
 
 
        System.out.println("The test sentence is:");
        System.out.println(sentence);
        System.out.println();
 
        System.out.println("The test long text is:");
        System.out.println(longText);
        System.out.println();
 
 
        float avg = sw.calculateAverageWordSize(sentence);
        System.out.print("Found average word size: ");
        System.out.println(avg);
        System.out.println();
 
 
        System.out.println("Finding shortest and longest words in the sentence.");
        sw.printShortestAndLongest(sentence);
        System.out.println();
 
        System.out.println("Finding shortest and longest words in the longer text.");
        sw.ShortestAndLongest(longText);
        System.out.println();
 
 
        String replaced = sentence.replaceAll("cat", "dog");
        System.out.println("Replacing dog for cat:");
        System.out.println(replaced);
        System.out.println();
 
 
        int numSentences = sw.countSentences(longText);
 
        System.out.println("Finding the number of sentences in this text:");
        System.out.println(longText);
        System.out.print("=======\nsentences found: ");
        System.out.println(numSentences);
        System.out.println();
    }
 
    private void ShortestAndLongest(String longText) {
 
    }
 
    private float calculateAverageWordSize(String sentence) {
        return 0f;
    }
 
    private int countSentences(String longText) {
        return 0;
    }
 
    private void printShortestAndLongest(String sentence) {
        
    }
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Sdurrani
ID: 24209072
thanks .. i dont know why there seems to be error messages when i try to compile - is it possible that there is a problem whit my compiler?
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 1200 total points
ID: 24209105
What are the errors you are getting?
0
 

Author Comment

by:Sdurrani
ID: 24209123
any line with sw.   is being recognized as an error.
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 1200 total points
ID: 24209460
Did you paste in the code I supplied and hit file save?  Try using Shift+F11 to clean and build main project.
0
 
LVL 16

Assisted Solution

by:imladris
imladris earned 800 total points
ID: 24280206
Did any of that help?

If so, it is now time to select one or more answers, close and grade them.

If not, perhaps a clarifying question would help.

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question