I am trying to make a class which will create a simple window and gives data to variables in 'Main' class.
public class Input extends JFrame{
int catch_catcher=1;
private JTextField i1;
private JTextField i2;
private JTextField i3;
private JTextField i4;
private JButton ok;
private GridLayout layout;
public Input(){
super("Input Window");
setLayout(new FlowLayout());
i1 =new JTextField("Enter the number of row 1st Mat.",20);
i2 =new JTextField("Enter the number of column 1st Mat.",20);
i3 =new JTextField("Enter the number of row 2nd Mat.",20);
i4 =new JTextField("Enter the number of column 2nd Mat.",20);
ok = new JButton("OKay");
add(i1);
add(i2);
add(i3);
add(i4);
add(ok);
ok.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(i1.equals("Enter the number of row 1st Mat.") || i2.equals("Enter the number of column 1st Mat.") || i3.equals("Enter the number of row 2nd Mat.") || i4.equals("Enter the number of column 2nd Mat.") ){
JOptionPane.showMessageDialog(null,"Warning","Please prvude required data",JOptionPane.PLAIN_MESSAGE);
}
try{
int chk = Integer.parseInt(i1.getText());
int chk2 = Integer.parseInt(i2.getText());
int chk3 = Integer.parseInt(i3.getText());
int chk4 = Integer.parseInt(i4.getText());
System.out.println("Sucessful");
}catch(Exception execpt){
JOptionPane.showMessageDialog(null, "Warning", "Please Enter valid String", JOptionPane.PLAIN_MESSAGE);
catch_catcher=0;
}
if(catch_catcher==1){
Main Obj=new Main();
Obj.getVals();
Obj.calculate();
}
}
}
);
}
int get1(){
return Integer.parseInt(i1.getText()); //this is the line
}
int get2(){
return Integer.parseInt(i2.getText());
}
int get3(){
return Integer.parseInt(i3.getText());
}
int get4(){
return Integer.parseInt(i4.getText());
}
}
At the try-catch block in actionPerformed you can see I have initialized 4 variables to ensure that if the data entered in JTextField is String it will invoke catch and the following if (just after try - catch) statement will not run. And I have also a System.out.println() to see if try block has executed completely. And it prints "Successful" indicating that I can use those code in methods below.(Or am I wrong? Does try block execute completely in spite of error?). But the problem is there's an error in method get1(); . This method is invoked as:
public void getVals(){
Input in = new Input();
d1 = in.get1();
d2 = in.get2();
d3 = in.get3();
d4 = in.get4();
}
the error Says:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException:
For input string: "Enter the number of row 1st Mat."
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at main.Input.get1(Input.java:70)
at main.Main.getVals(Main.java:19)
at main.Input$1.actionPerformed(Input.java:56)
The window pops up. I entered some numeric values. All values were 3 to be exact. And as soon as I press ok button error pops up.
code for main class:
import javax.swing.JFrame;
public class Main {
private int d1;
private int d2;
private int d3;
private int d4;
Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Input inpt = new Input();
inpt.setVisible(true);
inpt.setSize(450, 450);
inpt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void getVals(){
Input in = new Input();
d1 = in.get1();
d2 = in.get2();
d3 = in.get3();
d4 = in.get4();
}
/*public void calculate(){
if (d2 == d3) {
int[][] MatA = new int[d1][d2];
for (int i = 0; i < d1; i++) {
for (int j = 0; j < d2; j++) {
System.out.printf(" Enter [%d,%d]th element of first matrix", i + 1, j + 1);
MatA[i][j] = input.nextInt();
}
}
int[][] MatB = new int[d3][d4];
for (int i = 0; i < d3; i++) {
for (int j = 0; j < d4; j++) {
System.out.printf(" Enter [%d,%d]th element of second matrix", i + 1, j + 1);
MatB[i][j] = input.nextInt();
}
}
int newM[][] = new int[d1][d4];
int y;
for (int x = 0; x < d1; x++) {
y = 0;
int a = 0;
while (y < d4) {
if (a < d2) {
newM[x][y] += MatA[x][a] * MatB[a][y];
a++;
} else {
y++;
a=0;
}
}
}
for (int a[] : newM) {
for (int b : a) {
System.out.printf("%d \n", b);
}}
}else{
System.out.println("Sorry Multiplication not possible ! Dimention error !");
}
endstopper();
}
public static void endstopper(){
Scanner input = new Scanner(System.in);
input.nextLine();
}*/
}
I have commented the calculate block cause still its not invoked. I have not deleted Scanner because it was used in dry run.