Question: This code is not working with inputs like a7 and b 6. It works with corner adjacent just not edge adjacen. It is supposed to
This code is not working with inputs like a7 and b 6. It works with corner adjacent just not edge adjacen. It is supposed to handle inputs that are in these formats: (5, 5), 3 0, 4,1, a7, b 2 andd also output the same format. It works for everything except a7 and b 2 format with the edge adjacent and not adjacent
import java.util.*; import java.lang.*; import java.io.*;
class Ideone{
public static int[][] hashBoard = new int[9][9];
public static void printCornerAdjacent(String str,int x, int y){ System.out.println("Corner Adjacent Points :"); int nextX, nextY,t=0; //Left Top corner nextX=x-1; nextY=y-1; if(nextX>=0 && nextY>=0){ for(int i=0; i if(str.charAt(i)>='0' && str.charAt(i)<='8'){ if(t==0){ System.out.print(nextX); t=1; } else{ System.out.print(nextY); t=0; } } else System.out.print(str.charAt(i)); hashBoard[nextX][nextY]=1; } System.out.print(" "); } //Right Top corner nextX=x+1; nextY=y-1; if(nextX<9 && nexty>=0){ for(int i=0; i if(str.charAt(i)>='0' && str.charAt(i)<='8'){ if(t==0){ System.out.print(nextX); t=1; } else{ System.out.print(nextY); t=0; } } else System.out.print(str.charAt(i)); hashBoard[nextX][nextY]=1; } System.out.print(" "); } //Left Down corner nextX=x-1; nextY=y+1; if(nextX>=0 && nextY<9){ for(int i=0; i if(str.charAt(i)>='0' && str.charAt(i)<='8'){ if(t==0){ System.out.print(nextX); t=1; } else{ System.out.print(nextY); t=0; } } else System.out.print(str.charAt(i)); hashBoard[nextX][nextY]=1; } System.out.print(" "); } //Right Down corner nextX=x+1; nextY=y+1; if(nextX<9 && nextY<9){ for(int i=0; i if(str.charAt(i)>='0' && str.charAt(i)<='8'){ if(t==0){ System.out.print(nextX); t=1; } else{ System.out.print(nextY); t=0; } } else System.out.print(str.charAt(i)); hashBoard[nextX][nextY]=1; } System.out.print(" "); } } public static void printEdgeAdjacent(String str,int x, int y){ System.out.println("Edge Adjacent Points :"); int nextX, nextY,t=0; //Left nextX=x-1; nextY=y; if(nextX>=0){ for(int i=0; i if(str.charAt(i)>='0' && str.charAt(i)<='8'){ if(t==0){ System.out.print(nextX); t=1; } else{ System.out.print(nextY); t=0; } } else System.out.print(str.charAt(i)); hashBoard[nextX][nextY]=1; } System.out.print(" "); } //Right nextX=x+1; nextY=y; if(nextX<9){ for(int i=0; i if(str.charAt(i)>='0' && str.charAt(i)<='8'){ if(t==0){ System.out.print(nextX); t=1; } else{ System.out.print(nextY); t=0; } } else System.out.print(str.charAt(i)); hashBoard[nextX][nextY]=1; } System.out.print(" "); } //Top nextX=x; nextY=y-1; if(nextY>=0){ for(int i=0; i if(str.charAt(i)>='0' && str.charAt(i)<='8'){ if(t==0){ System.out.print(nextX); t=1; } else{ System.out.print(nextY); t=0; } } else System.out.print(str.charAt(i)); hashBoard[nextX][nextY]=1; } System.out.print(" "); } //Down nextX=x; nextY=y+1; if(nextY<9){ for(int i=0; i if(str.charAt(i)>='0' && str.charAt(i)<='8'){ if(t==0){ System.out.print(nextX); t=1; } else{ System.out.print(nextY); t=0; } } else System.out.print(str.charAt(i)); hashBoard[nextX][nextY]=1; } System.out.print(" "); } } public static void printNonAdjacent(String str){ int t=0; System.out.println("Non Adjacent Points :"); for(int i=0; i<9; i++){ for(int j=0; j<9; j++){ if(hashBoard[i][j]==0){ for(int n=0; n if(str.charAt(n)>='0' && str.charAt(n)<='8'){ if(t==0){ System.out.print(i); t=1; } else{ System.out.print(j); t=0; } } else System.out.print(str.charAt(n)); } System.out.print(" "); } } } } public static void main (String[] args) throws java.lang.Exception{ for(int i=0; i<9; i++){ for(int j=0; j<9; j++) hashBoard[i][j]=0; } Scanner scanner = new Scanner( System.in ); System.out.print( "please enter your coordinates: " ); String response = scanner.nextLine(); int x=-1, y=-1,t=0; //x->row and t->column //here get the user input as string for(int i=0; i if(response.charAt(i)>='0' && response.charAt(i)<='8'){ //if input char is a no, then first no taken as x int temp = response.charAt(i); if(x<0) x=temp-48; else y=temp-48; //second char is taken as y } //other char are takes as it else if((response.charAt(i) == '(')||(response.charAt(i) == ')')||(response.charAt(i) == ',')||(response.charAt(i) == ' ')|| (response.charAt(i)>='a' && response.charAt(i)<='z')){} //else format not supported else{ System.out.println("Input format not supported!!!"); System.exit(0); } } printCornerAdjacent(response,x, y); printEdgeAdjacent(response,x, y); printNonAdjacent(response); //print as given format for(int i=0; i if(response.charAt(i)>='0' && response.charAt(i)<='8'){ //if it no, then if(t==0){ System.out.print(x); //x is printed t=1; } else{ System.out.print(y); //second y is printed t=0; } } else System.out.print(response.charAt(i)); //else format is printed hashBoard[x][y]=1; } System.out.print(" "); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
