public static void main(String[] args) { try { Scanner sc = new Scanner(new File(args[0])); int carPos = -1; boolean first = true; while (sc.hasNextLine()) { StringBuilder line = new StringBuilder(sc.nextLine()); if (first) { carPos = line.lastIndexOf("C"); if (carPos == -1) { carPos = line.lastIndexOf("_"); } line.setCharAt(carPos, '|'); first = false; } else { int cPos = line.lastIndexOf("C"); int gatePos = line.lastIndexOf("_"); if (cPos != -1) { moveCar(carPos, cPos, line); carPos = cPos; } else { moveCar(carPos, gatePos, line); carPos = gatePos; } } System.out.println(line); } sc.close(); } catch (FileNotFoundException e) { System.out.println("File not found!"); } }