5 Replies - 1000 Views - Last Post: 22 August 2013 - 04:11 AM Rate Topic: -----

#1 salmayoussef  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-August 13

File Stream: Make a subroutine and use it to read files

Posted 21 August 2013 - 04:33 PM

Hello! I'm a Java beginner and new to this forum so I hope I can manage this properly. My assignment asks me to write a program that will:
-read a file called message.txt (ten lines of text)
-determine the total number of words in the text
-the number of vowels in each line
-the average number of words per line

I created the text and called it message.txt, as instructed. My problem is that I'm supposed to turn each of these tasks (read, number of words, number of vowels, average number of words/line) into their own subroutines and call them in the main method, but I have NO CLUE how to! Normally with simpler programs, I could do it, but I'm just learning file streaming and I'm completely confused.

Also, I need a simple code that will read and DISPLAY the text, so that it's easier to see the lines and letters. What code would I need to use for that?

Here's my program:

import java.io.BufferedReader;
import java.io.FileReader;
public class ReadZILLA{
    public static void main(String[] args){
        try{
            String file = "c:/java/bin/message.txt";
            BufferedReader br = new BufferedReader(new FileReader(file));
            int wordCount = 0;
            String line;
            char[] vowels = new char[] {'a','e','i','o','u'};
            int lineCount = 0;
            while ((line = br.readLine()) != null) {
                lineCount++;

            // count the number of words in a line
            String[] words = line.split(" ");
            if (words != null)
                wordCount+=words.length;

            // count the number of vowels in a line
            int vowelCount = 0;
            for(int i=0;i<line.length();i++){
                char a = line.charAt(i);
                for(char vowel : vowels){
                    if (a == vowel){
                        vowelCount++;
                        break;
                    }
                }
            }
            
            System.out.println("Line " + lineCount + " has " + vowelCount + " vowels.");
            }
            System.out.println("Total number of words in the text is " + wordCount + " words.");
            System.out.println("Average number of words per line is " + (wordCount/lineCount) + " words.");
            br.close();
        }
        catch(Exception ex){}
    }
}


I've read all over the net and can't find anything--everyone says something different! Any help would be great! Thanks in advance!

salmayoussef (AKA desperate)

Is This A Good Question/Topic? 0
  • +

Replies To: File Stream: Make a subroutine and use it to read files

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 402
  • View blog
  • Posts: 1,004
  • Joined: 14-November 12

Re: File Stream: Make a subroutine and use it to read files

Posted 21 August 2013 - 05:05 PM

Not quite sure I'm understanding what problem you are having but..

Subroutines technically don't exist in Java. Only methods.
class Yada {
   public static void main(String[] args) {
      doThis(); //"subroutine" 1
      doThat(); //"subroutine" 2
      doSomethingCrazyAndHip(); //"subroutine 3
   }
   
   //these methods are called by the main method, in order to keep things tidy and readable
   static void doThis() {
      //code
   }
   static void doThat() {
      //code
   }
   static void doSomethingCrazy() {
      //code
   }
}

I expect that your tutor will be looking for this kind of design. It promotes readability and maintainability by keeping tasks separate and tidy.

Also, your indentation is slightly wrong, causing a little confusion around your while loop. (Will have no effect on the program but will impair your debugging skills)

This post has been edited by Flukeshot: 21 August 2013 - 06:08 PM
Reason for edit:: see next post

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: File Stream: Make a subroutine and use it to read files

Posted 21 August 2013 - 05:25 PM

Sorry but doThis() doThat() and doSomethingCrazyAndHip() are class method so you can't call them from the main() method as you snippet does

you will rather need to

class TextFileCounter {

     static final char[] voyel = {'a', 'e', 'i', 'o', 'u'};
     int[] nbEachVoyel = new int[voyel.length];
     int nbLine;
     int maxWordsInLine;
     

     TextFileCounter(String fileName) {
        ... open file
        ... loop while there is line
            ++nbLine;
            countWord(line);
            countVoyel(line);
        }
        presentResults();
     }

     void countWord(String line) {
        String[] word = line.split(" ");
        if(word.length > maxWordsInLine)
           maxWordInLine = word.length;
     }

     void countVoyel(String line) {
        for(int i = 0; i < line.length; ++i) {
           char letter = line.charAt(i);
           if(letter == ' ')
              continue;
           ... test if voyel update counter nbEachVoyel[]
        }
     }

     void presentResults() {
         System.out.println("In the " + nbLine + " the max words in a line is: " + maxWordInLine);
        for( loop for the voyel counter...
     }

     public static void main(String[] args) {
       new TextFileCounter("bla.txt");
     }
}


Was This Post Helpful? 2
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 402
  • View blog
  • Posts: 1,004
  • Joined: 14-November 12

Re: File Stream: Make a subroutine and use it to read files

Posted 21 August 2013 - 06:06 PM

My apologies, this is true, but I merely aim to describe separation of main and "subroutine", I'll edit them as static to fix the issue, shall I? :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: File Stream: Make a subroutine and use it to read files

Posted 22 August 2013 - 03:36 AM

then all your counters will have to be declared static too
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,645
  • Joined: 20-September 08

Re: File Stream: Make a subroutine and use it to read files

Posted 22 August 2013 - 04:11 AM

Avoid using any more that one static method (main) in a Java class unless you have very good reasons. That main method should have at most just a few lines of code. Java is an OO language and shouldn't be used procedurally

This post has been edited by g00se: 22 August 2013 - 04:12 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1