9 Replies - 7991 Views - Last Post: 30 January 2006 - 09:38 PM Rate Topic: -----

#1 donnyj47  Icon User is offline

  • New D.I.C Head

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

Constructor throwing exception and opening a file

Post icon  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
{
	//creates BufferedReader to read/open the input file
	private BufferedReader br=null;
	
	//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
  {
 	 br=new BufferedReader(new FileReader(afile));
  }
  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  Icon User is offline

  • New D.I.C Head

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

Re: Constructor throwing exception and opening a file

Post icon  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
{
	//creates BufferedReader to read/open the input file
	private BufferedReader br=null;
	
	//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
 	 {
    br=new BufferedReader(new FileReader(afile));
 	 }
 	 
 	 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");
  }
 	 
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 Voodoo Doll  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 donnyj47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
{
	//creates BufferedReader to read/open the input file
	private BufferedReader br=null;
	
	//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
	{
                br=new BufferedReader(new FileReader(afile));
	}
	
	public void countVowels() throws IOException
	{
  //read a line of string from the input file 
  String line=br.readLine();
  
  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++;
    }
    
    line=br.readLine();
 	 }

	}
	
	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
Was This Post Helpful? 0
  • +
  • -

#5 Voodoo Doll  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • 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. :P

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

Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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++;
    }
   
   line=br.readLine();
  }


Was This Post Helpful? 0
  • +
  • -

#7 donnyj47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 Voodoo Doll  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

#10 donnyj47  Icon User is offline

  • New D.I.C Head

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

Re: Constructor throwing exception and opening a file

Posted 30 January 2006 - 09:38 PM

OOOOOOOOO, ok :D i got u now thx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1