How to count whitespace in line? [JAVA] -
if have code counting specific word in file , want count whitespace in line, how can write it?
ex. want count whitespace in "int = 0;"
output: whitespace 3.
thank much.
my code:
public static void main(string[] args) throws ioexception { scanner in = new scanner (new file("/users/boommacbookproretina/desktop/c/resource [java sample file]/gradecal.java")); int c1 = 0, c2 = 0, c3 = 0; string t1 = "int"; string t2 = " "; string t3 = ";"; while (in.hasnext()){ string line = in.nextline(); line = line.trim(); if (line.length() >0 ){ int k = -1; while (true){ //count system k = line.indexof(t1, k+1); if (k<0) break; c1++; } } } system.out.println("total number integers are: " +c1); system.out.println("total number of whitespace in int line are: " +c2); }
resource file counting: download dropbox
try
int = str.length() - str.replaceall(" ", "").length();
for requirement
int whitespacecount = 0; //inside while loop if (character.iswhitespace(text.charat(i)) { whitespacecount++ }
Comments
Post a Comment