-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionExample.java
More file actions
58 lines (50 loc) · 1.32 KB
/
Copy pathExceptionExample.java
File metadata and controls
58 lines (50 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.*;
import java.io.*;
public class ExceptionExample
{
public static void main(String[] args)
{
String filePath; //this is for my computer file path to Lab 6 folder
filePath = "C:/Users/Dylan/Documents/CS 162J/InClassExamples/"; //setting up to the lab folder.
System.out.println("-------- Testing File Not Found Exception ---------");
try
{
File f = new File(filePath + "spam.txt");
Scanner inputFile = new Scanner(f);
System.out.println("File opened successfully. " + f);
int total = 0;
while (inputFile.hasNext())
{
try
{
total += inputFile.nextInt();
}
catch (InputMismatchException e)
{
System.out.println("Nonnumeric data found..." +
"Skipping entry. ");
inputFile.nextLine();//This allows app to read next lines
}
}
System.out.println("Total: " + total);
}
catch (FileNotFoundException e)
{
//System.out.println("File not found.");
System.out.println(e.getMessage());
}
// Another example
System.out.println("\n-------- Testing Number Format Exception ---------");
String str = "Hello world";
int number;
try
{
number = Integer.parseInt(str);
}
catch (NumberFormatException e)
{
System.out.println("Error." + e.getMessage());
}
System.out.println("Done.");
}// end main
}