|
nt_virus wrote: how to rectify this error any suggestion.
I think this statement for (int i=0;i<m;m++) has a slight error.
|
|
|
|
|
but that line add column values for 2nd row and so on till
|
|
|
|
|
nt_virus wrote: but that line add column values for 2nd row and so on till
Look closely, at the end of the loop you are incrementing the wrong variable
|
|
|
|
|
poor me...
I modified as following ,, and I m done around 90%
package javaapplication6;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter number of rows");
int m = Integer.parseInt(br.readLine());
System.out.println("Enter number of columns");
int n = Integer.parseInt(br.readLine());
int[][] first = new int[m][n];
int[][] second = new int[m][n];
int[][] sum = new int[m][n];
System.out.println("Enter Value of first matrix");
{
for (int i=0;i<first.length;i++)
{
System.out.println("Row no. " + (i+1));
for (int j=0; j<first[i].length;j++)
{
System.out.println("Enter value for column no. " + (j+1));
first[i][j] = Integer.parseInt(br.readLine());
}
}
}
System.out.println("Enter value of second matrix");
{
for (int i=0;i<second.length;i++)
{
System.out.println("Row no. " + (i+1));
for (int j=0;j<second[i].length;j++)
{
System.out.println("Enter value for column no. " + (j+1));
second[i][j] = Integer.parseInt(br.readLine());
}
}
}
System.out.println("Sum of two matrices are:");
{
for (int i=0;i<first.length;i++)
{
for (int j = 0; j<second.length;j++)
{
sum[m][n] = first[i][j] + second[i][j];
}
}
System.out.println(sum[m][n]);
}
}
catch (IOException e)
{
System.out.println(e);
}
}
}
same error is showing when i m adding two arrays..
|
|
|
|
|
nt_virus wrote: sum[m][n] = first[i][j] + second[i][j];
Sorry but I stupidly missed this first time round, but you are using the wrong indices again. sum[m][n] is an array of size m by n, so the highest cell that can be addressed is sum[m-1][n-1] . Change it to sum[i][j] and I think it should work.
|
|
|
|
|
We both post posted at the same time. haha.
|
|
|
|
|
99% done..
for (int i=0;i<first.length;i++)
{
for (int j = 0; j<second.length;j++)
{
sum[i][j] = first[i][j] + second[i][j];
System.out.println(sum[i][j]);
}
But result are showing in vertical manner.. not like matrix way..
|
|
|
|
|
nt_virus wrote: But result are showing in vertical manner.. not like matrix way..
I can never remember what the order is supposed to be (I stick to one-dmensional arrays), so I'm afraid you will have to find it by trial and error. I would give you a hand but my Java system is not usable today.
|
|
|
|
|
please do it in c# if you can, I will convert. I did my all possible things.
|
|
|
|
|
OK, but it may take some time.
|
|
|
|
|
yes, no problem. do you use messenger ?
|
|
|
|
|
OK, I got the answer in C#, so I think the Java code needs to be:
for (int i=0;i < first.length;i++)
{
for (int j = 0; j < first[i].length;j++)
{
sum[i][j] = first[i][j] + second[i][j];
System.out.print(sum[i][j] + " ");
}
System.out.println("");
}
I'm not sure about the expression first.length , but basically i needs to iterate the rows, and j the columns. You also need to print each column value without the newline (print rather than println ?) and then put the System.out.println(""); statement after the last column. It probably needs some tidying up to get the columns lined up, but I'm sure you will be able to manage it.
No, not on messenger.
|
|
|
|
|
fantastic.. It works.. you made my day (night here actually) ..
Thanks a lot.
|
|
|
|
|
Cheers, happy to help. It also gave me some incentive to get back on Java which I haven't touched for a while.
It's early evening here (UK) so are you west or east?
|
|
|
|
|
Java is very nice.
I m on east. India. +5.30
|
|
|
|
|
You may find this hard to believe but that's the country of my birth! I was born in Mussoorie (in the hills), my dad and my brother in Naini Tal.
But my only return visit was in 2005 to Bangalore, which also was nice - at least the people.
|
|
|
|
|
Hello I would like to ask a simple way to load a picture in JFrame?
I was a planning to convert my txt based game into a JFrame with a simple image
design but I dont know yet how to load a picture in JFrame.
thank if in case someone give suggestion,
dfan23
|
|
|
|
|
|
|
I am not sure whether your question is a concept or an actual implementation either way you already have the list of registered news papers just add a method to the agency class something like broadcast newspaper that takes the news papers names as parameters. In the news papers class you will have a method that listens for the broadcast.
Good luck
|
|
|
|
|
|
What is the scope of the implementation? and include the code you did so that I could analyse and give a solution.
|
|
|
|
|
|
Classes:
Agency will create 3 Files and append to them:
1. Agency: The name of it
2. AgencyNews: relating agency and news
3. News: the news of the newspaper add optional newspaper name and datetime
Steps:
Newspaper will choose which agency to write the news. The name of the newspaper and the news will be added by the agency to the files.
Agency will broadcast it by simply looping its file and getting the newspapers names then going to the AgencyNews file and locating its news file then loop over the news files and prints out the news to all the newspapers.
Things to look for:
1. Arraylist
2. append to text file
3. File directory and name
The above solution is to store more data rather than calling the methods over and over for every transaction.
This relation is better suited with database.
Hope this helps
|
|
|
|
|
Hello everybody, Is there such datagrid control in Java like we have in VS? Thank you.
modified on Wednesday, October 14, 2009 9:47 AM
|
|
|
|