java - using string methods to count characters -


I am trying to calculate the number of empty letters in the string.

When there is no major space, works properly, but when I add 3 places, it doubles the number of non-blank characters.

This is my code:

  import java.io. *; Import java.util. *; Public class counting {public static zero main (string [] args) exception {string string 1; integer number; Calculation = 0; BufferedReader Data In = New BufferedReader (New InputStreamReader (System.in)); System.out.print ("Enter a string:"); Str1 = dataIn.readLine (); While (str1.length ()> gt; {System.out.println ("The string" '+ 1 + 1' ''); System.out.println ("is" + str1.length () + "Characters with all spaces."); (Int i = 0; i & lt; str1.length (); ++ i) if (str1.charAt (i)! = '') Count ++; Str1 = str1.trim (); System.out.println ("and" + str1.length () + "characters filtering forward and trailing spaces."); System.out.println ("and" + count + "Non-empty characters."); Println (""); System.out.print ("Enter a string:"); Str1 = dataIn.readLine ();} System.out.println ("Program complete." );}}  

Are you sure It repeats the count every time? Maybe it only happens through the main loop for a second time?

You should reset count while inserting a new string. , You are just adding to the count from the last time through the main loop. count = 0; before System.out.print ("enter a string Do: "); Add or declare and start at the bottom of the main loop count , before the loop.


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -