What is the difference between throwing an exception and catching an exception?
ID: 3554671 • Letter: W
Question
- What is the difference between throwing an exception and catching an exception? Provide an answer in two to three sentences.
- Implement the method getSequence that takes a Scanner as a parameter. It should read Strings one line at a time from the Scanner and store them in an ArrayList<String> until it reaches a line with a single '.' character on it. It should then return the array list to the calling program.
public static ArrayList<String> getSequence(Scanner in)
- Now implement the method summarizeSequences that takes a String as a parameter. This method should open the file named by this String, and read a sequence from that file into an array list using the getSequence method you wrote above. It should then display on a single line the first, last, and middle String in the sequence (if the list is even, the position of the "middle" String is the size of the list divided by 2). Make sure you catch and handle the possible IOException exceptions.
public static void summarizeSequences(String fileName)
- Now implement the method writeSequenceReverse that takes a PrintWriter and an ArrayList<String> as parameters. This method shouldwrite the Strings in the ArrayList to the PrintWriter in reverse order and then write a line with a single '.' on it.
public static void writeSequenceReverse(PrintWriter out, ArrayList<String> list)
- Now implement a method reverseSequences that takes two Strings as parameters. The first String is the name of an input file containing String values and the second is the name of an output file to write the reverse sequences to. The method should read sequences from the input file one sequence at a time using getSequence and then write them out to the output file. Do not forget to close the output file when the method is finished. Make sure you catch and handle the possible IOException exceptions.
public static void reverseSequences(String inputFile, String outputFile)
Explanation / Answer
1. Difference between throwing an exception and catching an exception:
Throwing an exception means notifying the program that an unexpected condition was occured.
Catch exception means handle that unexpected condition and allowing program to excecute normally
2. public static ArrayList<String> getSequence(Scanner in) :
public static ArrayList<String> getSequence(Scanner in)
{
String s;
ArrayList<String> list=new ArrayList<String>();
while(in.hasNext())
{
if((s=in.nextLine()).equalsIgnoreCase("."))
break;
list.add(s);
}
return list;
}
3. public static void summarizeSequences(String fileName) :
public static void summarizeSequences(String fileName)
{
ArrayList<String> list=new ArrayList<String>();
File file = new File(fileName);
Scanner in=null;
try {
in = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list=getSequence(in);
int len=list.size();
System.out.println("First Line:"+list.get(0));
System.out.println("Middle Line:"+list.get(len/2));
System.out.println("Last Line:"+list.get(len));
}
4. public static void writeSequenceReverse(PrintWriter out, ArrayList<String> list) :
public static void writeSequenceReverse(PrintWriter out, ArrayList<String> list)
{
for(int i=list.size();i>0;i++)
{
out.println(list.get(i-1));
}
out.println(".");
}
5. public static void reverseSequences(String inputFile, String outputFile) :
public static void reverseSequences(String inputFile, String outputFile)
{
ArrayList<String> list=new ArrayList<String>();
File file = new File(inputFile);
Scanner in=null;
BufferedWriter writer=null;
try {
in = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list=getSequence(in);
try {
writer = new BufferedWriter(new FileWriter(outputFile));
for(int i=0;i>list.size();i++)
{
writer.write(list.get(i));
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.