# Constructor throwing exception and opening a file

Page 1 of 1

## 9 Replies - 8269 Views - Last Post: 30 January 2006 - 09:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=14472&amp;s=4d7a35f09fa1122b51b30eb711397e7b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 donnyj47

Reputation: 0
• Posts: 30
• Joined: 27-January 06

# Constructor throwing exception and opening a file

Posted 30 January 2006 - 07:19 PM

here is what i need to do:
A parameterized constructor that accepts a filename as a String argument. Your constructor will open the file. If there is a problem opening the file, the constructor will throw the appropriate exception back to the calling routine.

Here is what i have so far, and i am wondering if this is the correct way to go about it?
public class WordCount
{

//print writer to write to a file
private PrintWriter pw=null;

public WordCount(String afile) throws Exception
{
//if the file the user puts in does not exist, then throw an exception
try
{
}
catch(FileNotFoundException fnfe)
{
System.out.println(fnfe.getMessage());
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}

}
}



Is This A Good Question/Topic? 0

## Replies To: Constructor throwing exception and opening a file

### #2 donnyj47

Reputation: 0
• Posts: 30
• Joined: 27-January 06

## Re: Constructor throwing exception and opening a file

Posted 30 January 2006 - 07:29 PM

OK, round II.

I modified the code some, and it compiles, I just need ot know if this is a correct way of doin the above stated.
public class WordCount
{

//print writer to write to a file
private PrintWriter pw=null;

public WordCount(String afile) throws Exception
{
File inFile=new File(afile);
//if the file the user puts in does not exist, then throw an exception
if(inFile.exists())
{
try
{
}

catch(FileNotFoundException fnfe)
{
System.out.println(fnfe.getMessage());
}

catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}

catch(Exception e)
{
System.out.println(e.getMessage());
}
}

else
{
throw new Exception("The input file does not exist");
}

}
}



### #3 Voodoo Doll

Reputation: 12
• Posts: 108
• Joined: 24-January 06

## Re: Constructor throwing exception and opening a file

Posted 30 January 2006 - 07:48 PM

Quote

it compiles, I just need ot know if this is a correct way of doin the above stated.

Relax, you'll stop having fun if you're too paranoid about being correct when your code is just fine as it is.

This post has been edited by Voodoo Doll: 30 January 2006 - 07:49 PM

### #4 donnyj47

Reputation: 0
• Posts: 30
• Joined: 27-January 06

## Re: Constructor throwing exception and opening a file

Posted 30 January 2006 - 08:44 PM

Everything i think is workin well, howver my final product does not look like it should. My final numbers are way off. I think it may have something to do with the counting loops in method countVowels.

Functional class:
public class WordCount
{

//print writer to write to a file
private PrintWriter pw=null;

//counts for each vowels
private int countA=0;
private int countE=0;
private int countI=0;
private int countO=0;
private int countU=0;

public WordCount(String afile) throws Exception
{
}

public void countVowels() throws IOException
{
//read a line of string from the input file

while(line != null)
{
//seperate delimiters for each letter
String []letterA=line.split("[^aA]");
String []letterE=line.split("[^eE]");
String []letterI=line.split("[^iI]");
String []letterO=line.split("[^oO]");
String []letterU=line.split("[^uU]");

//go through each array while incremeting the number of times each vowel is seen
for(String a: letterA)
{
countA++;
}

for(String e: letterE)
{
countE++;
}

for(String i: letterI)
{
countI++;
}

for(String o: letterO)
{
countO++;
}

for(String u: letterU)
{
countU++;
}

}

}

public String toString()
{
return "Torry Butler\n"+"4002-218-02\n"+"\na:"+countA+"\ne:"+countE+"\ni:"+
countI+"\no:"+countO+"\nu:"+countU+"\n";
}

public void close() throws IOException
{
if(br !=null)
{
br.close();
}
}

}



TestClass:
import java.io.*;

public class TestWordCount
{
public static void main(String args[])
{
//create the WordCount object
WordCount totc=null;

try
{
totc = new WordCount(args[0]);
}

catch(Exception e)
{
System.out.println(e.getMessage());
}

try
{
totc.countVowels();
totc.close();
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
finally
{
System.out.println(totc);
}
}
}



The number should be:
a:57
e:110
i:59
o:58
u:8

### #5 Voodoo Doll

Reputation: 12
• Posts: 108
• Joined: 24-January 06

## Re: Constructor throwing exception and opening a file

Posted 30 January 2006 - 09:12 PM

Hmm, that smells a bit funny. Splitting with a regex will give you a bunch of empty strings, so using the number of strings in the array will give you several false positives. Capitalizing on the zero length of empty strings, you can do something like this instead to count the number of actual characters without getting false hits on the empty ones.
for(String a: letterA)
{
countA += a.length();
}

for(String e: letterE)
{
countE += e.length();
}

for(String i: letterI)
{
countI += i.length();
}

for(String o: letterO)
{
countO += o.length();
}

for(String u: letterU)
{
countU += u.length();
}



But then people will ask you why you didn't just traverse the string and count matching vowels.

This post has been edited by Voodoo Doll: 30 January 2006 - 09:13 PM

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: Constructor throwing exception and opening a file

Posted 30 January 2006 - 09:18 PM

LOL, I was going to ask that myself!

Something along the lines of
 while(line != null)
{
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)=='a')
countA++;
if(line.charAt(i)=='e')
countE++;
if(line.charAt(i)=='i')
countI++;
if(line.charAt(i)=='o')
countO++;
if(line.charAt(i)=='u')
countU++;
}

}



### #7 donnyj47

Reputation: 0
• Posts: 30
• Joined: 27-January 06

## Re: Constructor throwing exception and opening a file

Posted 30 January 2006 - 09:19 PM

Thank you for your help, howver for fututre occurneces for this problem, i kinda know what's goin on and why you put .length(), however a little short info on it would be cool.

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: Constructor throwing exception and opening a file

Posted 30 January 2006 - 09:31 PM

Assuming you are referring to Voodoo's use of the .length method, she correctly pointed out that your method of splitting using regex will give you a lot of false positives...she has advised adding the lengths of the resulting strings, so that false positive (with a length of 0) will not adversely affect your totals.

### #9 Voodoo Doll

Reputation: 12
• Posts: 108
• Joined: 24-January 06

## Re: Constructor throwing exception and opening a file

Posted 30 January 2006 - 09:38 PM

Okie dokie. A little example is in order.
public class Example {
public static void main(String[] args)
{
String base = "agooga";
String[] split_base = base.split("[^a]");

for (String it: split_base)
System.out.println("<" + it + ">");
}
}



The trick is more or less intuitive when you think really hard. The string is searched using the regex. Any adjacent matches count as a single match. Any matches are replaced with empty strings. Matches at the end of the string are just thrown away rather than replaced with an empty string. Since the regex matches anything that isn't 'a', and the two o's are adjacent, there should be three empty strings between two strings with an 'a'.
<a>
<>
<>
<>
<a>



Now, if you change the regex to match on anything except 'o', the two separate matches before the 'oo' will be replaced with empty strings. The last match will just be tossed, so you can count on having three strings in the array, two of them empty. Since the two o's are adjacent, they count as a single non-match and are in one of the strings together instead of two separate strings.
<>
<>
<oo>



It's easy to see now how counting the length of the array would give you too many false positives, and how if you ignore the empty strings and just sum the length of the non-empty strings, you'll get the right numbers...I hope.

### #10 donnyj47

Reputation: 0
• Posts: 30
• Joined: 27-January 06

## Re: Constructor throwing exception and opening a file

Posted 30 January 2006 - 09:38 PM

OOOOOOOOO, ok i got u now thx