Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: I need help in finding the max and min value of total of 2d array if possible please

1. I need help in finding the max and min value of total of 2d array if possible please

Hi everyone
I thank you a lot for taking the time to read my code
I need to display a two dimensional array, using two classes, calculate the total of each row, and display the maximum total and the minumum total( I was able to do everything except for the maximum and minimum) and I have to submit my project on wednesday, tried my best and thats what I could formulate, please help
This is my code for the first class:
```package companyproject;
public class company {
private int [][] arrayname={{88,45,73},{33,45,58},{58,45,27},{28,45,55}};
private int max=0;
private int min=0;
public void Output ()
{  for (int x=0; x<3; x++)
{System.out.printf("\tProduct %d \t", x+1);  }
System.out.printf("Total");
for (int row=0; row<arrayname.length; row++)
{System.out.println("");
System.out.printf("Salesperson %d\t", row+1);
for (int column=0; column<arrayname[row].length; column++)
System.out.printf("%d\t\t",arrayname[row][column]);
int total = totalx (arrayname[row]);
System.out.printf("%d", total);
}
}
public int totalx (int [] arrayname)
{  int total=0;  for (int aaa=0; aaa<arrayname.length; aaa++)
{total=total+arrayname[aaa];}
if (total > max) {max=total;  }
System.out.printf("The maximum number is %d", max);
if (total < min) {min=total;}
System.out.printf("The minimum number is %d", min);
return 0;
}
}```
Thats the code for the second class in which I should display the first class:
```package companyproject;

public class companytext {
public static void main (String [] args)

{

company totally = new company ();

totally.Output();

}

}```
Unfortunately thats what Im getting as an output

The maximum number is 206The minimum number is 00
after each row (Under total), while I should get the sum of the row

I should get the maximum number and minimum number under the 2d array once
I thank you a lot again for dedicating your time to help me

2. Re: I need help in finding the max and min value of total of 2d array if possible ple

You should try debugging your code by adding println statement to show the values of variables as they change and to show logic flow.

Can you show what the output should look like? I don't understand your comments.

Cross posted at
I need help in finding the max and min value of total of 2d array if possible please - Dev Shed

3. Re: I need help in finding the max and min value of total of 2d array if possible ple

Yes dear, I posted this post in another java forum but MrFujin was not able to help because that other forum didn't allow the attachment of images

4. Re: I need help in finding the max and min value of total of 2d array if possible ple

That should be the output dear

5. Re: I need help in finding the max and min value of total of 2d array if possible ple

One trick that is useful in finding the max and the min of a value is to initialize the starting value for the max to a number smaller than the smallest data value and the starting value for the min to a number BIGGER than the biggest data value.

If you change this variable so that the totals are changed:

private int [][] arrayname={{88,45,73},{33,45,58},{158,45,27},{28,4 5,55}};

and look at the output, you'll get some info on how your program is working.

You need to step thru your logic and see why the output is being printed the way it is.

6. Re: I need help in finding the max and min value of total of 2d array if possible ple

Originally Posted by Norm
You should try debugging your code by adding println statement to show the values of variables as they change and to show logic flow.
Debugging With System.out.println()