Java字符串数组,计算空格的数量:” “

huangapple go评论81阅读模式
英文:

Java String Array, count number of spaces " "

问题

以下是翻译好的代码部分:

if (answer.equals("No")) {
    for (int i = 0; i < array.length; i++) {
        int count = 0;
        if (array[i] != null) {
            for (int j = 0; j < array[i].length(); j++) {
                if (array[i].charAt(j) == ' ') {
                    count++;
                }
            }
            if (count == 0) {
                System.out.println(array[i]);
            }
        }
    }
}

请注意,我在代码中做了一些修改,以正确计算字符串中的空格数并显示满足条件的字符串。

英文:

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(&quot;Please enter anything..., or type QUIT to quit.&quot;);
    
    for (int i = 0; i &lt; array.length; i++) {
        array[i] = s.nextLine();
        boolean result = Arrays.stream(array).anyMatch(&quot;QUIT&quot;::equals);
       if(result) 
       {
    	   break;
       }
    }
    String str = null;
    int len = -1;
    
    
    System.out.println(&quot;Would you like to display strings with No Spaces, One Space or More? Type No, One, More to see the results: &quot;);
    String answer = s.nextLine();
    if(answer.equals(&quot;No&quot;)){
    	
    	for (int i = 0; i &lt; array.length;i++) {
    		int count = 0;
            if (array[i] != null) {
            	if (array[i].charAt(i) != &#39; &#39;) {
            		count++;
            		System.out.println(count);
                }
            }
        }
		   
    	

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

    System.out.println();
}	

}

The section I'm looking at is:

 	    if(answer.equals(&quot;No&quot;)){
    	
    	for (int i = 0; i &lt; array.length;i++) {
    		int count = 0;
            if (array[i] != null) {
            	if (array[i].charAt(i) != &#39; &#39;) {
            		count++;
            		System.out.println(count);
                }
            }
        }

答案1

得分: 2

根据 @Pshemo 的评论:您需要添加一个嵌套的 for 循环。第二个 for 循环必须迭代遍历 array[i] 的内容(构成一个句子),并计算该句子中 &#39; &#39; 字符的数量。

英文:

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 &#39; &#39; characters in that sentence.

huangapple
  • 本文由 发表于 2020年10月20日 03:20:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64433862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定