Note that there are some explanatory texts on larger screens.

plurals
  1. POJAVA: Trouble putting values into a toString method
    text
    copied!<p>My method locateLargest() as show below is a method to find the coordinates of my largest value in my array. I'm having trouble putting the return values into a toString method. I have no idea how to format it into the toString method.</p> <pre><code>public Location locateLargest(int[][] x){ int maxValue = getMax(x); for (int i = 0; i &lt; x.length; i++){ for (int j = 0; j &lt; x.length; j++) if (x[i][j] == maxValue) return new Location(i,j); } } </code></pre> <p>My toString attempt:</p> <pre><code>public String toString(){ return "[" + i + "][" + j + "]"; } </code></pre> <p>location class code:</p> <pre><code>class Location { private int row; private int column; Location(){}//end constructor Location(int row, int column){ this.row = row; this.column = column; }//end arg constructor public int getRow(){ return this.row; } public int getColumn(){ return this.column; } </code></pre> <p>Here is my full code for my program:</p> <pre><code>import java.util.Scanner; public class LocationTest { public static void main(String[] args) { Scanner numberInput = new Scanner(System.in); System.out.println("Enter the number of rows and columns of the array: "); int row = numberInput.nextInt(); int column = numberInput.nextInt(); Location l1 = new Location(row, column); Location l2 = new Location(); row = l1.getRow(); column = l1.getColumn(); int[][] array = new int[l1.getRow()][l1.getColumn()]; System.out.println("Please enter the array elements: "); for (int r = 0; r &lt; array.length; r++){ for (int c = 0; c &lt; array[r].length; c++){ array[r][c] = numberInput.nextInt(); }//end nested loop }//end for loop System.out.println(getMax(array)); System.out.println(l1.locateLargest(array).toString()); } public static int getMax(int[][] x){ int max = Integer.MIN_VALUE; for (int i = 0; i &lt; x.length; i++){ for (int j = 0; j &lt; x[i].length; j++){ if (x[i][j] &gt; max) max = x[i][j]; } } return max; } public Location locateLargest(int[][] x){ int maxValue = getMax(x); for (int i = 0; i &lt; x.length; i++){ for (int j = 0; j &lt; x.length; j++) if (x[i][j] == maxValue) return new Location(i,j); } return null; } } class Location { private int row; private int column; Location(){}//end constructor Location(int row, int column){ this.row = row; this.column = column; }//end arg constructor public int getRow(){ return this.row; } public int getColumn(){ return this.column; } public String toString(){ return "[" + row + "][" + column + "]"; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload