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
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
Related Q & A:
- How to find inodes within a file?Best solution by cyberciti.biz
- What is the difference between a static method and class method in Python?Best solution by pythoncentral.io
- Why cannot we use static keyword inside a method in java?Best solution by Stack Overflow
- How to refresh a listview within a Fragment?Best solution by Stack Overflow
- How costly is it to renovate an old barn, into a three bedroom, water, electric n gas are within a few meters?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.