0

I am requesting a string from the user. After the user enters their strings a prompt asks if they want to see strings with no, one or more spaces in their strings, then displays the strings.

I am running into issues with counting the spaces in the string. The code provided only counts 1 if there is more than one space in the string. Whole code:

import java.util.*;
import java.util.Scanner;

public class CountSpacesInStrings {

public static void main(String[] args) 
{
    
    Scanner s = new Scanner(System.in);
    String[] array = new String[20];
    System.out.println("Please enter anything..., or type QUIT to quit.");
    
    for (int i = 0; i < array.length; i++) {
        array[i] = s.nextLine();
        boolean result = Arrays.stream(array).anyMatch("QUIT"::equals);
       if(result) 
       {
           break;
       }
    }
    String str = null;
    int len = -1;
    
    
    System.out.println("Would you like to display strings with No Spaces, One Space or More? Type No, One, More to see the results: ");
    String answer = s.nextLine();
    if(answer.equals("No")){
        
        for (int i = 0; i < array.length;i++) {
            int count = 0;
            if (array[i] != null) {
                if (array[i].charAt(i) != ' ') {
                    count++;
                    System.out.println(count);
                }
            }
        }
           
        

    }
        else if(answer.equals("One"))
        {
            for (int i = 0; i < array.length;i++) {
                int count = 0;
                if (array[i] != null) {
                    if (array[i].charAt(i) != ' ') {
                        count++;
                        System.out.println(count);
                    }
                    
                        //System.out.print(array[i] + " ");   
                }
             }
        }
        else
            System.out.println("No values to show");

    System.out.println();
}   

}

The section I'm looking at is:

        if(answer.equals("No")){
        
        for (int i = 0; i < array.length;i++) {
            int count = 0;
            if (array[i] != null) {
                if (array[i].charAt(i) != ' ') {
                    count++;
                    System.out.println(count);
                }
            }
        }
ciresuark
  • 71
  • 5
  • 1
    What do you think `if (array[i].charAt(i) != ' ') {` does? You only have one loop which lets you iterate over all *sentences* in array, but don't have any mechanism to iterate over all *characters* in each sentence. – Pshemo Oct 19 '20 at 19:31

1 Answers1

2

Based on the comment of @Pshemo: You would need to add a nested for loop. The second for loop has to iterate through the contents of array[i] (which makes one sentence) and count the number of ' ' characters in that sentence.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
OSteTs
  • 86
  • 5
  • That is not the problem. Notice that loop `for (int i = 0; i < array.length;i++) {` iterates over element in array, so setting `count` to `0` for each element inside that array is correct. What OP needs is now checking all characters in each sentence. – Pshemo Oct 19 '20 at 19:32
  • @Pshemo Ah right, I missed that part of the logic. Thanks for clarifying! – OSteTs Oct 19 '20 at 19:39