Results 1 to 9 of 9
  1. #1
    shine1986 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Exclamation 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......

    Java 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 ++ ;
    }
    Last edited by shine1986; 06-28-2012 at 09:44 PM. Reason: added code

  2. #2
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: counting characters in a string without an array...

    Java Code:
    String someString = "Some Data";
    int totalCharacters = someString.length();
    might get you started ;-)

  3. #3
    shine1986 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default 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...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default 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.
    Last edited by Norm; 06-28-2012 at 09:33 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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! ...

  6. #6
    shine1986 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default 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???

  7. #7
    shine1986 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Counting characters in a string without an array...

    No bother Snake... Thanks for the tip... :)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Counting characters in a string without an array...

    Sounds like it could have some techniques that you could use.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    shine1986 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default 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.

Similar Threads

  1. Counting characters
    By Reine in forum New To Java
    Replies: 5
    Last Post: 09-28-2011, 10:04 PM
  2. Replies: 4
    Last Post: 11-05-2010, 11:43 AM
  3. Counting char in String
    By j@v@ in forum New To Java
    Replies: 2
    Last Post: 10-26-2010, 03:29 PM
  4. Creating Array of Characters From a String
    By ngc0202 in forum New To Java
    Replies: 6
    Last Post: 07-30-2009, 08:30 AM
  5. Counting characters
    By Tiff89 in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 10:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •