Exception Handling

Thread Solved

Join Date: Jul 2008
Posts: 10
Reputation: shaynicb25 is an unknown quantity at this point 
Solved Threads: 0
shaynicb25 shaynicb25 is offline Offline
Newbie Poster

Exception Handling

 
0
  #1
Aug 10th, 2008
I have to create a program that reads the text, outputs the text as is and prints the number of lines and the number of times each letter appears in the text. I have to include an exception, so that if the array index goes out of bounds when the program accesses the array letterCount, it throws and handles the ArrayIndexOutOfBoundsException. I have the program completed and working , but I'm having trouble adding the exception to the program. Here is my code:

  1.  
  2. import java.io.*;
  3.  
  4. public class CharacterCount
  5. {
  6. public static void main(String[] args)
  7. throws FileNotFoundException, IOException
  8. {
  9. int lineCount = 0;
  10. try
  11. {
  12. int[] letterCount = new int[26];
  13. }
  14. catch (ArrayIndexOutOfBoundsException e)
  15. {
  16. System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage());
  17. e.printStackTrace();
  18. }
  19.  
  20. IntClass next = new IntClass();
  21.  
  22. FileReader inputStream = new FileReader("text.txt");
  23. PrintWriter outfile =
  24. new PrintWriter(new FileWriter("textCh.out"));
  25.  
  26. next.setNum(inputStream.read());
  27.  
  28. while (next.getNum() != -1)
  29. {
  30. copyText(inputStream, outfile, next, letterCount);
  31. lineCount++;
  32. next.setNum(inputStream.read());
  33. } //end while loop
  34.  
  35. writeTotal(outfile, lineCount, letterCount);
  36.  
  37. outfile.close();
  38. }
  39.  
  40. static void copyText(FileReader infile, PrintWriter outfile,
  41. IntClass next, int[] letterC) throws IOException
  42. {
  43. while (next.getNum() != (int)'\n')
  44. {
  45. outfile.print((char)(next.getNum()));
  46. chCount((char)(next.getNum()), letterC);
  47. next.setNum(infile.read());
  48. }
  49. outfile.println();
  50. }
  51.  
  52. static void chCount(char ch, int[] letterC)
  53. {
  54. int index;
  55. int i;
  56.  
  57. ch = Character.toUpperCase(ch); //Step a
  58. index = (int) ch - 65; //Step b
  59. if (index >= 0 && index < 26) //Step c
  60. letterC[index]++;
  61. }
  62.  
  63. static void writeTotal(PrintWriter outfile, int lines,
  64. int[] letters)
  65. {
  66. int i;
  67.  
  68. outfile.println();
  69. outfile.println("The number of lines = " + lines);
  70.  
  71. for (i = 0; i < 26; i++)
  72. outfile.println((char)(i + 65) + " count = "
  73. + letters[i]);
  74. }
  75. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 65
Reputation: lich has a little shameless behaviour in the past 
Solved Threads: 4
lich lich is offline Offline
Junior Poster in Training

Re: Exception Handling

 
0
  #2
Aug 11th, 2008
try
{
<you code here>
}

catch (ArrayIndexOutOfBounds evt)
{
System.out.println(" Ur Message");
}

catch(NumberFormatException evt2)
{

System.out.println("Ur Message");
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Exception Handling

 
0
  #3
Aug 11th, 2008
First of all, this:
int[] letterCount = new int[26] ;
does not throw an exception.
Second: Let me guess your program does not compile. This is why:

  1. try
  2. {
  3. int[] letterCount = new int[26];
  4. }
  5. catch (ArrayIndexOutOfBoundsException e)
  6. {
  7. System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage());
  8. e.printStackTrace();
  9. }
You declare letterCount inside the try { ... }. Meaning that outside try{...} no one can "see" it. So when you try to use it at the while loop you will get an error. If you have to give value to a variable inside the try{...} but you will have to use it outside as well then:

  1. int[] letterCount = null;
  2. try
  3. {
  4. letterCount = new int[26];
  5. }
  6. catch (ArrayIndexOutOfBoundsException e)
  7. {
  8. System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage());
  9. e.printStackTrace();
  10. }
You declare letterCount outside try{} so anyone can see it.
By the way the above is not necessary, since the declaration does not throw an ArrayIndexOutOfBoundsException, unless you use negative value. And it is not very correct to catch such exceptions. It is up to the programmer to check in the code and make sure that you will never try to access an element of an array with an index greater than the length.

  1. //try {
  2. int[] letterCount = new int[26];
  3. /*
  4. } catch (ArrayIndexOutOfBoundsException e) {
  5. System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage());
  6. e.printStackTrace();
  7. }
  8. */
Try it without the try-catch.

As for you question, remove the throws Exception, from the main. You use these at method declarations in order to be caught by the method that calls the first method. No one calls main. Example:
  1. void A() throws Exception {
  2. }
  3.  
  4. void B() {
  5. try {
  6. A();
  7. } catch (Exception e) {
  8. }
  9. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Exception Handling

 
0
  #4
Aug 11th, 2008
Yes, including a throws on the end of main is considered bad programming practice since nothing can really catch an error generated from main and handle it afterward.

Only use throws on the end of main to save time when practicing something particular without bloating the code in try-catch blocks.

Use File I/O or Networking extensively for an example.
Last edited by Alex Edwards; Aug 11th, 2008 at 4:20 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Exception Handling

 
0
  #5
Aug 11th, 2008
  1. IntClass next = new IntClass();
  2.  
  3. FileReader inputStream = new FileReader("text.txt");
  4. PrintWriter outfile =
  5. new PrintWriter(new FileWriter("textCh.out"));
  6.  
  7. next.setNum(inputStream.read());
  8.  
  9. while (next.getNum() != -1)
  10. {
  11. copyText(inputStream, outfile, next, letterCount);
  12. lineCount++;
  13. next.setNum(inputStream.read());
  14. } //end while loop
  15.  
  16. writeTotal(outfile, lineCount, letterCount);
  17.  
  18. outfile.close();

The above code needs to be altered like this:

  1. IntClass next = new IntClass();
  2. FileReader inputStream = null;
  3. PrintWriter outfile = null;
  4.  
  5. try {
  6. inputStream = new FileReader("text.txt");
  7. outfile = new PrintWriter(new FileWriter("textCh.out"));
  8.  
  9. next.setNum(inputStream.read());
  10.  
  11. while (next.getNum() != -1)
  12. {
  13. copyText(inputStream, outfile, next, letterCount);
  14. lineCount++;
  15. next.setNum(inputStream.read());
  16. } //end while loop
  17.  
  18. writeTotal(outfile, lineCount, letterCount);
  19.  
  20. outfile.close();
  21. } catch (Exception e) {
  22. System.out.println(e.getMessage());
  23. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Exception Handling

 
0
  #6
Aug 11th, 2008
And something that I forgot:
NEVER, NEVER "hard code" the length of an array when you access it :

This:
  1. for (i = 0; i < 26; i++)
  2. outfile.println((char)(i + 65) + " count = "
  3. + letters[i]);
Should become this:
  1. for (i = 0; i < letters.length ; i++)
  2. outfile.println((char)(i + 65) + " count = "
  3. + letters[i]);

And this:
  1. if (index >= 0 && index < 26) //Step c
  2. letterC[index]++;

To this:
  1. if (index >= 0 && index < letters.length) //Step c
  2. letterC[index]++;
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Exception Handling

 
0
  #7
Aug 11th, 2008
Originally Posted by shaynicb25 View Post
I have to create a program that reads the text, outputs the text as is and prints the number of lines and the number of times each letter appears in the text. I have to include an exception, so that if the array index goes out of bounds when the program accesses the array letterCount, it throws and handles the ArrayIndexOutOfBoundsException. I have the program completed and working , but I'm having trouble adding the exception to the program. Here is my code:
Hi it's me again. Sorry for not checking what you wrote, I was only looking at the code you gave. It seems that I suggested not to use ArrayIndexOutOfBoundsException even though that is what you wanted.
Who told you to use this exception handling? Because what I suggested was generally correct: you don't want to catch such exception, but instead make sure it doesn't happen by checking the length of the array.
Anyway if you want ArrayIndexOutOfBoundsException here it is.:

  1. static void chCount(char ch, int[] letterC) throws ArrayIndexOutOfBoundsException {
  2. int index;
  3. int i;
  4.  
  5. ch = Character.toUpperCase(ch); //Step a
  6. index = (int) ch - 65; //Step b
  7. if (index >= 0 && index < 26) //Step c
  8. letterC[index]++;
  9. }
  10.  
  11. static void writeTotal(PrintWriter outfile, int lines,
  12. int[] letters)
  13. throws ArrayIndexOutOfBoundsException {
  14. int i;
  15.  
  16. outfile.println();
  17. outfile.println("The number of lines = " + lines);
  18.  
  19. for (i = 0; i < 26; i++)
  20. outfile.println((char)(i + 65) + " count = "
  21. + letters[i]);
  22. }
  23.  
  24. static void copyText(FileReader infile, PrintWriter outfile,
  25. IntClass next, int[] letterC) throws IOException, ArrayIndexOutOfBoundsException
  26. {
  27. while (next.getNum() != (int)'\n')
  28. {
  29. outfile.print((char)(next.getNum()));
  30. chCount((char)(next.getNum()), letterC);
  31. next.setNum(infile.read());
  32. }
  33. outfile.println();
  34. }
When you add throws to a method (methodA) declaration, if an exception of that kind occurs in the method then it will not give you trouble inside the method. You will not have to put try-catch inside the method. But instead you throw it to the method that called methodA.
The above method copyText has a throws because it calls chCount which throws an ArrayIndexOutOfBoundsException .

It depends where you want to handle it:

1st version:
  1. void A(String s) {
  2. try {
  3. int i = Integer.parseInt(s);
  4. // do other stuff
  5. // perhaps return something if the method was not void
  6. } catch(NumberFormatException nfe) {
  7. //handle it here
  8. //print a message
  9. }
  10. }
  11.  
  12. void B() {
  13. A("3");
  14. A("a");
  15. }

Or Version 2:
  1. void A(String s) throws NumberFormatException {
  2. int i = Integer.parseInt(s);
  3. }
  4.  
  5. void B() {
  6. try {
  7. A("3");
  8. } catch (NumberFormatException nfe) {
  9. //handle it here
  10. }
  11. try {
  12. A("a");
  13. } catch (NumberFormatException nfe) {
  14. //handle it here
  15. }
  16.  
  17.  
  18. }

So now that you have added the throws ArrayIndexOutOfBoundsException, go to where you call the methods and put them in a try-catch. Since in my suggestions I have already put your code in try-catch, simply add one more catch() { }

And please tell me who told you to use ArrayIndexOutOfBoundsException? And sorry for confusing you and for my many posts
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 10
Reputation: shaynicb25 is an unknown quantity at this point 
Solved Threads: 0
shaynicb25 shaynicb25 is offline Offline
Newbie Poster

Re: Exception Handling

 
0
  #8
Aug 11th, 2008
This assignment was given to me by my professor. I have tried implementing the changes you wrote about but now I have 35 errors
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Exception Handling

 
0
  #9
Aug 11th, 2008
post you final code
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 10
Reputation: shaynicb25 is an unknown quantity at this point 
Solved Threads: 0
shaynicb25 shaynicb25 is offline Offline
Newbie Poster

Re: Exception Handling

 
0
  #10
Aug 11th, 2008
  1.  
  2. import java.io.*;
  3.  
  4. public class CharacterCount
  5. {
  6. public static void main(String[] args)
  7. throws FileNotFoundException, IOException
  8. {
  9. int lineCount = 0;
  10. int[] letterCount = new int[26];
  11.  
  12. IntClass next = new IntClass();
  13. FileReader inputStream = null;
  14. PrintWriter outfile = null;
  15.  
  16. try
  17. {
  18.  
  19. FileReader inputStream = new FileReader("text.txt");
  20. PrintWriter outfile =
  21. new PrintWriter(new FileWriter("textCh.out"));
  22.  
  23. next.setNum(inputStream.read());
  24.  
  25. while (next.getNum() != -1)
  26. {
  27. copyText(inputStream, outfile, next, letterCount);
  28. lineCount++;
  29. next.setNum(inputStream.read());
  30. } //end while loop
  31.  
  32. writeTotal(outfile, lineCount, letterCount);
  33.  
  34. outfile.close();
  35. }
  36. catch (Exception e)
  37. {
  38. System.out.println(e.getMessage());
  39. }
  40.  
  41.  
  42. static void copyText(FileReader infile, PrintWriter outfile,
  43. IntClass next, int[] letterC) throws IOException, ArrayIndexOutOfBoundsException
  44. {
  45. while (next.getNum() != (int)'\n')
  46. {
  47. outfile.print((char)(next.getNum()));
  48. chCount((char)(next.getNum()), letterC);
  49. next.setNum(infile.read());
  50. }
  51. outfile.println();
  52. }
  53.  
  54. static void chCount(char ch, int[] letterC)throws ArrayIndexOutOfBoundsException
  55.  
  56. {
  57. int index;
  58. int i;
  59.  
  60. ch = Character.toUpperCase(ch); //Step a
  61. index = (int) ch - 65; //Step b
  62. if (index >= 0 && index < 26) //Step c
  63. letterC[index]++;
  64. }
  65.  
  66. static void writeTotal(PrintWriter outfile, int lines,
  67. int[] letters) throws ArrayIndexOutOfBoundsException
  68. {
  69. int i;
  70.  
  71. outfile.println();
  72. outfile.println("The number of lines = " + lines);
  73.  
  74. for (i = 0; i < 26; i++)
  75. outfile.println((char)(i + 65) + " count = "
  76. + letters[i]);
  77. }
  78. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC