``````   Q.Write a program using 2D array. A company manufactures three
products P1, P2 and P3. Company has hired five salesman. Each
salesman sell all three products. Accept from user number of each
product sold by each salesman. Find the product whose sale was
largest.Also find the salesman whose sales were the least.
-------------------------------------------------------------------------------------------------------
THIS IS THE QUESTION.I TRIED A LOT AND CAME UP WITH THIS SOLUTION BUT STILL GETTING ERRORS....PLZ HELP

import java.util.*;
class Salesman
{
public static void main(String args[])
{
int i,j;
int s[][]=new int[5][4];
int salesman_hs=s[0][3];
int salesman_ind=0;
int max_prod=0;
int prod_ind=0;
int sum;
Scanner sc= new Scanner(System.in);
for(i=0;i<=4;i++)
{
System.out.println("Enter product sold by salesman"+(i+1));
for(j=0;j<=2;j++)
{
System.out.println("Enter sale for product"+(j+1));
s[i][j]=sc.nextInt();
s[i][3]=s[i][3]+s[i][j];
}
if(s[i][3]>salesman_hs)
{
salesman_hs=s[i][3];
salesman_ind=i;
}
}

for(i=0;i<=2;i++)
{
sum=0;
for(j=0;j<=4;j++)
{
sum=sum+s[j][i];
}
if(max_prod<sum)
{
max_prod=sum;
prod_ind=i;
}
}
}
System.out.println("p1\t p2\t p3\t total");
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
System.out.print(s[i][j]+"\t");
}
System.out.println();
}
System.out.println("\n Salesman"+(salesman_ind+1)+"has highest sales of"+salesman_hs);
System.out.println("Product"+(prod_ind+1)+"has the highest sales of"+max_prod);

}``````

You accidentally put your entire post in a code block so that's OK. You can try harder from now on.
You wrote there are errors but didn't call out what the error was or what line it happens on. Add that next.

Sorry for that.actually i am completely new to this...
This was the actual program that i wrote.
``````import java.util.*;
class Salesman
{
public static void main(String args[])
{
int i,j;
int s[][]=new int[5][4];
Scanner sc= new Scanner(System.in);
for(i=0;i<=4;i++)
{
System.out.println("Enter product sold by salesman"+(i+1));
for(j=0;j<=2;j++)
{
System.out.println("Enter sale for product"+(j+1));
s[i][j]=sc.nextInt();
s[i][3]=s[i][3]+s[i][j];
}
int salesman_hs=s[0][3];
int salesman_ind=0;
for(i=1;1<=4;i++)
{
if(s[i][3]>salesman_hs)
{
salesman_hs=s[i][3];
salesman_ind=i;
}
}
int max_prod=0;
int prod_ind=0;
int sum;
for(i=0;i<=2;i++)
{
sum=sum+s[j][i];
}
if(max_prod<sum)
{
max_prod=sum;
prod_ind=i;
}
}
System.out.println("p1\t p2\t p3\t total");
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
System.out.print(s[i][j]+"\t");
}
System.out.println();
}
System.out.println("\n Salesman"+(salesman_ind+1)+"has the hightest sales of"+salesman_hs);
System.out.println("Product"+(prod_ind+1)+"has the highest sales of"+max_prod);
}
}           ``````

This is the error i am getting in the output.I have declared the variables, still it is showing variable not found.
output=

``````javac "Salesman.java" (in directory: D:\)
Salesman.java:50: error: cannot find symbol
System.out.println("\n Salesman"+(salesman_ind+1)+"has the hightest sales of"+salesman_hs);
^
symbol:   variable salesman_ind
location: class Salesman
Salesman.java:50: error: cannot find symbol
System.out.println("\n Salesman"+(salesman_ind+1)+"has the hightest sales of"+salesman_hs);
^
symbol:   variable salesman_hs
location: class Salesman
Salesman.java:51: error: cannot find symbol
System.out.println("Product"+(prod_ind+1)+"has the highest sales of"+max_prod);
^
symbol:   variable prod_ind
location: class Salesman
Salesman.java:51: error: cannot find symbol
System.out.println("Product"+(prod_ind+1)+"has the highest sales of"+max_prod);
^
symbol:   variable max_prod
location: class Salesman
4 errors
Compilation failed.``````

This is a variable scope error. You declared the variables salesman_ind, prod_ind, salesman_hs, max_prod in the code bracketed by the brackets starting on lines 10 and ending on line 40. That means you cannot use those variables outside lines 10 to 40. They go "out of scope" after line 40 and are destroyed so you cannot use them in lines 50 and 51. That is your error. You can think of bracket pairs as circles within circles within circles. If a variable is created inside a circle, it can be used in that circle and it can be used in the inner circles, but it cannot be used in the outer circles. Think of fencing around each circle with a guard and a gate. You can pass into an inner gate with a variable, but you cannot take it outside the gate you created it inside.

If you declare those four variables below line 5 and above line 9, you will be able to use them in lines 50 and 51 because the relevant brackets would then be on lines 5 and 52. The code on line 50 and line 51 would then be in scope and you will not get those errors.

Except people viewing the post, if someon comes forward and helps it would have been really good !

I did as you said but still it gave me 1 error.

``````import java.util.*;
class Salesman
{
public static void main(String args[])
{
int i,j;
int s[][]=new int[5][4];
int salesman_hs=s[0][3];
int salesman_ind=0;
int max_prod=0;
int prod_ind=0;
int sum;
Scanner sc= new Scanner(System.in);
for(i=0;i<=4;i++)
{
System.out.println("Enter product sold by salesman"+(i+1));
for(j=0;j<=2;j++)
{
System.out.println("Enter sale for product"+(j+1));
s[i][j]=sc.nextInt();
s[i][3]=s[i][3]+s[i][j];
}
for(i=1;1<=4;i++)
{
if(s[i][3]>salesman_hs)
{
salesman_hs=s[i][3];
salesman_ind=i;
}
}
for(i=0;i<=2;i++)
{
sum=sum+s[j][i];
}
if(max_prod<sum)
{
max_prod=sum;
prod_ind=i;
}
}
System.out.println("p1\t p2\t p3\t total");
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
System.out.print(s[i][j]+"\t");
}
System.out.println();
}
System.out.println("\n Salesman"+(salesman_ind+1)+"has the hightest sales of"+salesman_hs);
System.out.println("Product"+(prod_ind+1)+"has the highest sales of"+max_prod);
}
}           ``````

This is the error i am getting.

``````javac "salesmanfix.java" (in directory: D:\)
salesmanfix.java:31: error: unreachable statement
for(i=0;i<=2;i++)
^
1 error
Compilation failed.``````

You have a for-loop using i as the loop control variable in line 14. Then you have it again on line 23 and 31, so your inner loop counts are messing up your outer loop counts and vice-versa. Perhaps you want to use j instead of i as the loop counter for the loops starting in lines 23 and 31?

There's also a problem on line 23...

``for(i=1;1<=4;i++)``

I'm guessing you did not intend to test whether one is less than four, because it always is. Hence, infinite loop from lines 23 to 30.

Finally i was able to compile successfully....thanks a lot...but now i am having error with output
`````` import java.util.*;
class Salesmanfix
{
public static void main(String args[])
{
int i,j;
int s[][]=new int[5][4];
int salesman_hs=s[0][3];
int salesman_ind=0;
int max_prod=0;
int prod_ind=0;
int sum=1;
Scanner sc= new Scanner(System.in);
for(i=0;i<=4;i++)
{
System.out.println("Enter product sold by salesman"+(i+1));
for(j=0;j<=2;j++)
{
System.out.println("Enter sale for product"+(j+1));
s[i][j]=sc.nextInt();
s[i][3]=s[i][3]+s[i][j];
}
for(j=1;j<=4;j++)
{
if(s[i][3]>salesman_hs)
{
salesman_hs=s[i][3];
salesman_ind=i;
}
}
for(j=0;j<=2;j++)
{
sum=sum+s[j][i];
}
if(max_prod<sum)
{
max_prod=sum;
prod_ind=j;
}
}
System.out.println("p1\t p2\t p3\t total");
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
System.out.print(s[i][j]+"\t");
}
System.out.println();
}
System.out.println("\n Salesman"+(salesman_ind+1)+"has the hightest sales of"+salesman_hs);
System.out.println("Product"+(prod_ind+1)+"has the highest sales of"+max_prod);
}
}       ``````
Output
``````Enter product sold by salesman1
Enter sale for product1
1
Enter sale for product2
2
Enter sale for product3
3
Enter product sold by salesman2
Enter sale for product1
4
Enter sale for product2
5
Enter sale for product3
6
Enter product sold by salesman3
Enter sale for product1
7
Enter sale for product2
8
Enter sale for product3
9
Enter product sold by salesman4
Enter sale for product1
1
Enter sale for product2
2
Enter sale for product3
3
Enter product sold by salesman5
Enter sale for product1
4
Enter sale for product2
5
Enter sale for product3
6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Salesmanfix.main(Salesmanfix.java:33)
Press any key to continue . . .``````

I'm seeing some possible problems involving i and j. If the first matrix index is supposed to refer to i and the second matrix index refers to j, make sure that is what happens. By the same token, I see a loop starting on line 23 where j varies, but the code inside the loop uses i, not j. Make sure that's what you want.

Yeah you were right. I didnt realize it. But still in output its taking only input for salesman1 and directly showing output for others as 0.

`````` import java.util.*;
class Salesmanfix
{
public static void main(String args[])
{
int i,j;
int s[][]=new int[5][4];
int salesman_hs=s[0][3];
int salesman_ind=0;
int max_prod=0;
int prod_ind=0;
int sum;
Scanner sc= new Scanner(System.in);
for(i=0;i<=4;i++)
{
System.out.println("Enter product sold by salesman"+(i+1));
for(j=0;j<=2;j++)
{
System.out.println("Enter sale for product"+(j+1));
s[i][j]=sc.nextInt();
s[i][3]=s[i][3]+s[i][j];
}
for(i=1;i<=4;i++)
{
if(s[i][3]>salesman_hs)
{
salesman_hs=s[i][3];
salesman_ind=i;
}
}
for(i=0;i<=2;i++)
{
sum=0;
for(j=0;j<=4;j++)
{
sum=sum+s[j][i];
}
if(max_prod<sum)
{
max_prod=sum;
prod_ind=i;
}
}
System.out.println("p1\t p2\t p3\t total");
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
System.out.print(s[i][j]+"\t");
}
System.out.println();
}
System.out.println("\n Salesman"+(salesman_ind+1)+"has the hightest sales of"+salesman_hs);
System.out.println("Product"+(prod_ind+1)+"has the highest sales of"+max_prod);
}
}
}              ``````

Output :

``````Enter product sold by salesman1
Enter sale for product1
1
Enter sale for product2
54
Enter sale for product3
23
p1       p2      p3      total
1       54      23      78
0       0       0       0
0       0       0       0
0       0       0       0
0       0       0       0

Salesman1has the hightest sales of0
Product2has the highest sales of54
Press any key to continue . . .``````

Any idea what has gone wrong ??? @AssertNull

I finally found out the error.It was with one of the for loop....Thanks everyone for the help....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.