3 Replies - 14900 Views - Last Post: 23 December 2006 - 04:48 PM Rate Topic: -----

#1 ClintWB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 02-September 06

Help with Counting Characters

Posted 29 November 2006 - 07:52 PM

I have attached my code it runs fine on the first run after that all hell breaks loose.....I input the second set of strings and the calclations do not change. I could use some advice do i need a string buffer are an arraylist or is the main just not coded correctly just need to be pointed in the right direction thanks!



import java.io. *;
import java.util.*;

public class CharacterCount
{

  public static void main(String[] args) throws IOException
  {

	  String str,noEndSpace;	//Declare Variables
	  int length=0;
	  int noEs=0;
	  int whitespaceCount=0;
	  int totalCharacter=0;

	  BufferedReader dataIn = new BufferedReader(new  InputStreamReaderSystem.in));						   //Get inputs from User
	  System.out.print("Enter a string: ");
	  str = dataIn.readLine();

	  length = str.length();	 //Conversion of strings
	  noEndSpace = str.trim();
	  noEs = noEndSpace.length();


	  while( str.length() > 0)  //looping when input of characters is valid
	{

	  for (int i = 0; i < length; i++)  //counting spaces in string
				   {
			if (Character.isWhitespace(str.charAt(i)))
			whitespaceCount++;
		}

	   totalCharacter =  length-whitespaceCount;		 //removing spaces


   System.out.println("The String, "+(str)+ ",");		 //Output to screen
   System.out.println("has " +(length)+" characters, including all blanks,");
   System.out.println("and " +(noEs)+ " characters, trimmed of leading and		trailing blanks");
   System.out.println("and " +(totalCharacter)+ " non-blank characters\n");

   System.out.println("Enter a string: "); // request for input from user
   str = dataIn.readLine();
  }

System.out.println("Program complete."); //end of program no characters entered
 }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Counting Characters

#2 salindor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 10-November 06

Re: Help with Counting Characters

Posted 29 November 2006 - 08:21 PM

at least one bug can be attributed ot you code reling on a variable set prior to the loop running, but not being set afterwards.

The one I found with quick inspection is:
length = str.length(); //Conversion of strings


set before the while loop.

used at
for (int i = 0; i < length; i++) 



but never reinitalized when at the end of the for loop you change str.

I would go throuhg your loop and make sure all variable dependencies are taken care of prior to using the variable.
Was This Post Helpful? 0
  • +
  • -

#3 ClintWB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 02-September 06

Re: Help with Counting Characters

Posted 29 November 2006 - 08:45 PM

View Postsalindor, on 29 Nov, 2006 - 08:21 PM, said:

at least one bug can be attributed ot you code reling on a variable set prior to the loop running, but not being set afterwards.

The one I found with quick inspection is:
length = str.length(); //Conversion of strings


set before the while loop.

used at
for (int i = 0; i < length; i++) 



but never reinitalized when at the end of the for loop you change str.

I would go throuhg your loop and make sure all variable dependencies are taken care of prior to using the variable.



can you explain a little bit more on how to reinitalize the variables I'm pretty sure that is the problem but not sure how to fix it thanks
Was This Post Helpful? 0
  • +
  • -

#4 canrdboy28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-December 06

Re: Help with Counting Characters

Posted 23 December 2006 - 04:48 PM

Ok I'm a novice myself but I played around with the code and if you take your length=str.length() and move it above your for (int i = 0; i < length; i++) //counting spaces in string will work perfectly :)


View PostClintWB, on 29 Nov, 2006 - 08:45 PM, said:

View Postsalindor, on 29 Nov, 2006 - 08:21 PM, said:

at least one bug can be attributed ot you code reling on a variable set prior to the loop running, but not being set afterwards.

The one I found with quick inspection is:
length = str.length(); //Conversion of strings


set before the while loop.

used at
for (int i = 0; i < length; i++) 



but never reinitalized when at the end of the for loop you change str.

I would go throuhg your loop and make sure all variable dependencies are taken care of prior to using the variable.



can you explain a little bit more on how to reinitalize the variables I'm pretty sure that is the problem but not sure how to fix it thanks

This post has been edited by canrdboy28: 23 December 2006 - 04:56 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1