943,950 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1510
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 10th, 2008
0

Exception Handling

Expand 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:

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaynicb25 is offline Offline
10 posts
since Jul 2008
Aug 11th, 2008
0

Re: Exception Handling

try
{
<you code here>
}

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

catch(NumberFormatException evt2)
{

System.out.println("Ur Message");
}
Reputation Points: -1
Solved Threads: 4
Junior Poster in Training
lich is offline Offline
78 posts
since May 2008
Aug 11th, 2008
0

Re: Exception Handling

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:

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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.

Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  1. void A() throws Exception {
  2. }
  3.  
  4. void B() {
  5. try {
  6. A();
  7. } catch (Exception e) {
  8. }
  9. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Aug 11th, 2008
0

Re: Exception Handling

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 11th, 2008
0

Re: Exception Handling

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Aug 11th, 2008
0

Re: Exception Handling

And something that I forgot:
NEVER, NEVER "hard code" the length of an array when you access it :

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

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

To this:
Java Syntax (Toggle Plain Text)
  1. if (index >= 0 && index < letters.length) //Step c
  2. letterC[index]++;
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Aug 11th, 2008
0

Re: Exception Handling

Click to Expand / Collapse  Quote originally posted by shaynicb25 ...
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.:

Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Aug 11th, 2008
0

Re: Exception Handling

This assignment was given to me by my professor. I have tried implementing the changes you wrote about but now I have 35 errors
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaynicb25 is offline Offline
10 posts
since Jul 2008
Aug 11th, 2008
0

Re: Exception Handling

post you final code
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Aug 11th, 2008
0

Re: Exception Handling

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaynicb25 is offline Offline
10 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Accessing JApplet "Window Close" [x] box
Next Thread in Java Forum Timeline: Icons not showing?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC