'How should I reconfigure the arrow "->" to not print when done with with my pathing?

I'm trying to create an optimal path to collect as many 1's as I can but after I execute my code, I still have an arrow pointing to nothing as there are no more places to go. How would I remove the arrow at the end of the code?

import java.util.Arrays;
import java.util.Scanner;

public class Main{
  public static void main(String[] args){
    Scanner s1 = new Scanner(System.in);
    int n = s1.nextInt();
    int m = s1.nextInt();
    int mat[][] = new int[n][m];
    for (int i = 0; i < mat.length; i++){
      for (int j = 0; j < mat[0].length; j++){
        mat[i][j] = s1.nextInt();
      } 
    }
    int path[][] = new int[n][m];
    for (int i = 0; i < path.length; i++){
      Arrays.fill(path[i], -1);
    }
      
    int maxCoins = util(0, 0, mat, path);
    System.out.println("Max coins:" + maxCoins);
      
    int row = 0, column = 0;
    System.out.print("Path:");
     
    while(row < mat.length && column < mat[0].length){
      System.out.print("(" + (row + 1) + "," + (column + 1) + ")");
      System.out.print("->");
      
      if(row < n - 1 && column < m - 1){
        int down = path[row + 1][column];
        int right = path[row][column + 1];
        if(down > right){
          row += 1;
          continue;
        }
        else if (right > down){
          column += 1;
          continue;
        }
        else{
          row += 1;
          continue;
        }
      }
      if(row + 1 < n){
        row += 1;
      }
      else{
        column += 1;
      }
    }
  }
  
  private static int util(int row,int column,int mat[][], int path[][]){
    if(row >= mat.length || column >= mat[0].length){
      return 0;
    }
    
    if(path[row][column]!= -1){
      return path[row][column];
    }
    
    int right = util(row, column + 1, mat,path);
    int down = util(row + 1, column, mat,path);
    
    path[row][column]=Math.max(right, down);
    if(mat[row][column] == 1){
      path[row][column] += 1;
    }
    
    return path[row][column];
  }
}

My current input looks like:

5 6
0 0 0 0 1 0
0 1 0 1 0 0
0 0 0 1 0 1
0 0 1 0 0 1
1 0 0 0 1 0

And output is:

Max coins:5
Path:(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(3,6)->(4,6)->(5,6)->

I am just trying to remove the one at the end but unsure where to insert my code:

System.out.print("->");


Solution 1:[1]

Cleanest way would be using a StringJoiner. You can use it as follows

StringJoiner joiner = new StringJoiner("->");
joiner.add("a");
joiner.add("b");
System.out.println(joiner); //prints a->b - you can use toString if you want to return a joined String

You can also define a prefix and suffix for your joined String.

Or if you are familiar with Streams, there is Collectors.joining("->") available.

Solution 2:[2]

Three solutions that come to mind:

  1. Add another check inside the loop, and put your sysout -> thingy after that check.
  2. Usually code would generate some kind of list or similar data about the results and return it. It's a lot simpler to print lists, because you know the length etc.
  3. Another common solution is to use StringBuilder and correct it before generating the output with toString()

Solution 3:[3]

You could just do something like this:

if (!(row == mat.length - 1 && column == mat[0].length - 1)) {
    System.out.print("->");
}

Or a little cleaner:

if (arrowIsNotAtTheEnd(mat, row, column)) {
    System.out.print("->");
}

// ...

private static boolean arrowIsNotAtTheEnd(int[][] mat, int row, int column) {
    return !(row == mat.length - 1 && column == mat[0].length - 1);
}

Solution 4:[4]

For java 8 and above, the String class already has a convenient join method.

CharSequence[] path=new CharSequence[]{
        "(1,1)","(2,1)","(2,2)","(2,3)","(2,4)","(3,4)","(3,5)","(3,6)","(4,6)","(5,6)"};

String output=String.join("->",path);
System.out.println(output);
//output: (1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(3,6)->(4,6)->(5,6)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 H3AR7B3A7
Solution 4 Mark Rotteveel