6 Replies Latest reply: Mar 4, 2014 2:35 PM by TPD-Opitz RSS

    array words count

    896976

      Hi guys

      This code is giving me this output

      The New York Times is an American daily newspaper founded and continuously published in New York City since September 18 1851 It has won 112 Pulitzer Prizes more than any other news organization 0

      I need to read and split the array (I believe that part works)

      I also need to count the words and display it like this

      The(1)  New(2) York(2) Times(1) is (1) ...etc

       

      I am   System.out.print(token + " "  ) ; then I need to count the words and display the number in front

       

      Thank you

       

      package test4;

      import java.util.StringTokenizer;

      /**
      *
      * @author rechever
      */
      public class Test4 {

          /**
           * @param args the command line arguments
           */
          public static void main (String[] args) {
             System.out.println("Displaying a paragraph: ”): ");
                
                  String sentence1 = "The New York Times is an American daily newspaper, founded and continuously published "
                          + "in New York City since September 18, 1851. It has won 112 Pulitzer Prizes, more than any other news organization";
                 
          //        String sentence1  = scanner.nextLine();
            /* regular expression   */
            sentence1 = sentence1.replaceAll("\\,"," ");
            sentence1 = sentence1.replaceAll("\\."," ");
            sentence1 = sentence1.replaceAll("\\;"," ");

                  StringTokenizer stt = new StringTokenizer(sentence1," ");
                // int countWords = 1;  
                  while
                      (stt.hasMoreTokens()){
                      String token = stt.nextToken();
                     System.out.print(token + " "  ) ;
                   // String  value = sentence1;
                   //  String value = token;
                //  insertNode(value);
                 
                 
                  }
                //  insertNode(value);
             
                  for(int countWords = 0; countWords < sentence1.length(); countWords++ ) {
           
             if(sentence1.isEmpty()) {
                  }
               System.out.println(countWords);
                 }
                  } // end of Main  

      }

        • 1. Re: array words count
          TPD-Opitz

          what is your actual question here?

           

          bye

          TPD

          • 2. Re: array words count
            896976

            I need to add counts:


            This code is giving me this output:

            The New York Times is an American daily newspaper founded and continuously published in New York City since September 18 1851 It has won 112 Pulitzer Prizes more than any other news organization 0


             

            I also need to count the words and display it like this

            The(1)  New(2) York(2) Times(1) is (1) ...etc


            I need to counts the words in the array after I split it (tokenize)


            • 3. Re: array words count
              TPD-Opitz

              I understand what you need, but there is still no question to answer.

               

              Please understand that this is not a free homework service. You have to come up with solutions for your problem. We will help you to make them run.

               

              bye

              TPD

              • 4. Re: array words count
                896976

                Believe I try before I ask, if you see the code I was able to split the array, I am having problems with the counts and displaying the results in front of the words..

                as you can see I initiated the loop, I just can not get it to work...

                 

                for(int countWords = 0; countWords < sentence1.length(); countWords++ ) {

                     

                       if(sentence1.isEmpty()) {

                            }

                         System.out.println(countWords);

                           }

                            } // end of Main 


                I need to able to split the array

                StringTokenizer stt = new StringTokenizer(sentence1," ");

                          // int countWords = 1;  

                            while

                                (stt.hasMoreTokens()){

                                String token = stt.nextToken();

                               System.out.print(token + " "  ) ;

                then after the array split I need to counts the words,

                this 

                for(int countWords = 0; countWords < sentence1.length(); countWords++ ) {

                     

                       if(sentence1.isEmpty()) {

                            }

                         System.out.println(countWords);

                           }

                is not doing the trick..

                I am in the business of learning, I am not looking for a free ride...I google it, I look in books


                This code is doing it but I have to call a method to insert the array in a Binary tree

                something like tree.insertNode (value)

                being value the data stoom the split array....


                Now if you want to give some ideas (a link, documentation anything of how to call a method from  a class that is using maps...that will be great

                I though that doing it in a simple array was easy... again, I just learning

                Thank you



                import java.io.PrintStream;

                import java.util.*;

                 

                 

                public class WordTypeCount

                {

                   public static void main( String[] args )

                   {

                      // create HashMap to store String keys and Integer values

                      Map< String, Integer > myMap = new HashMap< String, Integer >();

                 

                 

                      createMap( myMap ); // create map based on user input

                      displayMap( myMap ); // display map content

                   } // end main

                 

                 

                   // create map from user input

                   private static void createMap( Map< String, Integer > map )

                   {

                      Scanner scanner = new Scanner( System.in ); // create scanner

                      System.out.println( "Please Enter the sentence from the the from Edgar Allen Poe’s “Fall of the House of Usher”):" ); // prompt for user input

                      String input = scanner.nextLine();

                 

                 

                      // tokenize the input

                         

                      String[] tokens = input.split( " ");

                              

                      // processing input text

                      for ( String token : tokens )

                      {

                         String word = token.toString(); 

                                 ///toLowerCase(); // get lowercase word

                                 

                         // if the map contains the word

                         if ( map.containsKey( word ) ) // is word in map

                         {

                            int count = map.get( word ); // get current count

                            map.put( word, count + 1 ); // increment count

                         } // end if

                         else

                            map.put( word, 1 ); // add new word with a count of 1 to map

                      } // end for

                   } // end method createMap

                  

                   // display map content

                   private static void displayMap( Map< String, Integer > map )

                   {     

                      Set< String > keys = map.keySet(); // get keys

                 

                 

                      // sort keys

                      TreeSet< String > sortedKeys = new TreeSet<  >( keys );

                 

                 

                      System.out.println( "\nMap contains:\nKey\t\tValue");

                     

                   

                      // generate output for each key in map

                      for ( String key : sortedKeys )

                      // System.out.printf( "%-10s%10s\n", key, map.get( key ) );

                        

                      System.out.printf( "%-5s%-5s", key,map.get( key ) );

                                     

                     // System.out.printf("\nsize: %d\nisEmpty: %b\n", key, map.get( key ) );

                     

                      System.out.printf(

                         "\nsize: %d\nisEmpty: %b\n", map.size(), map.isEmpty() );

                   } // end method displayMap

                } // end class WordTypeCount


                Thank you

                  

                 

                • 5. Re: array words count
                  896976

                  Here is another try

                  it it giving me this output

                  Displaying a paragraph: ”):

                  The New York Times is an American daily newspaper founded and continuously published in New York City since September 18 1851 It has won 112 Pulitzer Prizes more than any other news organization 0

                  and the counts something like this

                  1

                  2

                  3

                  4

                  I need to be able to do something like this:

                   

                  the user entered:

                  The New  York Times, is an American daily newspaper founded and continuously published in New York City since September 18. 1851, It has won 112 Pulitzer Prizes more than any other news organization

                   

                  the array got split and counts the words after the split

                   

                  The(1) New(2)  York(2) Times(1)  is(1) ..etc..ect..count the words and the regular expresion will get rid off the , or .

                   

                   

                  public class TreeTest {

                     

                  public static void main( String[] args ) {

                       

                    //    Tree< Integer > tree = new Tree <  >();

                     //    int value = 0;

                   

                   

                        

                             System.out.println("Displaying a paragraph: ”): ");

                            

                              String sentence1 = "The New York Times is an American daily newspaper, founded and continuously published "

                                      + "in New York City since September 18, 1851. It has won 112 Pulitzer Prizes, more than any other news organization";

                             

                      //        String sentence1  = scanner.nextLine();

                        /* regular expression   */

                        sentence1 = sentence1.replaceAll("\\,"," ");

                        sentence1 = sentence1.replaceAll("\\."," ");

                        sentence1 = sentence1.replaceAll("\\;"," ");

                   

                   

                              StringTokenizer stt = new StringTokenizer(sentence1," ");

                            // int countWords = 1;  

                              while

                                  (stt.hasMoreTokens()){

                                  String token = stt.nextToken();

                                 System.out.print(token + " "  ) ;

                               // String  value = sentence1;

                               //  String value = token;

                            //  insertNode(value);

                  • 6. Re: array words count
                    TPD-Opitz

                    ok, here is a push:

                     

                    you cannot do this with a single loop. You need to use at least one Collection

                     

                    my preference was a HashSet and a List (constructed from the String array you have via Arrays.asList() method). Set.addAll() would safe you form the first iteration (finding the distinct words).

                     

                    in the next step you need to iterate over the distinct words (google "java foreach loop") and count their occurrences in the array/list (which requires another loop inside the first).

                     

                    after the inner loop print count and curerent word in desired order.

                     

                    bye

                    TPD