English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

3 answers

use quotation marks.

2006-11-09 03:50:13 · answer #1 · answered by xerocs 5 · 1 0

So, there are several ways in java. It depends on how deep you want to use the pre-written classes -- the JFC.

I assume, you are learning java, this is an assignment to learn String, StringBuffer, and char

It was hard for me at first to figure out why java is so "type" sensitive, but it is. If we write class looking for a String, we have to give a String and so on so forth for all the types.

Strings are different from characters.
String myString = "this is spiffy string";
char myChar = 'c';

and, they are different animals

================

public class WhiteSpaceTest {

public static void main(String[] args) {

String tabstop = "\t";
String s = "This is a test of the emergency string system."+tabstop+tabstop;
s+= "1" + " " + ", put a stop to this.";
int len = s.length();


int count = 0;
StringBuffer dest = new StringBuffer( s );
char space = ' ';
char tabb = '\t';

for (int i = 0; i < len; i++) {

char test = dest.charAt(i);

if( (test == space ) || (test == tabb ) ) count++;



}
String results ="The total count of the line is: " + s.length() + "\n";
results += "The number of white spaces in the line is: " + count;
System.out.println( results );

}
}
===========
Study my code. I have assigned /space/ as a String. I have assigned /space/ as a char.

Best of luck. I love java. Wished I had my old java club back from the 1990s.

2006-11-09 13:36:48 · answer #2 · answered by Anonymous · 0 0

String input = ...;

char[] chars = input.toCharArray();
int numOfWhitespaceChars = 0;

for(int i = 0; i if(Character.isWhitespace(chars[i])){
numOfWhitespaceChars++;
}

System.out.println(
"Number of whitespace characters = "
+ numOfWhitespaceChars);

Note: this counts line feeds and carriage returns separately.
This could also be done using regular expressions...

2006-11-09 19:33:39 · answer #3 · answered by vincentgl 5 · 0 0

fedest.com, questions and answers