What is your preferred method of disposal for human corpses?

Java Question: Calling a method within a method.?

  • Hello, I have to write the babble method: The babble method This is a void method that takes one integer parameter. This method calls the makeASentence method the number of times specified by the parameter. That is, if the value 20 was passed as the parameter to this method, it would call the makeASentence method 20 times, causing 20 random sentences to appear in the terminal window. However, the makeASentence method should only be called if the parameter is a value greater than 0. Thus, this method needs both an if statement and a loop statement (your choice of a for loop or a while loop). Here is my code so far: public void makeASentence() { int howIndex = randomosity.nextInt( how.length ); String howWord = how[ howIndex ]; int whoIndex = randomosity.nextInt(who.length); String whoWord = who[ whoIndex ]; int whatIndex = randomosity.nextInt(what.length); String whatWord = what[ whatIndex ]; int whenIndex = randomosity.nextInt(when.length); String whenWord = when[ whenIndex ]; System.out.println(howWord +"," + whoWord + " " + whatWord + " " + whenWord + "."); } public void babble(int howManyTimes) { for (int times = 0; times < howManyTimes; times++) { if(times <= 0){} else{ String babble = makeASentence(); } } } It keeps saying that the method is incompatible type. I don't quite understand what I'm doing wrong here.

  • Answer:

    Instead of String babble = makeASentence(); replace it with only makeASentence();

shawl92 at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

I don't know if I'm gonna answer your question, but what I see is that at the beginning of the for loop, times is always zero, so it gets into the if loop and doesn't do anything, te method ends there. You could try: for (int times = howManyTimes; times == 0; times --) { //rest of code }

Roberto

1. makeaSentence() does not return any thing according to your definition. But it is called wrongly. 2. I still did not understand what you are doing in makeASentence. How ever you would have called this like if( times>0) makeASentence(); 3. Also you have declared babble method and also babble as String

James Bond

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.