View Single Post
  #1 (permalink)  
Old 06-10-2007, 05:49 PM
horhora
Posts: n/a
[SOLVED] fix the error in java program...?

import java.util.*;

class Ch9CountWords {

private static final char BLANK = ' ';

public static void main (String[] args) {

//Attempt No. 2

//There is still a problem with the Attempt No. 2 code.
//If the sentence ends with one or more blank spaces, then the
//value for wordCount will be one more than the actual number of
//words in the sentence. It is left as an exercise to correct
//this bug.

int index, wordCount, numberOfCharacters;
Scanner in = new Scanner(System.in);

// when running next(), stop when ENTER is pressed
in.useDelimiter("\n"

This program has an error, when user enters more than two
spaces after the sentence, the program counts words one too many.

is there anyone that can fix this?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!