Hello everyone, I'm new to Java. I found this site and the members to have a lot of knowledge and was hoping I can get some assistance.

I have written this 2D array code to find the lowest value in each row and return the values. "getLowestInRow" is the method I'm using. This method should return the total of the lowest value in the specified row of array. I can get it to return only if I have this in the loop: "System.out.println("The lowest of row " + row + " is " + low);"

This is the result that I get:

The lowest of row 0 is 1
The lowest of row 1 is 4
The lowest of row 2 is 7
The lowest of row 3 is 10

4 x 3 array

The lowest of row 0 is 1
The lowest of row 1 is 4
The lowest of row 2 is 7
The lowest of row 3 is 10

How can I have the method return the total of the lowest value in the specified row of array without a print statement in the loop. If I remove the statement, the method does not return anything, but with it in, it prints while going through loop and when it's return. Thanks in advance.

public class IssueWithDoublePrint
{
		public static void main (String [] args)
		{
				// Declare a 2D array with 4 rows and 3 columns.
				int [] [] array = {{1,2,3},
					           {4,5,6},
					           {7,8,9},
						   {10,11,12}};
				
				
				// Initialize							
				int total = 0;
				int row = 0;
				int col = 0;
				int rowLowestOfArray = getLowestInRow(array);
				
				// Display the outputs.
				System.out.println("");
      		                System.out.println(array.length + " x " + array[col].length + " array");
				System.out.println("");
				getLowestInRow(array);
							 
 
		}
		
		//Finding the lowest value of the elements in the array in a row and returning the value
		public static int getLowestInRow (int [] [] array)
		{
				int low = array [0] [0];
				
				for (int row = 0; row < array.length; row++)
				{
					
					// Setting low to 0
					low = 0;
										
					for (int col = 0; col < array[row].length; col++) 
						if (array[row][col] > low)
							{
							low = array[row][col];
							}
								for (int col = 0; col < array[row].length; col++)
								{
									if (array[row][col] < low)
									low = array[row][col];
								}
						System.out.println("The lowest of row " + row + " is " + low);
				}		
				return low;
		}

 		

}

Recommended Answers

All 12 Replies

If I remove the statement, the method does not return anything

It should, I think you may be removing something else there then the print statement.

I don't really understand what you want.
Could you explain that a little more?

The print statement should not change the value of a variable.
Can you explain exactly what you see when you execute the code with the print statement
and when you execute the code without the print statement.

Where do you show the value that is returned by the getLowestInRow() method?

Member Avatar for hfx642

I think that I understand what the problem is.
The problem is that you are calling your getLowestInRow() method TWICE!
Once on line #16 and once on line #22.
On line #16, just declare the variable rowLowestOfArray.
Don't initialize it.

Thanks for the prompt responses. That's what I can't figure out how the print statement in the outer loop allows a value to be return to the main method when the getLowestInRow method is executed while nothing is return without the print statement in the getLowestInRow's outer loop. This is the result when I leave the print statement in and when I tell the compiler to ignore it:


With the print statement

----jGRASP exec: java IssueWithDoublePrint

The lowest of row 0 is 1
The lowest of row 1 is 4
The lowest of row 2 is 7
The lowest of row 3 is 10

4 x 3 array

The lowest of row 0 is 1
The lowest of row 1 is 4
The lowest of row 2 is 7
The lowest of row 3 is 10

----jGRASP: operation complete.


Without the print statement

----jGRASP exec: java IssueWithDoublePrint


4 x 3 array


----jGRASP: operation complete.

I think that I understand what the problem is.
The problem is that you are calling your getLowestInRow() method TWICE!
Once on line #16 and once on line #22.
On line #16, just declare the variable rowLowestOfArray.
Don't initialize it.

When I don't initialize it, this is what I get:

----jGRASP exec: java IssueWithDoublePrint


4 x 3 array

10

----jGRASP: operation complete.

It only prints the lowest number of row 3. Like it was only return the row 3, hence the experimental print statement in the outer loop.

Member Avatar for hfx642

I JUST ran your code with MY suggestion, and it works perfect.

4 x 3 array

The lowest of row 0 is 1
The lowest of row 1 is 4
The lowest of row 2 is 7
The lowest of row 3 is 10

I JUST ran your code with MY suggestion, and it works perfect.

4 x 3 array

The lowest of row 0 is 1
The lowest of row 1 is 4
The lowest of row 2 is 7
The lowest of row 3 is 10

I will try again. Can you please post up edited code so I can see where exactly it was modified thanks.

@skracer

Look into your code, and all our comments. Start learning, and dont copy our code. You should be able to find the problem by trying, and more trying, until it works! Programming is never going to itsself, you have to put some time in it!

@skracer

Look into your code, and all our comments. Start learning, and dont copy our code. You should be able to find the problem by trying, and more trying, until it works! Programming is never going to itsself, you have to put some time in it!

Found it! Thanks everyone. I figured that the print statement should have been in the main method instead of the getlowestrow method. I did as hfx642 suggested and it magically work lol. Thanks again everyone.

Your program will not print anything unless you use a print or println statement.
If you take the println statements out of your program it will not print anything.

Please mark the thread as solved if it is solved!

Member Avatar for hfx642

No. The print statment has to be in your method in order to display the lowest is EACH row.

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.