1 2 3 4 Previous Next 55 Replies Latest reply: Apr 9, 2007 5:14 PM by 807606 RSS

    Java loading text file and doing stuff help

    807606
      Hello,
      Can anyone help me with this, I just don't know where to start..

      I need to create a java program which will load up a text file. The text file must be no bigger than 1000 characters (so somewhere there needs to be an exception I am guessing)
      Once loaded, it needs to run some methods on the text file which each return a single statistical count, the methods need to count things like number of white space, number of vowels, frequency of each vowel (this would not need to return a single count but maybe update some sort of array?) , number of printable characters and number of consonants.

      Any help would be very good.

      Dean
        • 1. Re: Java loading text file and doing stuff help
          Puce
          Have a look at the tutorial:
          http://java.sun.com/docs/books/tutorial/essential/io/index.html

          -Puce
          • 2. Re: Java loading text file and doing stuff help
            807606
            Have a look at this link. You'll get a lot of code you might need.

            http://leepoint.net/notes-java/
            • 3. Re: Java loading text file and doing stuff help
              807606
              Thanks I am looking now
              • 4. Re: Java loading text file and doing stuff help
                807606
                Hmm, I have had a good look through, but I am not completely clear on how to start this.

                I had a good go on the sun website, there were lots of tutorials on there, i started with the CopyCharacters.java one.

                And then tried some others. There are lots of useful examples, but I am still not so sure about how to implement this.

                Dean
                • 5. Re: Java loading text file and doing stuff help
                  807606
                  I need to create a java program which will load up a
                  text file. The text file must be no bigger than 1000
                  characters (so somewhere there needs to be an
                  exception I am guessing)
                  You will need to use java.io.BufferedReader
                  Once loaded, it needs to run some methods on the text
                  file which each return a single statistical count,
                  For each br.readLine() you do chatAt()
                  the methods need to count things like number of white
                  space, number of vowels, frequency of each vowel
                  then just keep a count for each of the above e,g noVowel, noRepeat e.t.c
                  (this would not need to return a single count but
                  maybe update some sort of array?)
                  , number of
                  printable characters and number of consonants.

                  Any help would be very good.
                  Is that OK
                  • 6. Re: Java loading text file and doing stuff help
                    807606
                    Thanks for the reply.

                    The only thing I am not too sure about is the

                    For each br.readLine() you do chatAt()

                    I have never used a chatAt() before. And the search on the sun website is not comming up with any results for it.


                    Many thanks for your reply.
                    • 7. Re: Java loading text file and doing stuff help
                      Puce
                      - read the characters using a FileReader (maybe wrap it with a BufferedReader)
                      - e.g. use Maps (for the statistics) and the Character class (for information about the character) to get your information.
                      - tell us where you stuck

                      -Puce

                      Message was edited by:
                      Puce
                      • 8. Re: Java loading text file and doing stuff help
                        807606
                        while (br.ready()){
                            String s=br.readLine();
                            for(int i=0;i<s.lenght();i++){
                                if(s.chart(i)=='a'){
                                      countVolA++;
                                }
                            }
                        i above code is not tested. it just an idea
                        • 9. Re: Java loading text file and doing stuff help
                          807606
                          Thanks for the help so far
                          But first things first, I should start at the beginning.

                          I am thinking maybe start with something like from the Sun tutorial for 'CopyCharacters.java'.

                          So we have the first part of the program that loads up the txt file....
                          (I really would like to have GUI for this, but I can worry about that once the actual program is working

                          import java.io.FileReader;
                          import java.io.FileWriter;
                          import java.io.IOException;

                          public class CopyCharacters {
                          public static void main(String[] args) throws IOException {
                          FileReader inputStream = null;
                          FileWriter outputStream = null;

                          try {
                          inputStream = new FileReader("input_characters.txt");


                          Right, now its time to actually read what is in the file and use some of the statements you have suggested, can anyone help me get started with this?

                          Dean
                          • 10. Re: Java loading text file and doing stuff help
                            807606
                            Thanks for the help so far
                            But first things first, I should start at the
                            beginning.

                            I am thinking maybe start with something like from
                            the Sun tutorial for 'CopyCharacters.java'.

                            So we have the first part of the program that loads
                            up the txt file....
                            (I really would like to have GUI for this, but I
                            can worry about that once the actual program is
                            working


                            import java.io.FileReader;
                            import java.io.FileWriter;
                            import java.io.IOException;

                            public class CopyCharacters {
                            public static void main(String[] args) throws
                            IOException {
                            FileReader inputStream = null;
                            FileWriter outputStream = null;

                            try {
                            inputStream = new
                            FileReader("input_characters.txt");


                            Right, now its time to actually read what is in the
                            file and use some of the statements you have
                            suggested, can anyone help me get started with this?

                            Dean
                            public class DeanHomeworkAssignment {
                            public static void main(String args[]) {
                            //Put your code here
                            }
                            }
                            There ya go, that should get you started.

                            Start by writing the code to open the file.

                            This isn't that difficult.
                            • 11. Re: Java loading text file and doing stuff help
                              807606
                              Ok, how does this look?

                              public class FileAnalyzer {
                                        
                                   public static void main(String args[]) {
                                   
                                   FileReader fr = new FileReader("file.txt");
                                   
                                   }
                              }
                              • 12. Re: Java loading text file and doing stuff help
                                807606
                                good very good..
                                • 13. Re: Java loading text file and doing stuff help
                                  807606
                                  The only problem at the moment, is the small part of code I have done so far, does not compile well. I am getting the following errors.. (I know the code is not finished, but the compiler is having problems with 'FileReader'




                                  --------------------Configuration: <Default>--------------------
                                  FileAnalyzer.java:6: cannot find symbol
                                  symbol : class FileReader
                                  location: class FileAnalyzer
                                  FileReader fr = new FileReader("file.txt");
                                  ^
                                  FileAnalyzer.java:6: cannot find symbol
                                  symbol : class FileReader
                                  location: class FileAnalyzer
                                  FileReader fr = new FileReader("file.txt");
                                  ^
                                  2 errors

                                  Process completed.


                                  • 14. Re: Java loading text file and doing stuff help
                                    807606
                                    Hint: do you know about "import" statements?
                                    1 2 3 4 Previous Next