Skip to content

Commit 1e2f010

Browse files
kufo2018kufo2018
kufo2018
authored and
kufo2018
committed
Bug fixes and performance improvements
1 parent a8535d5 commit 1e2f010

File tree

7 files changed

+63
-28
lines changed

7 files changed

+63
-28
lines changed
1.45 KB
Binary file not shown.
Binary file not shown.
-439 Bytes
Binary file not shown.

output.txt

-8
This file was deleted.

src/project1/Main.java

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
11
package project1;
22

3+
import java.util.InputMismatchException;
4+
import java.util.Scanner;
5+
6+
/**
7+
* Driver class for Project1
8+
*/
39
public class Main {
410
public static void main(String[] args) {
11+
runProgram(args);
12+
}
13+
14+
private static void runProgram(String[] args) {
15+
Project1 project1;
16+
int response;
17+
18+
System.out.println("Enter 1 to use command line arguments or 2 to manually input file names:");
19+
Scanner scanner = new Scanner(System.in);
20+
try {
21+
response = scanner.nextInt();
22+
} catch (InputMismatchException exception) {
23+
response = 3;
24+
}
25+
26+
if (response == 1) {
27+
project1 = new Project1(args);
28+
} else if (response == 2) {
29+
System.out.print(
30+
"Enter input file only and program will generate an output.txt file for you OR ");
31+
System.out.print(
32+
"Enter input and output filenames separated by a comma e.g. " +
33+
"MyInputFile.txt,MyOutputFile.txt: ");
34+
35+
var userInput = scanner.next();
36+
if (userInput.isEmpty()) {
37+
System.out.println("Input file cannot be empty, please retry");
38+
return;
39+
} else {
40+
var fileNames = userInput.split(",");
41+
if (fileNames.length == 1) {
42+
project1 = new Project1(fileNames[0], "output.txt");
43+
} else {
44+
project1 = new Project1(fileNames[0], fileNames[1]);
45+
}
46+
}
47+
} else {
48+
System.out.println("Invalid response");
49+
return;
50+
}
551

6-
Project1 project1 = new Project1("testFile.txt", "output.txt");
7-
// Project1 project1 = new Project1(args);
852
project1.initializeProgram();
953
project1.runSearchAlgorithms();
54+
scanner.close();
1055
}
1156
}

src/project1/Project1.java

+14-17
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@ public class Project1 {
1717
private String inputFile = "";
1818
private String outputFile = "";
1919
private PrintWriter printWriter;
20-
Timer timer = new Timer();
20+
private Timer timer = new Timer();
2121

2222
/**
2323
* Creates an object of this class using command line arguments
2424
*
2525
* @param args the String array containing the input and output file names
2626
*/
2727
public Project1(String[] args) {
28-
try {
28+
29+
if (args.length == 0) {
30+
print("No command line arguments found");
31+
} else if (args.length == 1) {
32+
this.inputFile = args[0];
33+
this.outputFile = "output.txt";
34+
} else if (args.length == 2) {
2935
this.inputFile = args[0];
3036
this.outputFile = args[1];
31-
} catch (Exception exception) {
32-
print("Error accessing command line argument(s)");
3337
}
3438
}
3539

@@ -40,12 +44,8 @@ public Project1(String[] args) {
4044
* @param outputFile the file to be written to
4145
*/
4246
public Project1(String inputFile, String outputFile) {
43-
try {
44-
this.inputFile = inputFile;
45-
this.outputFile = outputFile;
46-
} catch (Exception exception) {
47-
print("Error with input or output string");
48-
}
47+
this.inputFile = inputFile;
48+
this.outputFile = outputFile;
4949
}
5050

5151
/**
@@ -66,12 +66,10 @@ public void initializeProgram() {
6666

6767
initializePrintWriter();
6868

69-
} catch (FileNotFoundException fileNotFoundException) {
70-
print("Error! Input or output file does not exist");
71-
} catch (IOException ioException) {
72-
print("Error! IO exception occurred while reading file");
73-
} catch (NumberFormatException numberFormatException) {
74-
print("Error! Input file contains invalid data");
69+
} catch (Exception exception) {
70+
print("Error!");
71+
print("Please ensure input file exists and is formatted correctly");
72+
System.exit(1);
7573
}
7674
}
7775

@@ -156,7 +154,6 @@ private void print(Object object) {
156154
*/
157155
private void initializePrintWriter() {
158156
try {
159-
if (outputFile.isEmpty()) outputFile = "output.txt";
160157
FileWriter fileWriter = new FileWriter(outputFile);
161158
printWriter = new PrintWriter(fileWriter);
162159
} catch (IOException ioException) {

testFile.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
9
2323
110
2424
13
25-
a
25+
123
26+
2627

0 commit comments

Comments
 (0)