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.");
}
}
}

Recommended Answers

All 3 Replies

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.

Thank you very much!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.