printing vowels and counting them from textfile

using exception handling (try/catch/finally block)

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 5627 Views - Last Post: 26 April 2010 - 08:51 AM Rate Topic: -----

#1 tennisguru09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 12-September 09

printing vowels and counting them from textfile

Posted 25 April 2010 - 11:58 AM

Hi,
Am having problems: reading from an input file "input.txt" text and
printing out the number of wordCount to a"output.txt" file
printing and counting out the vowels to a"output.txt" file(same one as the word count)
printing and counting out the preposition to a"output.txt" file(same one as the word count)

this is what i have now
import java.io.*;
public class FileIO {
	public static void main(String[] args) throws IOException {
		countWords();
		countVowels();
		countPrepositions();
	}
	public static void countWords() throws IOException {
		FileReader inputStream = null;
		FileWriter outputStream = null;
		String fileContents = "G:\\Java 1302\\project\\FILEIO\\src\\output.txt";
		
		try {
			inputStream = new FileReader(
					"G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
			outputStream = new FileWriter(
					"G:\\Java 1302\\project\\FILEIO\\src\\ouput.txt");

			BufferedReader br = new BufferedReader(inputStream);
			String lineRead = br.readLine();
			String wdcnt = " ";
			while (lineRead != null) {
				fileContents = fileContents + "\n" + lineRead;
				lineRead = br.readLine();
				// count words and display the count
				String[] wordList = fileContents.split("[\\s\\n]");
				// int size = wordList.length;
				wdcnt = (" the total word count is " + (wordList.length - 2));
			}
			// display on console word count
			System.out.println(wdcnt);

			// print into new file
			outputStream.write(wdcnt);
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
			if (outputStream != null) {
				outputStream.close();
			}
		}
	}
	public static void countVowels() throws IOException {
		FileReader inputStream = null;
		FileWriter outputStream = null;
		String fileContents = "G:\\Java 1302\\project\\FILEIO\\src\\output.txt";
		try {
			inputStream = new FileReader(
					"G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
			outputStream = new FileWriter(
					"G:\\Java 1302\\project\\FILEIO\\src\\ouput.txt");

			BufferedReader br = new BufferedReader(inputStream);
			int numOfVowels = 0;
			String lineRead = br.readLine();
			while (lineRead != null) {
				char[] ch = lineRead.toCharArray();
				for (int i = 0; i < lineRead.length(); i++) {
					char c = lineRead.charAt(i);
					if (c == 'a' || c == 'A' || c == 'e' || c == 'E'
							|| c == 'i' || c == 'I' || c == 'o' || c == 'O'
							|| c == 'u' || c == 'U') {
						numOfVowels++;
					}
					// display on console word count
					System.out.println("number of vowels in file = "
							+ numOfVowels);
					// print into new file
					outputStream.write("number of vowels in file = "
							+ numOfVowels);
				}
				lineRead = br.readLine();
			}
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
			if (outputStream != null) {
				outputStream.close();
			}
		}
	}
	public static void countPrepositions() throws IOException {
		System.out.println("number of prepositions in the file =");
	}
}


my out put on the console is

 the total word count is 9
number of vowels in file = 0
..........
number of vowels in file = 1
number of vowels in file = 1
.....repeating till

number of vowels in file = 10
number of prepositions in the file =


and on the output file is

number of vowels in file = 0number of vowels in file = 1number of vowels in file = 1number of vowels in file = 1number of vowels..... 8number of vowels in file = 9number of vowels in file = 9number of vowels in file = 10


I just want three line right now in my output file and console that say
number of words are= "the number"
number of vowels are= "the number"
number of preposition are= "the number"

help is welcomed...thanks in advanced

Is This A Good Question/Topic? 0
  • +

Replies To: printing vowels and counting them from textfile

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 12:16 PM

I'm sorry, but where is your error occurring, whats the error thats happening when you read and write to files?
I see your writing the vowels line in a while loop, not sure if that is intended.

This post has been edited by ianian112: 25 April 2010 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 tennisguru09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 12-September 09

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 12:46 PM

View Postianian112, on 25 April 2010 - 11:16 AM, said:

I'm sorry, but where is your error occurring, whats the error thats happening when you read and write to files?
I see your writing the vowels line in a while loop, not sure if that is intended.


printing out the number of wordCount to a"output.txt" file
it's not doing any of that.

and i don't know how to count to count prepositions

This post has been edited by tennisguru09: 25 April 2010 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 01:06 PM

View Posttennisguru09, on 25 April 2010 - 11:46 AM, said:

View Postianian112, on 25 April 2010 - 11:16 AM, said:

I'm sorry, but where is your error occurring, whats the error thats happening when you read and write to files?
I see your writing the vowels line in a while loop, not sure if that is intended.


printing out the number of wordCount to a"output.txt" file
it's not doing any of that.

and i don't know how to count to count prepositions

ok here is how I fixed it
private static  FileReader inputStream = null;
 private static FileWriter outputStream = null;


then after each method, bar the final one, only flush the stream. Also only instantiate them in the first method. Also for the preps. I suggest reading from a file into an ArrayList all possible answers. Then in the method use ArrayList.contains() method to cross check the results
Was This Post Helpful? 0
  • +
  • -

#5 tennisguru09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 12-September 09

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 01:37 PM

so change it to this....it's just a snippet
 public static void countVowels() throws IOException {
        	private static FileReader inputStream = null;
        	private static FileWriter outputStream = null;
                
                String fileContents = "G:\\Java 1302\\project\\FILEIO\\src\\output.txt";
                try {
                        inputStream = new FileReader(
                                        "G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
                        outputStream = new FileWriter(
                                        "G:\\Java 1302\\project\\FILEIO\\src\\ouput.txt");

                        BufferedReader br = new BufferedReader(inputStream);
                        int numOfVowels = 0;
                        String lineRead = br.readLine();

                      



the error is that "illegal modifier for outputStream/inputStream, only final is permitted"
Was This Post Helpful? 0
  • +
  • -

#6 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 02:36 PM

View Posttennisguru09, on 25 April 2010 - 12:37 PM, said:

so change it to this....it's just a snippet
 public static void countVowels() throws IOException {
        	private static FileReader inputStream = null;
        	private static FileWriter outputStream = null;
                
                String fileContents = "G:\\Java 1302\\project\\FILEIO\\src\\output.txt";
                try {
                        inputStream = new FileReader(
                                        "G:\\Java 1302\\project\\FILEIO\\src\\input.txt");
                        outputStream = new FileWriter(
                                        "G:\\Java 1302\\project\\FILEIO\\src\\ouput.txt");

                        BufferedReader br = new BufferedReader(inputStream);
                        int numOfVowels = 0;
                        String lineRead = br.readLine();

                      



the error is that "illegal modifier for outputStream/inputStream, only final is permitted"

sorry about my code confusion, I meant to put the code below, below the public class
private static FileReader inputStream = null;
private static FileWriter outputStream = null;


like
public class xxxxxxx
{
private static FileReader inputStream = null;
private static FileWriter outputStream = null;
public static void main(String[] args){
}
}


This post has been edited by ianian112: 25 April 2010 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 tennisguru09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 12-September 09

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 03:30 PM

that didn't change anything, it still isn't printing the work count
it's printin
to console
 the total word count is 6
number of vowels in file: 10
number of vowels in file: 12


and the output file
number of vowels in file = 10number of vowels in file = 12

Was This Post Helpful? 0
  • +
  • -

#8 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 03:40 PM

View Posttennisguru09, on 25 April 2010 - 02:30 PM, said:

that didn't change anything, it still isn't printing the work count
it's printin
to console
 the total word count is 6
number of vowels in file: 10
number of vowels in file: 12


and the output file
number of vowels in file = 10number of vowels in file = 12

What program do you use to open the txt file?

I had the same problem in January whenever I used Notepad to open the text files. I used \n to create a next line but it never showed in notepad, yet when I opened the file and used .nextline methods, it worked fine. Try to open the file using a different text editor and see if you get different results.
Was This Post Helpful? 1
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 03:56 PM

public static void main(String[] args) throws Exception {
    countWords();
    countVowels();
    countPrepositions();
}

public static void countWords() throws IOException {
    FileReader inputStream = null;
    FileWriter outputStream = null;
    String fileContents = "";

    try {
        inputStream = new FileReader("input.txt");
        outputStream = new FileWriter("ouput.txt");

        BufferedReader br = new BufferedReader(inputStream);

        String lineRead;
        String wdcnt;

        while ((lineRead = br.readLine()) != null) {
            fileContents = fileContents + "\n" + lineRead;
        }

        // count words and display the count
        String[] wordList = fileContents.split("[\\s\\n]");

        // int size = wordList.length;
        wdcnt = (" the total word count is " + (wordList.length - 2));

        // display on console word count
        System.out.println(wdcnt);

        // print into new file
        outputStream.write(wdcnt + "\n");
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

public static void countVowels() throws IOException {
    FileReader inputStream = null;
    FileWriter outputStream = null;
    String fileContents = "";

    try {
        inputStream = new FileReader("input.txt");
        outputStream = new FileWriter("ouput.txt");

        BufferedReader br = new BufferedReader(inputStream);

        int numOfVowels = 0;
        String lineRead;

        while ((lineRead = br.readLine()) != null) {
            char[] ch = lineRead.toCharArray();

            for (int i = 0; i < lineRead.length(); i++) {
                char c = lineRead.charAt(i);
                if (c == 'a' || c == 'A' || c == 'e' || c == 'E'
                        || c == 'i' || c == 'I' || c == 'o' || c == 'O'
                        || c == 'u' || c == 'U') {
                    numOfVowels++;
                }
            }
        }
        // display on console word count
        System.out.println("number of vowels in file = "
                + numOfVowels);
        // print into new file
        outputStream.write("number of vowels in file = "
                + numOfVowels + "\n");
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

public static void countPrepositions() throws IOException {
    System.out.println("number of prepositions in the file =");
}



Output:

Quote

run:
the total word count is 209
number of vowels in file = 322
number of prepositions in the file =
BUILD SUCCESSFUL (total time: 0 seconds)

This post has been edited by n8wxs: 25 April 2010 - 04:01 PM

Was This Post Helpful? 1
  • +
  • -

#10 tennisguru09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 12-September 09

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 04:03 PM

View PostEmber, on 25 April 2010 - 02:40 PM, said:

What program do you use to open the txt file?

I had the same problem in January whenever I used Notepad to open the text files. I used \n to create a next line but it never showed in notepad, yet when I opened the file and used .nextline methods, it worked fine. Try to open the file using a different text editor and see if you get different results.


yes notepad...ok let me see THanks
Was This Post Helpful? 0
  • +
  • -

#11 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 04:06 PM

Wait, don't use notepad. I used notepad and that was the problem. Now I open the text file using the IDE. Both Eclipse and NetBeans open .txt files and display them correctly.
Was This Post Helpful? 1
  • +
  • -

#12 tennisguru09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 12-September 09

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 04:12 PM

Thanks so much, it run beautifully..now am going to try and open it something apart from notepad..thanks
Was This Post Helpful? 0
  • +
  • -

#13 tennisguru09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 12-September 09

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 04:28 PM

View PostEmber, on 25 April 2010 - 03:06 PM, said:

Wait, don't use notepad. I used notepad and that was the problem. Now I open the text file using the IDE. Both Eclipse and NetBeans open .txt files and display them correctly.


i tried with netbeans but still it's just showing the line with the vowels and not the word count in the output file....

View Postn8wxs, on 25 April 2010 - 02:56 PM, said:

public static void main(String[] args) throws Exception {
    countWords();
    countVowels();
    countPrepositions();
}

public static void countWords() throws IOException {
    FileReader inputStream = null;
    FileWriter outputStream = null;
    String fileContents = "";

    try {
        inputStream = new FileReader("input.txt");
        outputStream = new FileWriter("ouput.txt");

        BufferedReader br = new BufferedReader(inputStream);

        String lineRead;
        String wdcnt;

        while ((lineRead = br.readLine()) != null) {
            fileContents = fileContents + "\n" + lineRead;
        }

        // count words and display the count
        String[] wordList = fileContents.split("[\\s\\n]");

        // int size = wordList.length;
        wdcnt = (" the total word count is " + (wordList.length - 2));

        // display on console word count
        System.out.println(wdcnt);

        // print into new file
        outputStream.write(wdcnt + "\n");
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

public static void countVowels() throws IOException {
    FileReader inputStream = null;
    FileWriter outputStream = null;
    String fileContents = "";

    try {
        inputStream = new FileReader("input.txt");
        outputStream = new FileWriter("ouput.txt");

        BufferedReader br = new BufferedReader(inputStream);

        int numOfVowels = 0;
        String lineRead;

        while ((lineRead = br.readLine()) != null) {
            char[] ch = lineRead.toCharArray();

            for (int i = 0; i < lineRead.length(); i++) {
                char c = lineRead.charAt(i);
                if (c == 'a' || c == 'A' || c == 'e' || c == 'E'
                        || c == 'i' || c == 'I' || c == 'o' || c == 'O'
                        || c == 'u' || c == 'U') {
                    numOfVowels++;
                }
            }
        }
        // display on console word count
        System.out.println("number of vowels in file = "
                + numOfVowels);
        // print into new file
        outputStream.write("number of vowels in file = "
                + numOfVowels + "\n");
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

public static void countPrepositions() throws IOException {
    System.out.println("number of prepositions in the file =");
}



Output:

Quote

run:
the total word count is 209
number of vowels in file = 322
number of prepositions in the file =
BUILD SUCCESSFUL (total time: 0 seconds)


it's just showing the line with the vowels and not the word count in the output file.. i also need that sentence to show in the output file...any ideas? Thanks again
Was This Post Helpful? 0
  • +
  • -

#14 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 04:28 PM

View Posttennisguru09, on 25 April 2010 - 03:26 PM, said:

View PostEmber, on 25 April 2010 - 03:06 PM, said:

Wait, don't use notepad. I used notepad and that was the problem. Now I open the text file using the IDE. Both Eclipse and NetBeans open .txt files and display them correctly.


i tried with netbeans but still it's just showing the line with the vowels and not the word count in the output file....

can you post your updated code? After I changed your code both lines showed up in notepad
Was This Post Helpful? 0
  • +
  • -

#15 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: printing vowels and counting them from textfile

Posted 25 April 2010 - 04:36 PM

Give us the input.txt file as well. I'm motivated to get this program working now.

This post has been edited by Ember: 25 April 2010 - 04:36 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2