166k views
1 vote
I just need some help working out this Java code! I can list the instructions below:

Modify the CountSpaces program so that contains a String that holds your favorite inspirational quote. Pass the quote to the calculateSpaces method and return the number of spaces to be displayed in the main method.

We're given this to start however I have been struggling as there are two classes and it's been confusing me. Thanks so much for any help! All good and God Bless!

public class CountSpaces
{
public static void main(String[] args)
{
// write your code here
}
public static int calculateSpaces(String inString)
{
// write your code here
}
}

User Gco
by
6.2k points

1 Answer

3 votes

Answer:

public class CountSpaces

{

public static void main(String[] args)

{

String quote = "The concept of infinity is meaningless inside of an insane human mind.";

int nrSpaces = calculateSpaces(quote);

System.out.println("Nr spaces in your quote is " + nrSpaces);

}

public static int calculateSpaces(String inString)

{

int count = 0;

for (int i = 0; i < inString.length(); i++) {

if (inString.charAt(i) == ' ') {

count++;

}

}

return count;

}

}

Step-by-step explanation:

In this example there is only one class, and since all methods are static the true concept of the class is not even being used.

User Dnagirl
by
5.8k points