Just a few quick questions about Strings. Thank you!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SuperDuper
    New Member
    • Oct 2007
    • 27

    Just a few quick questions about Strings. Thank you!

    Just a few quick questions.

    1) How would I count the length of words in 5 certain strings.
    SomeString.s1, SomeString.s2, etc..

    2) How to compute the average word length?

    3) How to compute the standard deviation (without duplicating code)

    4) How to do all of this without duplicating the code for each String.

    If anyone knows the answer to any of these questions, please reply with a solution; code or not.

    PS - Arrays, and String.split, String.trim, or String tokenizers cannot be implemented.
    Thanks a lot.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by SuperDuper
    Just a few quick questions.

    1) How would I count the length of words in 5 certain strings.
    SomeString.s1, SomeString.s2, etc..

    2) How to compute the average word length?

    3) How to compute the standard deviation (without duplicating code)

    4) How to do all of this without duplicating the code for each String.

    ....
    And you don't have a clue on how to do any of them yet? Please post your ideas.

    Originally posted by SuperDuper
    ...
    PS - Arrays, and String.split, String.trim, or String tokenizers cannot be implemented.
    Thanks a lot.
    That's unfortunate.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by SuperDuper
      PS - Arrays, and String.split, String.trim, or String tokenizers cannot be implemented.
      Thanks a lot.
      That's not true: all these classes and methods are already implemented; all you
      have to do is use (invoke or call) them to make use of them. Are you the poster
      who wanted to implement the 'Nim' game without wanting to use arrays and
      methods?

      kind regards,

      Jos

      Comment

      • SuperDuper
        New Member
        • Oct 2007
        • 27

        #4
        Originally posted by JosAH
        That's not true: all these classes and methods are already implemented; all you
        have to do is use (invoke or call) them to make use of them. Are you the poster
        who wanted to implement the 'Nim' game without wanting to use arrays and
        methods?

        kind regards,

        Jos
        Hah, yes that's me.
        I've passed in the last assignment, and I had no troubles.
        I'm not allowed to implement arrays/string tokens, etc.. (theoretically) because my uncle has yet to teach us, so I can't jump ahead.

        I have an idea of how to count the words.. my current code is as follows:

        [PHP]import java.io.*;
        public class WordCount
        {
        public static void main(String SomeStrings.s1, BufferedReader in)
        {
        String line;
        long numWords = 0;
        int index = 0;
        boolean prevWhiteSpace = true;
        while(index < line.length()){
        char c = line.charAt(ind ex++);
        boolean currWhiteSpace = Character.isWhi tespace(c);
        if(prevWhiteSpa ce && !currWhiteSpace ){
        numWords++;
        }
        prevWhiteSpace = currWhiteSpace;
        }
        System.out.prin tln(numWords);
        }
        }[/PHP]

        As you can see I state in the method (String SomeStrings.s1)

        SomeStrings.s1 is part of SomeStrings.cla ss which I've placed in my Java Files folder.

        Yet when I compile this I get a nasty red error:
        Current document is out of sync with the Interactions Pane and should be recompiled!

        Thanks a lot.

        Comment

        • SuperDuper
          New Member
          • Oct 2007
          • 27

          #5
          Okay well now I have this.
          [PHP]
          import java.util.Scann er;
          public class Word
          {
          public static void main(String []args)
          {
          int count = 1;
          String line = SomeStrings.s1;
          long numWords = 0;
          int index = 0;
          boolean prevWhitespace = true;
          while (index < line.length()) {
          char c = line.charAt(ind ex++);

          boolean currWhitespace = Character.isWhi tespace(c);
          if (prevWhitespace && !currWhitespace ) {
          numWords++;
          }
          prevWhitespace = currWhitespace;
          }
          System.out.prin tln("String: s"+count);
          System.out.prin tln(numWords);
          }


          }
          [/PHP]

          Except it prints the number of words..

          How do I get the LENGTH of each word.. and store it into a temp variable for SomeStrings.s1. .. then determine the average word length?

          I can't figure that out.

          Comment

          • SuperDuper
            New Member
            • Oct 2007
            • 27

            #6
            Okay well I think I've figured out how to count words.
            My code is posted below.

            Although at the top, where I have Line = SomeStrings.s1;
            it won't refer to the .class file in my Java Folder.

            I know the SomeStrings.cla ss is properly placed because if I set up a println it *will* print out the string.

            Although I can't when set as above.

            Any thoughts?

            Code:
            [PHP]public class WordTest
            {
            public static void main(String[]args)
            {

            String Line = SomeStrings.s1;
            long stringnumber = 1;
            double WordLength = 0;
            double NewWordLength = 0;
            double PrevWordLength = 0;
            double AvWordLength = 0;
            int count = 0;
            double NumberofWords = 1;
            int index = 0;
            char a = Line.charAt(cou nt);
            boolean prevWhitespace = true;
            boolean currWhitespace = Character.isWhi tespace(a);

            //While loop number 1 (counting number of words)
            while (index < Line.length())
            {
            char c = Line.charAt(ind ex++);

            boolean currWhitespace2 = Character.isWhi tespace(c);
            if (prevWhitespace && !currWhitespace 2)
            {
            NumberofWords++ ;
            }
            prevWhitespace = currWhitespace2 ;
            }
            //While loop number 2 (counting lengths)
            while(count <= Line.length())
            {
            if(!currWhitesp ace)
            {
            NewWordLength++ ;
            }
            count++;
            }

            WordLength = NewWordLength + PrevWordLength;
            PrevWordLength = WordLength;

            AvWordLength = WordLength/NumberofWords;

            System.out.prin tln("Average Word Length: "+AvWordLength) ;

            }

            }[/PHP]

            Comment

            • SuperDuper
              New Member
              • Oct 2007
              • 27

              #7
              Can somebody please help me?
              I'm super stuck, yet I'm sure there's an easy way out.

              I want to add up the length of each word.. in the code i'll provide, it has 2 words.

              The output is:
              1.0
              2.0
              Whitespace
              1.0
              2.0
              3.0
              4.0
              Last Word Length: 4.0
              Number of words: 2.0
              Average Word Length: 2.0


              As you can see, the "2.0" before the Whitespace, is the length of the first word.
              My goal is to Add this (+) to the "4.0" which is the length of the second word.
              Though it's getting the best of me. Please help.

              [PHP]public class WordTest
              {
              public static void main(String[]args)
              {

              String Line = "hi john";
              long stringnumber = 1;
              double WordLength = 0;
              double NewWordLength = 0;
              double PrevWordLength = 0;
              double AvWordLength = 0;
              double NumberofWords = 1;
              double TotalWordLength s =0;
              int count = 0;
              int index = 0;
              boolean prevWhitespace = true;

              //While loop number 1 (counting number of words)
              while (index < Line.length())
              {
              char c = Line.charAt(ind ex++);
              boolean currWhitespace2 = Character.isWhi tespace(c);
              if (currWhitespace 2 && !prevWhitespace )
              {
              NumberofWords++ ;
              }
              prevWhitespace = currWhitespace2 ;

              }
              //While loop number 2 (counting lengths of words)
              while(count < Line.length())
              {
              char a = Line.charAt(cou nt);
              boolean currWhitespace = Character.isWhi tespace(a);

              if(!currWhitesp ace)
              {
              NewWordLength++ ;

              System.out.prin tln(NewWordLeng th);
              }
              else if (currWhitespace )
              {
              NewWordLength = 0;
              System.out.prin tln("Whitespace ");
              }

              WordLength = NewWordLength;
              AvWordLength = WordLength/NumberofWords;
              count++;
              }

              System.out.prin tln("Last Word Length: " +WordLength);
              System.out.prin tln("Number of words: " +NumberofWords) ;
              System.out.prin tln("Average Word Length: "+AvWordLength) ;

              }

              }
              [/PHP]

              Comment

              Working...