Skip to content

Commit 13a4d43

Browse files
committed
GUI update+Exception Handling
1 parent 82f2bc9 commit 13a4d43

File tree

3 files changed

+70
-38
lines changed

3 files changed

+70
-38
lines changed

src/com/company/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
public class Main {
44
static int[][] sudoku_array = new int[9][9];
55
public static void main(String[] args) {
6-
guiController gui = new guiController();
7-
gui.setVisible(true);
6+
guiController gui = new guiController();//Initializes program
7+
gui.setVisible(true);//sets the GUI as visible
88
}
99
}
1010
class solver{

src/com/company/guiController.java

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@
77
import java.io.IOException;
88

99
class guiController extends JFrame {
10-
11-
private JPanel grid = new JPanel(new GridLayout(9, 9));
10+
/*
11+
This class handle GUI related functions and also allows GUI refresh whenever any move is taken by AI
12+
*/
13+
private JPanel grid = new JPanel(new GridLayout(9, 9)); //Grid layout
1214
private JPanel buttons = new JPanel(new GridLayout(2, 1));
13-
private JTextField[][] fieldArray = new JTextField[9][9];
15+
private JTextField[][] fieldArray = new JTextField[9][9]; //array of fields for easy extraction
1416

15-
private sudokuInitializer sud = new sudokuInitializer();
17+
private sudokuInitializer sud = new sudokuInitializer(); //initializes array from file
1618
private int flag;
1719

1820
guiController() {
19-
flag = chooseInputStream();
20-
guiInit();
21+
flag = chooseInputStream(); //choose input method
22+
guiInit();//Initializes GUI
2123
//sud.printArray();
2224
}
23-
25+
/*
26+
Function to display GUI for the user to choose input method method
27+
File or Manual
28+
*/
2429
private int chooseInputStream() {
2530
String[] options = new String[]{"File", "Manual"};
2631
int response = JOptionPane.showOptionDialog(null, "Choose one way", "",
@@ -30,13 +35,15 @@ private int chooseInputStream() {
3035
if (response == 0) {
3136
try {
3237
sud.fileInput();
33-
} catch (IOException e) {
34-
e.printStackTrace();
38+
} catch (Exception e) {
39+
JOptionPane.showMessageDialog(null,"Critical Error");
3540
}
3641
}
3742
return response;
3843
}
39-
44+
/*
45+
Initializes GUI with the given content from the array
46+
*/
4047
private void guiInit() {
4148
for (int i = 0; i < 9; i++) {
4249
for (int j = 0; j < 9; j++) {
@@ -65,39 +72,48 @@ private void guiInit() {
6572

6673
}
6774
add(grid);
68-
JButton solve = new JButton("Solve");
75+
JButton solve = new JButton("Solve");//Button to compute
6976
solve.setBorder(null);
7077
solve.setBackground(Color.white);
7178
buttons.add(solve);
7279
buttons.add(new JLabel());
7380
buttons.setBackground(Color.white);
7481
add(buttons, BorderLayout.SOUTH);
75-
solve.addActionListener(e -> {
76-
if (flag != 0)
77-
getText();
78-
sud.printArray();
79-
solve.setEnabled(false);
82+
solve.addActionListener(e -> { //If the button is pressed
83+
if (flag != 0) {
84+
try {
85+
getText(); //takes in text from the GUI
86+
sud.printArray(); //debugging purpose
87+
solve.setEnabled(false);//cell value becomes fixed
88+
} catch (Exception ex) {
89+
JOptionPane.showMessageDialog(null,"Invalid Input");//Throws error if the input is invalid
90+
}
91+
}
8092
});
81-
93+
//Basic Frame parameters are set below this
8294
setSize(400, 500);
83-
//setVisible(true);
95+
//setVisible(true);//done in Main function
8496
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
8597
setLocationRelativeTo(null);
8698
}
87-
88-
private void getText() {
99+
/*
100+
takes in valid input and sets the GUI cells as non editable
101+
*/
102+
private void getText() throws Exception{
89103
String temp;
90104
for (int i = 0; i < 9; i++) {
91105
for (int j = 0; j < 9; j++) {
92106
temp = fieldArray[i][j].getText();
93107
if (!temp.equals("")) {
94-
Main.sudoku_array[i][j] = Integer.parseInt(temp);
95-
fieldArray[i][j].setEditable(false);
108+
Main.sudoku_array[i][j] = sud.checkValidInput(temp); //throws exception
109+
fieldArray[i][j].setEditable(false);
96110
}
97111
}
98112
}
99113
}
100-
114+
/*
115+
Refreshes the GUI whenever AI makes some changes
116+
*/
101117
void refreshGUI(){
102118
for (int i = 0; i < 9; i++) {
103119
for (int j = 0; j < 9; j++) {

src/com/company/sudokuInitializer.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package com.company;
22

33
import javax.swing.*;
4-
import java.io.BufferedReader;
5-
import java.io.File;
6-
import java.io.FileReader;
7-
import java.io.IOException;
4+
import java.io.*;
85

96
class sudokuInitializer {
7+
/*
8+
Function to check whether the input given via file or manually is valid or not
9+
Thorws exception for Invalid Input
10+
*/
11+
int checkValidInput(String input) throws Exception{
12+
int n = Integer.parseInt(input+"");
13+
if (n<10 && n>-1)
14+
return n;
15+
else throw new Exception();
16+
}
17+
/*
18+
Function to print content of the array
19+
Used for debugging situations
20+
*/
1021
void printArray(){
1122
System.out.println("Printing...");
1223
for (int i=0;i<9;i++) {
@@ -15,33 +26,38 @@ void printArray(){
1526
System.out.print(Main.sudoku_array[i][j] + " ");
1627
}
1728
}
29+
/*
30+
Takes input from file using Jfilechooser, and the input from the file is inserted in the array
31+
While inserting, it checks for valid input as well
32+
*/
1833
void fileInput() throws IOException {
19-
System.out.println("opening Jfilechooser");
20-
//JFrame f = new JFrame("Choose a file");
2134
JFileChooser fileChooser = new JFileChooser("D:\\Windows\\Documents");
2235
int result = fileChooser.showOpenDialog(null);
2336
if (result == JFileChooser.APPROVE_OPTION) {
2437
File selectedFile = fileChooser.getSelectedFile();
25-
BufferedReader br = new BufferedReader(new FileReader(selectedFile));
38+
BufferedReader br = new BufferedReader(new FileReader(selectedFile));//reads the file
2639

27-
StringBuilder st = new StringBuilder();
40+
StringBuilder st = new StringBuilder(); // To store Extracted string
2841
String stt;
2942
int i=0;
30-
while (((stt = br.readLine())) != null){
43+
while (((stt = br.readLine())) != null){ //while file has content
3144
st.insert(0,stt);
3245
for (int j=0;j<st.length();j++){
33-
Main.sudoku_array[i][j] = Integer.parseInt(st.charAt(j)+"");
34-
//System.out.print(i+","+j+":"+Main.sudoku_array[i][j]+" ");
46+
try {
47+
Main.sudoku_array[i][j] = checkValidInput(st.charAt(j)+""); //checks valid input, catches exception
48+
}catch (Exception E){
49+
JOptionPane.showMessageDialog(null,"Error in extracting data from file\nExiting...");
50+
System.exit(1);
51+
}
3552
}
3653
i++;
37-
//System.out.println();
3854
st.delete(0,st.length());
3955
}
4056
}
4157
}
4258
/*
4359
44-
//NOTE : Class only to be used in catastrophic situations only
60+
//NOTE : Method only to be used in catastrophic situations only, used to take in console input
4561
void manualInput(){
4662
String[] str = new String[9];
4763
Scanner input = new Scanner(System.in);

0 commit comments

Comments
 (0)