0

Hi, I'm trying to create a program that reads in a line of text. For some reasons, when I try to run the program, "Your input has ---- non-blank characters." comes up under every words. I want it to just come up once at the end. Any help will be appreciated. Thank you!

import java.util.*;
public class WordsInStrings{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    String s = scan.nextLine();
    String[] words = s.split(" ");
    for(int i=0; i<words.length; i++){
    System.out.println(words[i]);
    int count=0;
    for(int a=0; a<words.length; a++){
      count=words[a].length()+count; 
  }
    System.out.println("Your input has " + count + " non-blank characters.");
}
}
}
2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by donaldw
0

Your output line is inside of your

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

loop, so it will execute words.length times.

If you want it to only execute once, move it outside of any loops which execute multiple times per function execution.

0

Thank you very much!

Fixed? Don't forget to mark the thread solved so people don't keep posting the same solution over and over.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.