Counting characters in a string without an array...
Hi people i'm a complete noob to posting to forums so bear with me.
I have a big problem. I have to take an input of a string from a user and then print out the maximum, minimum and average number of character's in the string. I cannot use an array at all. :( I already have the average but i'm stuck trying to get the maximum and minimum number of characters of a word. I know i'm going wrong on a simple part but i don't have a clue where. I need help asap. If someone could just point me in the right direction it would be great help.
thanks
dazed and confused......
Code:
for(index = 1 ; index < sen.length() ; index++)
{
cCprevious = Character.toUpperCase(sen.charAt(index-1)) ;
cC = Character.toUpperCase(sen.charAt(index)) ;
if((cC < 'A' || cC > 'Z') && cC != ' ')
{
goodWord = false ;
System.out.print("Found Error -> " + cC + " : " ) ; // finds and points to error in string input
}
if(sen.charAt(index-1) == ' ' && cC >= 'A' && cC <= 'Z' && goodWord == true)
{
words++ ;
noOfChars = sen.length() ;
if (sen.length() >= currWord && goodWord == true)
{
maximum = currWord ;
}
if (sen.length() <= currWord && goodWord == true)
{
minimum = currWord ;
}
}
if (sen.charAt(index-1) == ' ' )
{
goodWord = true ;
noOfSpaces ++ ;
}
Re: counting characters in a string without an array...
Code:
String someString = "Some Data";
int totalCharacters = someString.length();
might get you started ;-)
Re: counting characters in a string without an array...
Thanks but i have that bit down with the average and all and the total number of chars but its getting the length of seperate words into memory thats the trouble...
Re: Counting characters in a string without an array...
Look at the API doc for the String class. It has several methods that will be useful for looking at, finding parts and working with part of Strings.
Its often a help to use paper and pencil. Write out a String of letters, spaces and words. Use arrows under the letters in the String as indexes. Move the arrows to find the features that you need to identify. For example a word would start at the first non-space. The word would end at the last non-space before a space.
Re: Counting characters in a string without an array...
go re-edit your code please and use:
[ CODE] in front of all your code... and [ /CODE] at the end of all your code... make sure to remove the extra space i have between the [ and the C. makes it much easier to read. thanks! ...
Re: Counting characters in a string without an array...
Yeah i think that clears it up a bit Norm. I actually think I have some similar code in a previous program for eliminating a middle name and out putting just the first and last name. Would that be along the same lines???
Re: Counting characters in a string without an array...
No bother Snake... Thanks for the tip... :)
Re: Counting characters in a string without an array...
Sounds like it could have some techniques that you could use.
Re: Counting characters in a string without an array...
Thanks loads dude. I have been looking at it for the past few hours and just couldn't think of the solution.