13 Replies Latest reply: Aug 7, 2010 12:55 PM by 843789 RSS

    Need help with String Count

    843789
      Hi Everybody, I'm fairly new here and need help. I'm not that good with in depth coding with Java. I can do simple code though. I've compiled a code that opens a file but I don't understand how to get it to count how many Strings are the selected file. I tried using (Whitespace + 1) to search the whitespaces of the file and add 1 to give me the number of Strings but it didn't get the number from the selected file. Here's my code:
      import java.io.*;
      import java.util.Scanner;
      
      public class WordCountAttempt
      {
           public static void main(String[] args) throws IOException
           {
                char[] array;
                Scanner inputFile;
                int whitespaces = 0;
                
                Scanner kb = new Scanner(System.in);
                System.out.println("Enter a Filename");
                String filename = kb.nextLine();
                File file = new File(filename);
                inputFile = new Scanner(file);
                
                array = filename.toCharArray();
                
                while(inputFile.hasNext())
                {
                     String words = inputFile.nextLine();
                     System.out.println(words);
                }
                
                for (int i = 0; i < array.length; i++)
                {
                     if (Character.isWhitespace(array))
                          whitespaces++;
                }
                
                System.out.println("The file " + filename + " contains " + (whitespaces + 1) + " String(s).");
           }
      }
      Can anybody (Besides Melanie Green) show me where I went wrong and explain what I could've done to fix it?
      
      Thank You very much.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        • 1. Re: Need help with String Count
          843789
          chichideadly15 wrote:
          Hi Everybody, I'm fairly new here and need help.
          Okay a couple of things here.
          Can anybody (Besides Melanie Green)
          Don't do that. Carrying on grudges for weeks doesn't help anyone, especially you.

          Now I'm not entirely clear what you are trying to do here. Get a word count from the file I think. Well first of all there is this
          array = filename.toCharArray();
          // snipped
          for (int i = 0; i < array.length; i++)
          {
               if (Character.isWhitespace(array))
               whitespaces++;
          }
          System.out.println("The file " + filename + " contains " + (whitespaces + 1) + " String(s).");

          What do you think is happening there? What is that code actually doing? Hint: It's doing nothing when it comes to looking at the contents of the file.

          To be honest that loop is totally useless as is that array. You need to do something in that other loop with each String representing a line of text from the file.
          • 2. Re: Need help with String Count
            843789
            So it needs something like a .hasNext()?
            • 3. Re: Need help with String Count
              843789
              cotton.m wrote:
              Can anybody (Besides Melanie Green)
              Don't do that. Carrying on grudges for weeks doesn't help anyone, especially you.
              Actually I for one appreciate his doing this. It cues me in to read his post history, decide that Melanie had a valid point, and let me move on to another thread. ;)
              • 4. Re: Need help with String Count
                843789
                chichideadly15 wrote:
                So it needs something like a .hasNext()?
                No.

                If you want to actually process (count) the content of the file then you need to actually process the content of the file. Not the name of the file.
                • 5. Re: Need help with String Count
                  843789
                  Oh ok. I see what you mean. How would I go about reading the contents of a file?
                  • 6. Re: Need help with String Count
                    843789
                    chichideadly15 wrote:
                    Oh ok. I see what you mean. How would I go about reading the contents of a file?
                    I'm sorry is this a joke? You already are reading the contents of a file!
                    • 7. Re: Need help with String Count
                      843789
                      I'm sorry. I don't understand Java very well. I need help so I came here. I can do simple programs but detailed programs have me very confused. :(
                      • 8. Re: Need help with String Count
                        843789
                        chichideadly15 wrote:
                        I'm sorry. I don't understand Java very well. I need help so I came here. I can do simple programs but detailed programs have me very confused. :(
                        The. Code. You. Already. Posted. Is. Reading. A. File.

                        Do you see that?
                        • 9. Re: Need help with String Count
                          843789
                          To make your program do what you want I took your source, deleted 7 lines of stuff you don't need. Added one line that you do need and it does exactly what you want.

                          One line of code is all you need to add. That's it. I suspect you didn't write the code you posted to be honest.
                          • 10. Re: Need help with String Count
                            843789
                            Yeah I see what you're saying but it's not counting the number of Strings that are in it. I needed to write a program that asked the user to enter the name of a file and return the number of Strings that are in it but everything I tried didn't work. I thought that counting the Whitespaces + 1 would work but I now see that I did that code incorrectly. So, do I need to count the Whitespaces of the file contents?
                            • 11. Re: Need help with String Count
                              843789
                              I wrote the code but I had to use the book for reference.
                              • 12. Re: Need help with String Count
                                843789
                                chichideadly15 wrote:
                                Yeah I see what you're saying but it's not counting the number of Strings that are in it. I needed to write a program that asked the user to enter the name of a file and return the number of Strings that are in it but everything I tried didn't work. I thought that counting the Whitespaces + 1 would work but I now see that I did that code incorrectly. So, do I need to count the Whitespaces of the file contents?
                                Yes!

                                But it's even easier than that. If you want to count words then just count words. You can use the split method of Strings to help you with that...
                                • 13. Re: Need help with String Count
                                  843789
                                  Ok I'll try that. Thank you for your help. Sorry for not understanding detailed Java. I'm just having a really difficult time understanding deep detailed code. I was fine up until it got to creating classes and objects by using value returning methods. I understood everything else after creating arrays except creating detailed classes and objects and ever since then, I've been getting more and more confused. I honestly do appreciate you helping me. I will try that method.

                                  Also, I apologize to Melanie Green.