1 2 3 4 Previous Next 55 Replies Latest reply: Jun 26, 2007 7:29 PM by 807600 RSS

    Array Question

    807600
      Can someone please help me with this code? I'm not to good with arrays yet, and I'm trying I can't figure out why this prints out a line of nulls. When I put the displayArray method within the for loop, it prints out the correct word values, but otherwise I get rows of nulls. Also, if there are any other obvious errors please fix. Thank you so much.



      import java.io.*;
      import java.util.*;
      import java.text.*;

      public class ZinniSpeech
      {
           private static String[] originalZinni;
           private static Scanner rawText;
           private static StringBuilder rawTextString;
           private static StringTokenizer rawTextToken;
           private static int wordTotal1;
           private static int wordTotalFinal;

           public static void main(String args[])throws IOException
           {
                rawText = new Scanner(new File(
                     "\\Java\\Zinni\\ZinniSpeechCDI5_04.txt"));

                rawText.useDelimiter("\\W+");

                rawTextString = new StringBuilder();

                while(rawText.hasNext())
                {
                     rawTextString.append(" " + rawText.next());
                }

                rawText.close();

                rawTextToken = new StringTokenizer(rawTextString.toString());

                wordTotal1 = rawTextToken.countTokens();

                for (int x = 0; x < wordTotal1; x++)
                {
                     String[] tempArray = new String[wordTotal1];

                     tempArray[x] = rawTextToken.nextToken();

                     if (!(tempArray[x].equals("")))
                     if (!(tempArray[x].equals("s")))
                     if (!(tempArray[x].equals("t")))
                     if (!(tempArray[x].equals("ve")))
                     if (!(tempArray[x].equals("re")))
                     if (!(tempArray[x].equals("m")))
                     if (!(tempArray[x].equals("ll")))
                     if (!(tempArray[x].equals("null")))
                     {
                     originalZinni = tempArray;

                     String lowerCase = new String(originalZinni[x]);
                     
                     originalZinni[x] = lowerCase.toLowerCase();
                     }
                }

                displayList(tempArray);
           }

           private static void displayList(String[] originalZinni)
           {
                for(String word: originalZinni)
                {
                     System.out.println(word);
                }
           }
           
      }

      Message was edited by:
      minkus46
        • 1. Re: Array Question
          807600
          There, I cut some of the worthless stuff out.
          import java.io.*;
          import java.util.*;
          import java.text.*;

          public class ZinniSpeech
          {
               private static String[] originalZinni;
               private static Scanner rawText;
               private static StringBuilder rawTextString;
               private static StringTokenizer rawTextToken;
               private static int wordTotal1;
               private static int wordTotalFinal;

               public static void main(String args[])throws IOException
               {
                    rawText = new Scanner(new File(
                         "\\Java\\Zinni\\ZinniSpeechCDI5_04.txt"));

                    rawText.useDelimiter("\\W+");

                    rawTextString = new StringBuilder();

                    while(rawText.hasNext())
                    {
                         rawTextString.append(" " + rawText.next());
                    }

                    rawText.close();

                    rawTextToken = new StringTokenizer(rawTextString.toString());

                    wordTotal1 = rawTextToken.countTokens();

                    for (int x = 0; x < wordTotal1; x++)
                    {
                         String[] tempArray = new String[wordTotal1];

                         tempArray[x] = rawTextToken.nextToken();

                         originalZinni = tempArray;

                         String lowerCase = new String(originalZinni[x]);
                         
                         originalZinni[x] = lowerCase.toLowerCase();
                    }

                    displayList(tempArray);
               }

               private static void displayList(String[] originalZinni)
               {
                    for(String word: originalZinni)
                    {
                         System.out.println(word);
                    }
               }
               
          }
          • 2. Re: Array Question
            807600
            When you post code, please wrap it in [code][/code] tags. It makes it much easier to read. Also indent it sanely.

            This:
            if (!(tempArray[x].equals("")))
            if (!(tempArray[x].equals("s")))
            if (!(tempArray[x].equals("t")))
            if (!(tempArray[x].equals("ve")))
            if (!(tempArray[x].equals("re")))
            if (!(tempArray[x].equals("m")))
            if (!(tempArray[x].equals("ll")))
            if (!(tempArray[x].equals("null")))
            is hideous and bug-prone. Better to have one boolean statement connected with && than this degree of nested if statements.

            My guess is that your problem derives from this part:
            for (int x = 0; x < wordTotal1; x++)
            {
                String[] tempArray = new String[wordTotal1];
            You create this array once per loop iteration, and then apparently only use one position in the array each time. The remaining positions are filled with nulls, of course. You probably want to allocate this array once, outside the loop.
            • 3. Re: Array Question
              807600
              First off, please put the code into [ code ] tags for readability.
              for (int x = 0; x < wordTotal1; x++) {
              String[] tempArray = new String[wordTotal1];
              tempArray[x] = rawTextToken.nextToken();
              The issue here is that you're creating a new, blank array every time you go through the loop. Then you insert one value, check it, and convert it to lowercase.

              I don't even understand how this doesn't throw an error at compile time:
              displayList(tempArray);
              Since tempArray is declared only within the for loop, it shouldn't exist at this point.
              • 4. Re: Array Question
                807600
                I'm sorry guys, this is a very rough draft. I was playing around with the code. This is really what it's supposed to look like. I wasn't trying to reference the temporary variable. I just put it in there to see if I got the same result as originalZinni did, then I forgot to change it. Still returning nulls like this though.

                import java.io.*;
                import java.util.*;
                import java.text.*;
                
                public class ZinniSpeech
                {
                     private static String[] originalZinni;
                     private static Scanner rawText;
                     private static StringBuilder rawTextString;
                     private static StringTokenizer rawTextToken;
                     private static int wordTotal1;
                     private static int wordTotalFinal;
                
                     public static void main(String args[])throws IOException
                     {
                          rawText = new Scanner(new File(
                               "\\Java\\Zinni\\ZinniSpeechCDI5_04.txt"));
                
                          rawText.useDelimiter("\\W+");
                
                          rawTextString = new StringBuilder();
                
                          while(rawText.hasNext())
                          {
                               rawTextString.append(" " + rawText.next());
                          }
                
                          rawText.close();
                
                          rawTextToken = new StringTokenizer(rawTextString.toString());
                
                          wordTotal1 = rawTextToken.countTokens();
                
                          for (int x = 0; x < wordTotal1; x++)
                          {
                               String[] tempArray = new String[wordTotal1];
                
                               tempArray[x] = rawTextToken.nextToken();
                
                               originalZinni = tempArray;
                
                               String lowerCase = new String(originalZinni[x]);
                               
                               originalZinni[x] = lowerCase.toLowerCase();
                          }
                
                          displayList(originalZinni);
                     }
                
                     private static void displayList(String[] originalZinni)
                     {
                          for(String word: originalZinni)
                          {
                               System.out.println(word);
                          }
                     }
                     
                }
                • 5. Re: Array Question
                  807600
                  Wait, not all nulls. There is a long line of nulls returned, then the last spot in the array is returned correctly.
                  • 6. Re: Array Question
                    807600
                    Thanks for the formatting.
                    for (int x = 0; x < wordTotal1; x++)
                    {
                         String[] tempArray = new String[wordTotal1];
                         tempArray[x] = rawTextToken.nextToken();
                         originalZinni = tempArray;
                         String lowerCase = new String(originalZinni[x]);
                         originalZinni[x] = lowerCase.toLowerCase();
                    }
                    Okay, so the problem is still here. You're creating a new, blank array every time you go through the loop, and then you assign it to originalZinni. When you use the assignment (=) operator on an object, it doesn't create a new object. Instead, both variables refer to the same object. Also, even if it did create a new array, all the nulls in each new tempArray would get copied over to originalZinni everytime anyways.

                    Just get rid of the tempArray and write directly to originalZinni.

                    This should work:
                    originalZinni = new String[wordTotal1];
                    for (int x = 0; x < wordTotal1; x++)
                    {
                         originalZinni[x] = rawTextToken.nextToken().toLowerCase();
                    }
                    • 7. Re: Array Question
                      807600
                      Hey, thanks a lot. Wow, that was kind of a dumb mistake. I need to check more carefully. I've just had a long day and was pretty frustrated. If I have any more questions I'll post on the same thread, but thanks again.
                      • 8. Re: Array Question
                        807600
                        All right, another question. I wrote more code that searches for word repetitions and then creates a parallel area to keep track of word repetitions. But it displays "word" = 1
                        "word" = 2
                        "word1" = 1
                        "word1" = 2
                        "word1" = 3
                        And so forth. How do I delete everything except for the last number, and then trim the arrays? Here's my code for the whole thing:
                        import java.io.*;
                        import java.util.*;
                        import java.text.*;
                        
                        public class ZinniSpeech
                        {
                             private static String[] originalZinni;
                             private static int[] wordCount;
                             private static Scanner rawText;
                             private static StringBuilder rawTextString;
                             private static StringTokenizer rawTextToken;
                             private static int wordTotal1;
                             private static int wordTotalFinal;
                             private static int count = 0;
                             
                             public static void main(String args[])throws IOException
                             {
                                  rawText = new Scanner(new File(
                                       "\\Java\\Zinni\\ZinniSpeechCDI5_04.txt"));
                        
                                  rawText.useDelimiter("\\W+");
                        
                                  rawTextString = new StringBuilder();
                        
                                  while(rawText.hasNext())
                                  {
                                       rawTextString.append(" " + rawText.next());
                                  }
                        
                                  rawText.close();
                        
                                  rawTextToken = new StringTokenizer(rawTextString.toString());
                        
                                  wordTotal1 = rawTextToken.countTokens();
                        
                                  originalZinni = new String[wordTotal1];
                        
                                  for (int x = 0; x < wordTotal1; x++)
                                  {
                        
                                       originalZinni[x] = rawTextToken.nextToken();
                        
                                       if(!(originalZinni[x].equals("")))
                                       if(!(originalZinni[x].equals("s")))
                                       if(!(originalZinni[x].equals("t")))
                                       if(!(originalZinni[x].equals("ve")))
                                       if(!(originalZinni[x].equals("re")))
                                       if(!(originalZinni[x].equals("m")))
                                       if(!(originalZinni[x].equals("ll")))
                                       if(!(originalZinni[x].equals("null")))
                                       {
                        
                                       String lowerCase = new String(originalZinni[x]);
                        
                                       originalZinni[x] = lowerCase.toLowerCase();
                                       }
                                  }
                        
                                  Arrays.sort(originalZinni);
                        
                                  wordCount = new int [wordTotal1];
                        
                                  for(int x = 0; x < wordTotal1; x++)
                                  {
                                       if(originalZinni[x].contentEquals(originalZinni[x++]))
                                       {
                                            wordCount[x] = count++;
                                       }
                                       
                                       else if(!originalZinni[x].contentEquals(originalZinni[x++]))
                                       {
                                            wordCount[x] = count++;
                                            count = 0;
                                       }
                                  }
                        
                                  for(int x = 0; x < wordTotal1; x++)
                                  {
                                       System.out.println(originalZinni[x] + " = " + wordCount[x]);
                                  }
                             }
                        }
                        • 9. Re: Array Question
                          807600
                          Since I haven't seen an adequate discription of what you are trying to do, perhaps you could post that.
                          • 10. Re: Array Question
                            807600
                            You might want to reconsider your use of the ++ postfix operator.
                            Keep in mind that it permanently adds to the value of the variable. and only after the statement has executed.

                            Now, what I would do to get rid of the duplicate entries is create a new array that contains only the unique ones, adding each unique entry and the number of times it appears as it's going through and counting the first array.

                            Local variables are faster than class variables, by the way. You really don't need to declare class variables unless they're needed outside of the context of a single method.

                            Oh, and when will I be able to get those dukes, eh? >>
                            • 11. Re: Array Question
                              807600
                              Thanks, here's the basic description

                              Analyze the text of the speech with an alphabetized list of all words and the count of their occurrences. (word = 1) I also need to use parallel arrays. One for the alphabetized list of words, and one for their counts. I also need to calculate the total number of all words, unique words, and the average size(character amount) of all words. I can do those calculations though, I'm just having trouble with the arrays.

                              Message was edited by:
                              minkus46
                              • 12. Re: Array Question
                                807600
                                When I change it to x + 1, I get an arrayIndexOutOfBoundsException.
                                • 13. Re: Array Question
                                  807600
                                  Could I have some help please?
                                  • 14. Re: Array Question
                                    807600
                                    When I change it to x + 1, I get an arrayIndexOutOfBoundsException.
                                    Think about what you're trying to do. If you change it to x+1, then first it compares array[0] with array[1]. Good. Then it moves on to array[1] with array[2]. Also good. Then eventually it gets to the end of the array, where it tries to compare array[last_index] with array[last_index + 1]. Oops. There's no element after the last one. How might we fix that?
                                              for(int x = 0; x < wordTotal1; x++) {
                                                   if(originalZinni[x].contentEquals(originalZinni[x++])) {
                                                        wordCount[x] = count++;
                                                   }
                                                   else if(!originalZinni[x].contentEquals(originalZinni[x++])) {
                                                        wordCount[x] = count++;
                                                        count = 0;
                                                   }
                                              }
                                    Here, if you want to list only once each unique word found, consider the place in the code where you say, "count = 0;." If it reaches that point, then obviously, you've found all copies of a certain word, and are now about to move on to the next unique word. So make another array or list, and stick that unique word into that array, which should only contain each word once. Keep track of the number of words already in there so that you know which index to put the next word in. Number the wordCount to match this new array/list of words. Easy, yes?
                                    1 2 3 4 Previous Next