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
}
}
Help with Counting Characters
Page 1 of 13 Replies - 14900 Views - Last Post: 23 December 2006 - 04:48 PM
#1
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!
Replies To: Help with Counting Characters
#2
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:
set before the while loop.
used at
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.
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.
#3
Re: Help with Counting Characters
Posted 29 November 2006 - 08:45 PM
salindor, 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:
set before the while loop.
used at
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.
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
#4
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 
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
ClintWB, on 29 Nov, 2006 - 08:45 PM, said:
salindor, 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:
set before the while loop.
used at
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.
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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|