I wrote this matrix addition program and I dont know why but I keep getting this two errors on lines 61 and 63 and i dont want to handle the exceptions but just throwing would do
error: unreported exception IOException; must be caught or declared
The code of the program is as follows:
import java.io.*;
class Arr
{
int r,c;
int arr[][];
Arr(int r,int c)
{
this.r=r;
this.c=c;
arr=new int[r][c];
}
int[][] getMatrix()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println("enter element");
arr[i][j]=Integer.parseInt(br.readLine());
}
}
return arr;
}
int[][] findsum(int a[][],int b[][])
{
int temp[][]=new int[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
temp[i][j]=a[i][j]+b[i][j];
return temp;
}
void putmatrix(int res[][])
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println(res[i][j]+"\t");
}
}
System.out.println();
}
}
class Matrixsum
{
public static void main(String args[])
{
Arr obj1=new Arr(3,3);
Arr obj2=new Arr(3,3);
int x[][],y[][],z[][];
System.out.println("\nEnter matrix 1");
x=obj1.getMatrix();
System.out.println("Enter matrix 2");
y=obj2.getMatrix();
z=obj1.findsum(x,y);
System.out.println("the sum matrix is");
obj2.putmatrix(z);
}
}
Because getMatrix throws IOException which must be caught or declared as thrown by main.
In your case the simplest solution is to declare it on main.
public static void main(String... args) throws IOException
getMatrix() is declared to throw IOException. Calls to getMatrix() in main() can generate IOExceptions, which are required to be caught by the function or rethrown. To solve the problem, you could declare main() to throw IOException.
The design of class Arr is a bit strange. Instead of using two redundant objects obj1 and obj2, you should condense it to one object, say one called arrProcessor. So the calls would look like:
x=arrProcessor.getMatrix();
y=arrProcessor.getMatrix();
z=arrProcessor.findSum(x,y);
arrProcessor.putMatrix(z);
Even better, instead of using arrays, you should wrap the arrays in objects, say in a class called Matrix:
public class Matrix{
int[][] values;
int numRows, numCols;
protected Matrix(){/*...*/}
public static Matrix getMatrix(int nRows, int nCols){/*...*/}
public static Matrix addMatrices(Matrix a, Matrix b) throws Exception {/*...*/}
public void print(){/*...*/}
public void plus(Matrix another) throws Exception {/*...*/}
}
So now the code would look like:
Matrix x,y,z;
x=Matrix.getMatrix(3,3);
y=Matrix.getMatrix(3,3);
z=x.plus(y);
z.print();
hey i would suggest you this one.
http://commons.apache.org/math/userguide/linear.html
If you tired to write your own code with matrix, just download library I developed. It can add, multiply, invert, transpond, calculate determinant and solve linear systems.
please check: Linear Math Library
This is open source you can download the source code too and look how in works.
Related
Whenever I give the size of array more than 3, my program gives me StackOverflowError.
package database;
import java.util.Scanner;
public class The_Maximum_Subarray {
int a[];
public The_Maximum_Subarray(int size) {
a=new int[size];
}
public int maxsubArray(int[] a,int li,int ui)
{
if(ui ==li)
return a[li];
int m=(ui-li)/2;
int leftMaxSubarray=maxsubArray(a, li, m);
int rightMaxSubarray=maxsubArray(a, m+1, ui);
int leftSum=0,rightSum=0,sum=0;
for(int i=m;i>=li;i--)
{
sum+=a[i];
if(sum>leftSum)
leftSum=sum;
}
sum=0;
for(int i=m+1;i<=ui;i++)
{
sum+=a[i];
if(sum>rightSum)
rightSum=sum;
}
sum=leftSum+rightSum;
if(rightMaxSubarray>=leftMaxSubarray && rightMaxSubarray>=sum)
return rightSum;
else if(leftMaxSubarray>=rightMaxSubarray && leftMaxSubarray>=sum)
return leftSum;
else
return sum;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
The_Maximum_Subarray obj=new The_Maximum_Subarray(size);
for(int j=0;j<size;j++)
obj.a[j]=sc.nextInt();
System.out.println(obj.maxsubArray(obj.a, 0, size-1));
}
}
Can anyone tell me why it's giving me this exception for this small size array ?
From your question:
Can anyone tell me why its giving me this exception for this small size array ?
For the part Why it is causing??
According to the java language specifications for java.lang.StackOverflowError, this error occurs when an application recurses too deeply.
This recursion results in filling the stack and it tries to exceed the limit of the stack which is Xs.
Take a simple example:
class StackOverflowDemo {
private void causeOverflow(int i) {
causeOverflow(i);
System.out.println(i);
}
public static void main(String args[]) {
StackOverflowDemo demo = new StackOverflowDemo();
demo.causeOverflow(5);
}
}
Here, the System.out.println(i) will be called recursively i.e it will be pushed to the stack when it is called.
This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
Closed 7 years ago.
why cant i use System.out.println out side a method and when i try to use the method of my main class in other class it shows identifier not found
may be there are better way to do this . but i would like to learn this what is wrong here
package arraytest; //package declared
import java.util.Scanner; // for input
public class Arr { // this is my main class
void min(int arr[])
{
int min=arr[0];
for(int i=1;i<arr.length;i++)
{ if(min>arr[i])
min=arr[i];
}
System.out.println(min);
}
public static void main(String[] args)
{
Array a=new Array();
a.inputData();
a.display();
a.min();
}
}
class Array
{
int arr[]=new int[5];
Scanner sc= new Scanner(System.in);
// System.out.println(arr.length); will not work why?
void inputData()
{
for(int i=0;i<arr.length;i++)
{
arr[i]=sc.nextInt();
}
}
void display()
{
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
void min()
{
Arr a=new Arr();
a.min(int arr[]); // this shows error
}
}
You can't put code there. Your code has to be inside of a method. The 2 lines above that are declarations, and the System.out.println is code.
Code has to go inside a method.'
You could use a static block if your arr was static, but it's not.
Change you min method as below,
void min() {
Arr a = new Arr();
a.min(arr); // this shows error
}
This is how you pass a reference of an array to the method.
Like from {{1.0,2.0},{3.0,4.0}} and {{0.1,0.2},{0.3,0.4}} to {{1.1,2.2},{3.3,4.4}}. If the two input arrays are different sizes, method should return null.
My code below shows [[D#6d06d69c.
What is wrong with my code?
public class Homework13_1 {
public static double[][] sum(double[][]a,double[][]b){
double[][] newA= new double[a.length][a[0].length];
int c=0;
if ((a.length==b.length)){
for (int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
newA[i][j]=a[i][j]+b[i][j];
}
}
return newA;
}
else{
return null;
}
}
public static void main(String[] args) {
double[][]x={{1,2,3},{2,3,4}};
double[][]y={{2,3,4},{1,1,1}};
double[][] b = {{1,2,-9,7},{3,4,9.9,1.2}};
System.out.println(sum(x, y));
}
}
Java arrays don't override toString. But there is Arrays.deepToString(Object[]) which says (in part) This method is designed for converting multidimensional arrays to strings. You could change
System.out.println(sum(x, y));
to
System.out.println(Arrays.deepToString(sum(x, y)));
My aim here is to type a simple code to take in number input from the user and print a simple matrix. The code I typed seems to compile but doesn't work during run time! My code is something like this:
import java.util.Scanner;
class Arr
{
public static void main()
{Scanner in=new Scanner(System.in);
int a[ ][ ]=new int[2][3];
for(int i=0;i<2;i++)
{
for(int y=0;y<2;y++)
{
a[i][y]=in.nextInt();
}
}
for(int i=0;i<2;i++)
{
for(int y=0;y<2;y++)
{
System.out.print(a[i][y]);
}
}
}
}
At the same time could you suggest a solution if I were to transpose the matrix inputted by the user?
You have defined the main method incorrectly. The proper main method signature is
public static void main (String[] args)
That's why the compiler is not compiling your code.
I'm not very experienced in making static methods...I wanted some practice and I'm having some problems. I'm trying to make a program where you input a number and it prints out all the squares less than b. For example, if you put in 100, it returns 0, 1, 4, 9, 16, 25, 36, 49, 64, 81.
I'm getting errors, though.
Illegal modifier for parameter getSquares; only final is permitted. This is on the line public static double getSquares(double b)
-The method getSquares(int) is undefined for the type Squares when I try to do Squares.getSquares(100);...I'm guessing this is because of my first problem. Please help me, I know static methods are important but I don't know how to make them.
package Testers;
import java.util.Scanner;
public class Squares
{
public static void main(String[] args)
{
Squares.getSquares(100);
public static double getSquares(double b)
{
double sqrtNum=Math.sqrt(b);
int i=0;
while(i<sqrtNum)
{
sqrtNum=Math.pow(i,2);
System.out.print(sqrtNum+" ");
i++;
}
}
}
}
You can't declare a method inside a method - format your code and it is clearer to see. Example:
package Testers;
import java.util.Scanner;
public class Squares {
public static void main(String[] args) {
Squares.getSquares(100);
}
public static double getSquares(double b) {
double sqrtNum = Math.sqrt(b);
int i = 0;
while(i < sqrtNum) {
sqrtNum = Math.pow(i, 2);
System.out.print(sqrtNum + " ");
i++;
}
}
}
Also, there is no returned value in getSquares() - it looks like you intended to make it void.
Finally, this while loop:
int i = 0;
while(i < sqrtNum) {
// code
i++;
}
can be simplified to this for loop:
for (int i = 0; i < sqrtNum; i++) {
// code
}
Your static method should not be in main() if you want it to be a method of the Squares class. it should be in Squares and not in main, like:
public class Squares
{
public static void main(..) {...}
public static double getSquares(...) {...}
}
You're declaring your method in another method, which doesn't work. Put it outside and it should be good.
package Testers;
import java.util.Scanner;
public class Squares
{
public static void main(String[] args)
{
Squares.getSquares(100);
}
public static double getSquares(double b)
{
double sqrtNum=Math.sqrt(b);
int i=0;
while(i<sqrtNum)
{
sqrtNum=Math.pow(i,2);
System.out.print(sqrtNum+" ");
i++;
}
}
}
In your getSquares method you need a return statement.