Question

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.

public Location locateLargest(int[][] x){ 
    int maxValue = getMax(x);
    for (int i = 0; i < x.length; i++){
        for (int j = 0; j < x.length; j++)
            if (x[i][j] == maxValue)
                return new Location(i,j);
    }
}

My toString attempt:

public String toString(){
    return "[" + i + "][" + j + "]";
}

location class 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;
    }

Here is my full code for my program:

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 < array.length; r++){
            for (int c = 0; c < 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 < x.length; i++){
            for (int j = 0; j < x[i].length; j++){
                if (x[i][j] > max)              
                    max = x[i][j];
            }
        }
        return max;
    }

    public Location locateLargest(int[][] x){ 
        int maxValue = getMax(x);
        for (int i = 0; i < x.length; i++){
            for (int j = 0; j < 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 + "]";
    }
}
Was it helpful?

Solution

If the Location is a class your defined yourself, you should implement function toString() as well

class Location{
    int locationX,locationY;    

    public Location(int x,int y){
        locationX = x;
        locationY = y;
    }

    public String toString(){
        return locationX+","+locationY;
    }
}

OTHER TIPS

A toString method has the header

public String toString()

so all you need to do is make a method with that header that returns a String.

Your question is pretty vague as someone noted above, and Location isn't a standard library class, so I can't give you anything specific, but something like

public String toString() {
    return "(" + x + ", " + y + ")";
}

might be close to what you're going for if you want a toString to represent a point.

What do you need to convert into String? Anyway, whatever you do, add this to the end:

.toString();

You can make a String variable and return it:

StringVariable = <object>.toString();

or just print it or do anything else

System.out.println(<object>.toString());

Hope that helped. If it didn't tell me about it in the comments!

the cleanest way would be to put in your class:

public String toString(){
        return String.format("[%i][%i]",row,column);
    }

im not sure if that's helps but maybe maybe the codes below helps

  return new Location(i,j);
 String toStringI = Integer.toString(i); 
    String ToStringj = integer.toString(j);

or by using String.valueOf(myInteger);

Your class "Named:Location" is set to integer if you are trying to show it in the window or output it try this?

System.out.println("",i,"",j);

whatever ;p

You shoulrd use row column

public String toString(){
    return "[" + row + "][" + column + "]";
}

You can't use i j because no i j exists in Location.

public String toString(){
    return "[" + i + "][" + j + "]";
}

As long as this (below) is your constructor and not what you had before with xLocation and yLocation, you should be fine.

Location(int row, int column){
    this.row = row;
    this.column = column;
}//end arg constructor
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top