Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code is not working with inputs like a7 and b 6. It works with corner adjac

ID: 3745125 • Letter: T

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 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<str.length(); 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<str.length(); 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<str.length(); 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<str.length(); 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<str.length(); 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<str.length(); 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<str.length(); 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<str.length(); 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<str.length(); 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<response.length(); 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<response.length(); 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(" ");
}
}

Explanation / Answer

I have answered this question before. Please go ahead and use this code, it works for these cases too.All input formats have been handled and output formats have been used accordingly.

import java.util.*;

import java.lang.*;

import java.io.*;

class Ideone

{

public static int[][] hashBoard = new int[9][9];

public static int format=0;

public static void printCoord(int x, int y)

{

char c = (char)(x+97);

switch(format)

{

case 1:

System.out.println("("+x+", "+y+")");

break;

case 2:

System.out.println(x+" "+y);

break;

case 3:

System.out.println(x+","+y);

break;

case 4:

System.out.println(c+""+y);

break;

case 5:

System.out.println(c+" "+y);

break;

}

}

public static void printCornerAdjacent(int x, int y)

{

System.out.println("Corner Adjacent Points :");

int nextX, nextY;

//Left Top corner

nextX=x-1; nextY=y-1;

if(nextX>=0 && nextY>=0){

printCoord(nextX, nextY);

hashBoard[nextX][nextY]=1;

}

//Right Top corner

nextX=x+1; nextY=y-1;

if(nextX<9 && nextY>=0){

printCoord(nextX, nextY);

hashBoard[nextX][nextY]=1;

}

//Left Down corner

nextX=x-1; nextY=y+1;

if(nextX>=0 && nextY<9){

printCoord(nextX, nextY);

hashBoard[nextX][nextY]=1;

}

//Right Down corner

nextX=x+1; nextY=y+1;

if(nextX<9 && nextY<9){

printCoord(nextX, nextY);

hashBoard[nextX][nextY]=1;

}

}

public static void printEdgeAdjacent(int x, int y)

{

System.out.println("Edge Adjacent Points :");

int nextX, nextY;

//Left

nextX=x-1; nextY=y;

if(nextX>=0){

printCoord(nextX, nextY);

hashBoard[nextX][nextY]=1;

}

//Right

nextX=x+1; nextY=y;

if(nextX<9){

printCoord(nextX, nextY);

hashBoard[nextX][nextY]=1;

}

//Top

nextX=x; nextY=y-1;

if(nextY>=0){

printCoord(nextX, nextY);

hashBoard[nextX][nextY]=1;

}

//Down

nextX=x; nextY=y+1;

if(nextY<9){

printCoord(nextX, nextY);

hashBoard[nextX][nextY]=1;

}

}

public static void printNonAdjacent()

{

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)

printCoord(i, j);

}

}

}

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( "Type some data for the program: " );

String s = scanner.nextLine();

int x=-1, y=-1; //x->row and t->column

for(int i=0; i<s.length(); i++) {

if(s.charAt(i) == '(')

{

format = 1;

continue;

}

else if(s.charAt(i) == ')')

{

if(format==1)

continue;

else

{

System.out.println("Input format not supported!!!");

System.exit(0);

}

}

else if(s.charAt(i) == ',')

{

if(format == 0)

format = 3;

continue;

}

else if(s.charAt(i) == ' ')

continue;

else if(s.charAt(i)>='a' && s.charAt(i)<='z')

{

if(format == 0 && s.charAt(i+1)==' ')

format = 5;

else

format = 4;

int temp = s.charAt(i);

if(x<0)

x=temp-97;

else

y=temp-97;

}

else if(s.charAt(i)>='0' && s.charAt(i)<='8')

{

int temp = s.charAt(i);

if(x<0)

x=temp-48;

else

y=temp-48;

}

else

{

System.out.println("Input format not supported!!!");

System.exit(0);

}

}

if(format == 0)

format = 2;

printCornerAdjacent(x, y);

printEdgeAdjacent(x, y);

printNonAdjacent();

}

}