+ 13

SOME ERROR DUE TO DECLARATION OF ARRAY TWO TIMES

https://code.sololearn.com/cqQY720P8Um0/?ref=app plzzz give any idea to correct it and to get desired result thanks ☺

27th Sep 2017, 7:44 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
12 Antworten
+ 10
Tried to write some input error handling for you. Not all cases are caught (see comments), but I hope it will help you to get along ;) https://code.sololearn.com/camI0d8994k7/?ref=app
7th Oct 2017, 7:40 PM
Tashi N
Tashi N - avatar
+ 12
It wouldn't work for C++ as well ;) Anyways, you just need some restructuring, like public static void main(String[] args) { Scanner sc = new Scanner(System.in); int c,d,m=0,n=0; double mod=0.0; try{ //taking input (ie, entered matrix mtrx) m=sc.nextInt(); //no. of rows of matrix n=sc.nextInt(); //no. of columns of matrix } catch(Exception e){ errorKiller(); } double mtrx[][]=new double[m][n]; for(c=0;c<m;c++){ for(d=0;d<n;d++){ mtrx[c][d]=sc.nextDouble(); } } And the method errorKiller should exit your program, for you didn't receive valid input with which you could proceed. If you were not doing that at the playground you would rather put that inside a loop to prompt again for a valid input.
27th Sep 2017, 8:41 PM
Tashi N
Tashi N - avatar
+ 11
//see line 36 and 41 //example ::: int x; try {x=2.0; } //how to do this with array int arr [][]; try { arr [][]=new int [1][1]; } //can we do this ... //i am waiting 😐
27th Sep 2017, 7:59 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 11
again a doubt 😅 , what if someone entered string as element of matrix ... it will show exceptions ... can't we deal with all the exceptions ... making it fully error free
27th Sep 2017, 8:47 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 11
Oh, wait. Forgot to put the 6 lines from double mtrx into the try block ;) But I guess you'll manage that.
27th Sep 2017, 8:47 PM
Tashi N
Tashi N - avatar
+ 11
And put the code after the loop, where the matrix is used to a method an call it from the try block. (As you might have noticed this is a quick and dirty fix, but it will make it run. Try to refactor it after that) [Edit] Sry, it's late for me. Maybe you'll have a new version tomorrow, then I'll think about it again ;)
27th Sep 2017, 8:51 PM
Tashi N
Tashi N - avatar
+ 10
thanks
27th Sep 2017, 8:44 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 10
You're welcome ^^
27th Sep 2017, 8:45 PM
Tashi N
Tashi N - avatar
+ 10
i hv removed the errorhandlers , code is working ... will try it tomorrow again , gn 4 now
27th Sep 2017, 9:08 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 9
i was just wanted to handle all exceptions ... thats the point ... still some exceptions can't be handled ... i will try it again when find some time ... anyways thanks again ☺ @tashi noob means ??? @babak
27th Sep 2017, 8:54 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 7
thnx😊😊 4 the help @tashi N //i will see it ( busy 4 some days) /* Validating user input (matrix) for Gaurav Agrawal. There's code, too... Scroll down ;) Input test cases: 4 3 3 4 5 1 2 3 6 7 8 0 0 0 3 4 3 4 5 5 1 2 3 8 6 7 8 4 fail 4 3 4 5 5 1 2 3 8 6 7 8 4 0 4 3 4 3 4 5 5 1 2 3 8 6 7 tadaa! 4 FIX ME! Known issue - inputs like this are uncaught: 3 1 3 4 5 5 5 6 7 0 1 2 3 8 6 7 */ import java.util.*; public class Matrix { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = getInput(new Integer(0), sc); int n = getInput(new Integer(0), sc); double matrix[][]=new double[m][n]; for (int i = 0; i < m; i++) { System.out.println(); for (int j = 0; j < n; j++) { matrix[i][j]=getInput(new Double(0.0), sc); System.out.print(matrix[i][j] + " "); } } } private static int getInput(Integer i, Scanner sc) { try { i = sc.nextInt(); } catch (NoSuchElementException e) { System.out.println("Invalid input.\nPlease input int values for the number of rows and columns."); System.exit(0); }; if (i==0) { System.out.println("Zeriously zero? Are you kiddin' me?"); System.exit(0); } return i; } private static double getInput(Double d, Scanner sc) { try { d=sc.nextDouble(); } catch (NoSuchElementException e) { System.out.println("Invalid input.\nPlease input (all) double values for the matrix."); System.exit(0); }; return d; } }
8th Oct 2017, 1:37 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Those kind of errors occur some times and it's important to fix it as quick as possible in order to keep your code safety. Those errors can be handled right after they are detected. If it's getting too hard for you there are programs that help doing that, such as checkamrx and others that work fine. Wish you well.
14th Oct 2017, 3:27 PM
Ben hart