I want to use the variable that is taken from the inputString() method into the countWords() method below, but I unfortunately run into errors when I try using the latter in the main() method. How do I work around this?
import java.util.*;
public class WordCounter {
public static String inputString(){
Scanner s = new Scanner(System.in);
System.out.println("\nInput your desired sentence/string: ");
String userInput = s.nextLine();
return userInput;
}
public static void countWords(int count, String userInput){
count = 1;
for (int i = 0; i < userInput.length() - 1; i++)
{
if ((userInput.charAt(i) == ' ') && (userInput.charAt(i + 1) != ' '))
{
count++;
}
}
System.out.print("Number of words in the sentence/string: " + count);
}
public static void main (String [] args){
System.out.println("Word Counter.");
inputString();
countWords();
}
}
>Solution :
Learn to return values, and pass values
- Capture the return value coming from your
inputStringmethod. - Pass that returned value to your
countWordsmethod.
Change this:
inputString();
countWords();
… to this:
String input = inputString(); // Capture the return value.
countWords( input ); // Pass the captured value.
And it looks like count in countWords is supposed to be the result. So that should be returned to the calling method.
String input = WordCounter.inputString();
int count = WordCounter.countWords( input );
And alter your countWords to return that value. Change the declared return type from void to int. Add a return line at the end.
public static int countWords( String userInput ){
…
return count ;
}
OOP
Looking at the bigger picture, try to avoid static as a student learning object-oriented programming.
Create multiple classes, where each class has one main responsibility. If we look at your business problem, we have three responsibilities:
- Business logic: Analyzing text to count words.
- User interface: Interacting with user on the console.
- An app to run the show, with its
mainmethod.
You might start with three classes.
First, the business logic.
package work.basil.example.wordcounter;
import java.util.concurrent.ThreadLocalRandom;
// Takes a string, analyzes to determine a count of words, and returns that count.
public class WordCounter
{
private CharSequence text;
public WordCounter ( final CharSequence input )
{
this.text = input;
}
public int wordCount ( )
{
return ThreadLocalRandom.current().nextInt( 0 , 10 ); // Fake method. Put your code here.
}
public CharSequence text ( )
{
return this.text;
}
}
The console input manager.
package work.basil.example.wordcounter;
public class ConsoleInputGatherer
{
public String gatherInput ( )
{
// Fake method. Write your code here.
return "your text goes here.";
}
}
And the main app.
package work.basil.example.wordcounter;
public class App
{
public static void main ( String[] args )
{
App app = new App();
app.demo();
}
private void demo ( )
{
String input = new ConsoleInputGatherer().gatherInput();
int count = new WordCounter( input ).wordCount();
// Report results.
System.out.println( "Input: " );
System.out.println( input );
System.out.println( "… has a word count of: " + count );
}
}
When run:
Input:
your text goes here.
… has a word count of: 3
Avoid char
And, by the way, the char type has been essentially broken since Java 2, and legacy since Java 5. As a 16-bit value, it is physically incapable of representing most characters. Instead, to work with individual characters, use code point integer numbers.