import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class MatrixPath {

    static List<String[]> obstacles = new ArrayList<String[]>();
    static  String[] size = new String[2];
    static String[] startingPoint = new String[2];
    static String[] endPoint = new String[2];

     static int sizeOfRow = 0,sizeOfCol = 0;
    char one = ' '; 

    char two = ' ';
    static char[][] matrix;
    public static void main(String[] args) 
    {
    readFromFile();
    System.out.println();

}
public static String[] calculate(String line ) {
    line = line.substring(1,line.length()-1);
    String[] array = line.split(",");

    return array;
}
public static void readFromFile() {

     int counter = 1;

     try {
        for (String line : Files.readAllLines(Paths.get("C:\\Users\\Computer01\\Desktop\\InputText.txt"))) {
            if(counter <= 3) {
               if(counter == 1) {

                   size = calculate(line);
               }
               if(counter == 2) {

               startingPoint = calculate(line);
               }if(counter == 3) {

                   endPoint = calculate(line);
               }
            }
            else {

                if(calculate(line).length == 1) {
                    sizeOfRow = Integer.valueOf(size[0].trim());
                    sizeOfCol = Integer.valueOf(size[1].trim());
                    matrix = new char[7][6];
                    for(int i=1;i<=sizeOfRow;i++) {
                        for(int j=1;j<=sizeOfCol;j++) {
                            matrix[i][j] = 'W';
                        }
                    }
                    matrix[Integer.parseInt(startingPoint[0].trim())][Integer.parseInt(startingPoint[1].trim())] = 'S';
                    matrix[Integer.parseInt(endPoint[0].trim())][Integer.parseInt(endPoint[1].trim())] = 'E';

                    for(int i = 0;i<obstacles.size(); i++) {
                        matrix[ Integer.valueOf( ((String[])(obstacles.get(i)))[0].trim() ) ][Integer.valueOf( ((String[])(obstacles.get(i)))[1].trim())] = 'O';
                    }
                    System.out.println("The matrix is: ");
                    display(matrix);
                    System.out.println();
                    findPath(Integer.parseInt(startingPoint[0].trim()),Integer.parseInt(startingPoint[1].trim()));
                    System.out.println();
                    System.out.println("**********************************************");
                    System.out.println();
                counter = 0;   
                }
                else {
                    String[] arr = new String[2];
                    arr = calculate(line);
                    obstacles.add(arr);

                }

            }
            counter++;
            }
    } catch (IOException e) {

        e.printStackTrace();
    }

}
public static void display(char[][] mat) {
    for(int i=1;i<=sizeOfRow;i++) {
        for(int j=1;j<=sizeOfCol;j++) {
            System.out.print(mat[i][j]+" ");
        }
        System.out.println();
    }
}
public static char[][] findPath(int i,int j) {
    System.out.println("Vehicle started moving");
    System.out.println();
    int counter = 0; int flag = 0; int startingPointI = i; boolean flagEnd = false;
    int startingPointJ = j;

        outer: while(i <= (Integer.parseInt(endPoint[0].trim())) && j <= (Integer.parseInt(endPoint[1].trim())) ) 
        {
            startingPointI = i;
            startingPointJ = j;
        for(;j<sizeOfCol;j++) {

            if(matrix[i][j+1] == 'E')
            {
                flagEnd = true;
                matrix[i][j+1] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                break outer;
            }
            else if(matrix[i][j+1] != 'O') {
                matrix[i][j+1] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                counter++;

            }
            else 
                break;
        }

        for(;i<sizeOfRow;i++) {
            if(matrix[i+1][j] == 'E') {
                flagEnd = true;

                matrix[i+1][j] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                break outer;
            }
            else if(matrix[i+1][j] != 'O') {
                matrix[i+1][j] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                counter++;

            }

            else
                break;
        }

        for(;j>1;j--) {
            if(matrix[i][j-1] == 'E') {
                flagEnd = true;

                matrix[i][j-1] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                break outer;
            }
            else if(matrix[i][j-1] != 'O') {
                matrix[i][j-1] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                counter++;

            }
            else
                break outer;
        }

        for(;i>startingPointI;i--) {

            if(matrix[i-1][j] == 'E') {
                flagEnd = true;

                matrix[i-1][j] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                break outer;
            }
            else if(matrix[i-1][j] != 'O') {
                matrix[i-1][j] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                counter++;

            }

            else 
                break;
        }
        if(startingPointI == i && startingPointJ == j) {
            if(matrix[i+1][j] != 'O') {

                matrix[i+1][j] = 'S';
                matrix[i][j] = 'W';
                startingPointI++;
                i++;
                display(matrix);
                System.out.println();
                if(flag == 2)
                    {break outer;
                    }flag++;
            }
            else {
                break outer;
            }

        }

        }
        if(flagEnd == true) {
            System.out.println("The vehicle has arrived to the destination");
            System.out.println("The number of steps taken to arrive at the destination is: "+counter);

        }
        else {
            System.out.println("The vehicle has taken "+counter+" steps. But there was no route to reach destination.");
            System.out.println("The number of steps taken to arrive at the destination is: "+counter);

        }
        return matrix;
        }

}

I have written a java program which will take input from file and find the path from starting point to the end point. Number of paths taken doesn't matter, its not mandatory to have efficient program, just the path to reach matters. The input file will contain (size,starting point,end point,obstacles) in the following format:

InputText.txt
(6, 5)
(1, 1)
(4, 4)
(2, 1)
(1, 3)
(2, 4)
(2, 3)
-1

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;

public class MatrixPath {

    static List<String[]> obstacles = new ArrayList<String[]>();
    static  String[] size = new String[2];
    static String[] startingPoint = new String[2];
    static String[] endPoint = new String[2];

     static int sizeOfRow = 0,sizeOfCol = 0;
    char one = ' '; 

    char two = ' ';
    static char[][] matrix;
    public static void main(String[] args) 
    {
    readFromFile();
    System.out.println();

}
public static String[] calculate(String line ) {
    line = line.substring(1,line.length()-1);
    String[] array = line.split(",");

    return array;
}
public static void readFromFile() {

     int counter = 1;

     try {
        for (String line : Files.readAllLines(Paths.get("C:\\Users\\Computer01\\Desktop\\InputText.txt"))) {
            if(counter <= 3) {
               if(counter == 1) {

                   size = calculate(line);
               }
               if(counter == 2) {

               startingPoint = calculate(line);
               }if(counter == 3) {

                   endPoint = calculate(line);
               }
            }
            else {

                if(calculate(line).length == 1) {
                    sizeOfRow = Integer.valueOf(size[0].trim());
                    sizeOfCol = Integer.valueOf(size[1].trim());
                    matrix = new char[7][6];
                    for(int i=1;i<=sizeOfRow;i++) {
                        for(int j=1;j<=sizeOfCol;j++) {
                            matrix[i][j] = 'W';
                        }
                    }
                    matrix[Integer.parseInt(startingPoint[0].trim())][Integer.parseInt(startingPoint[1].trim())] = 'S';
                    matrix[Integer.parseInt(endPoint[0].trim())][Integer.parseInt(endPoint[1].trim())] = 'E';

                    for(int i = 0;i<obstacles.size(); i++) {
                        matrix[ Integer.valueOf( ((String[])(obstacles.get(i)))[0].trim() ) ][Integer.valueOf( ((String[])(obstacles.get(i)))[1].trim())] = 'O';
                    }
                    System.out.println("The matrix is: ");
                    display(matrix);
                    System.out.println();
                    findPath(Integer.parseInt(startingPoint[0].trim()),Integer.parseInt(startingPoint[1].trim()));
                    System.out.println();
                    System.out.println("**********************************************");
                    System.out.println();
                counter = 0;   
                }
                else {
                    String[] arr = new String[2];
                    arr = calculate(line);
                    obstacles.add(arr);

                }

            }
            counter++;
            }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
public static void display(char[][] mat) {
    for(int i=1;i<=sizeOfRow;i++) {
        for(int j=1;j<=sizeOfCol;j++) {
            System.out.print(mat[i][j]+" ");
        }
        System.out.println();
    }
}
public static char[][] findPath(int i,int j) {
    System.out.println("Vehicle started moving");
    System.out.println();
    int counter = 0; int flag = 0; int startingPointI = i; boolean flag1 = false; boolean flagEnd = false;
    int startingPointJ = j;

        outer: while(i <= (Integer.parseInt(endPoint[0].trim())) && j <= (Integer.parseInt(endPoint[1].trim())) ) 
        {
            startingPointI = i;
            startingPointJ = j;
        for(;j<sizeOfCol;j++) {

            if(matrix[i][j+1] == 'E')
            {
                flagEnd = true;
                matrix[i][j+1] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                break outer;
            }
            else if(matrix[i][j+1] != 'O') {
                matrix[i][j+1] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                counter++;

            }
            else 
                break;
        }

        for(;i<sizeOfRow;i++) {
            if(matrix[i+1][j] == 'E') {
                flagEnd = true;

                matrix[i+1][j] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                break outer;
            }
            else if(matrix[i+1][j] != 'O') {
                matrix[i+1][j] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                counter++;

            }

            else
                break;
        }

        for(;j>1;j--) {
            if(matrix[i][j-1] == 'E') {
                flagEnd = true;

                matrix[i][j-1] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                break outer;
            }
            else if(matrix[i][j-1] != 'O') {
                matrix[i][j-1] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                counter++;

            }
            else
                break outer;
        }

        for(;i>startingPointI;i--) {

            if(matrix[i-1][j] == 'E') {
                flagEnd = true;

                matrix[i-1][j] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                break outer;
            }
            else if(matrix[i-1][j] != 'O') {
                matrix[i-1][j] = 'S';
                matrix[i][j] = 'W';
                display(matrix);
                System.out.println();
                counter++;

            }

            else 
                break;
        }
        if(startingPointI == i && startingPointJ == j) {
            if(matrix[i+1][j] != 'O') {

                //add check for O
                matrix[i+1][j] = 'S';
                matrix[i][j] = 'W';
                startingPointI++;
                i++;
                display(matrix);
                System.out.println();
                if(flag == 2)
                    {break outer;
                    }flag++;
            }
            else {
                break outer;
            }

        }

        /*}*/
        }
        if(flagEnd == true) {
            System.out.println("The vehicle has arrived to the destination");
            System.out.println("The number of steps taken to arrive at the destination is: "+counter);

        }
        else {
            System.out.println("The vehicle has taken "+counter+" steps. But there was no route to reach destination.");
            System.out.println("The number of steps taken to arrive at the destination is: "+counter);

        }
        return matrix;
        }
/*public static void find()
{

    int currentCol=1,currentRow=1,stoppedCol = 1,stoppedRow = 1;

    while(currentRow<=3 && currentCol <=5)
        {
        System.out.println("inside while");
            for(int j=currentCol;j<5;j++)
            {
                System.out.println("inside first for loop");
                if(matrix[currentRow][j+1] != 'O') {
                    matrix[currentRow][j+1] = 'S';
                matrix[currentRow][j] = 'W';
                stoppedCol = j;
                }
                else {
                     display(matrix);
                    break;
                }

            }
            currentCol = stoppedCol;
            currentCol++;
            for(int i=currentRow;i<6;i++)
            {
                System.out.println("inside second for loop "+currentCol);
                if(matrix[i+1][currentCol] != 'O') {
                    matrix[i+1][currentCol] = 'S';
                matrix[i][currentCol] = 'W';
                stoppedRow = i;
                }
                else {
                break;
                }

            }
            currentRow = stoppedRow;
            for(int j=currentCol;j>1;j--)
            {
                if(matrix[currentRow][j-1] != 'O') {
                    matrix[currentRow][j-1] = 'S';
                matrix[currentRow][j] = 'W';
                stoppedCol = j;
                }
                else {
                    break;
                }
            }
            currentCol = stoppedCol;

            for(int i=currentRow;i>1;i--)
            {
                if(matrix[i-1][currentCol] != 'O') {
                    matrix[i-1][currentCol] = 'S';
                matrix[i][currentCol] = 'W';
                stoppedRow = i;
                }
                else {
                    break;
                }
            }
            currentRow = stoppedRow;

         c1++;
         c2--;
         r1++;
         r2--;
        }

     Printing the Circular matrix 
    System.out.println("The Circular Matrix is:");
    for(int i=1;i<=6;i++)
        {
            for(int j=1;j<=5;j++)
                {
                    System.out.print(matrix[i][j]+ "\t");
                }
         System.out.println();
        }}
*/

/*
public static void swap(char[][] matrix) {
    char temp = matrix[];
    one = two;
    two = temp;

}*/
/*public static char[][] writeToFile(int m,int n) {

    if((n)==5 || (n)==1) {

        return matrix;
    }
    else {
        if(matrix[m][n+1] == 'O') 
        {
            matrix[m][n+1] = 'S';
        matrix[m][n] = 'W';
        n++;
        display(matrix);
        System.out.println();
        }
        else {
            matrix[m][n-1] = 'S';
            matrix[m][n] = 'W';
            n--;
        }

        if(i<6 && matrix[i+1][j] != 'O') {
            matrix[i+1][j] = 'S';
        matrix[i][j] = 'W';
        display(matrix);
        System.out.println();
        }

        if(j>1 && matrix[i][j-1] != 'O') {
            matrix[i][j-1] = 'S';
        matrix[i][j] = 'W';
        display(matrix);
        System.out.println();
        }

        if(i>1 && matrix[i-1][j] != 'O') {
            matrix[i-1][j] = 'S';
        matrix[i][j] = 'W';
        display(matrix);
        System.out.println();
        }

        return writeToFile(m,n);
    }
}
*/
/*
public static void swap(char[][] matrix) {
    char temp = matrix[];
    one = two;
    two = temp;

}*/
/*public static char[][] writeToFileReverse() {
    if(j == 1 || matrix[i][j-1] == 'O') {

        return matrix;
    }
    else {

            matrix[i][j-1] = 'S';
        matrix[i][j] = 'W';

        display(matrix);
        System.out.println();
        j++;

        if(i<6 && matrix[i+1][j] != 'O') {
            matrix[i+1][j] = 'S';
        matrix[i][j] = 'W';
        display(matrix);
        System.out.println();
        }

        if(j>1 && matrix[i][j-1] != 'O') {
            matrix[i][j-1] = 'S';
        matrix[i][j] = 'W';
        display(matrix);
        System.out.println();
        }

        if(i>1 && matrix[i-1][j] != 'O') {
            matrix[i-1][j] = 'S';
        matrix[i][j] = 'W';
        display(matrix);
        System.out.println();
        }

        return writeToFile();
    }*/
}

Recommended Answers

All 2 Replies

I call this work "Transcoding." That is I usually have to hand convert from one language to another and to do so I have understand enough of each language to pull it off. I rarely see anyone offer to do this on an entire app so be sure to slim down your request to one issue you are having.

That out of the way did you try "j2c"?

As far as I can see that program has nothing specially "Java" about. It's a long time since I've seen a whole Java program entirely in static methods; it's even got statement labels! In particular it makes zero use of O.O.,
So translating into almost any other procedural language is just a matter of working through it one line at a time transliterating each in turn. Tedious, yes. Difficult, no.

commented: Nice to read about literation rather than littering. +12
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.