In: Computer Science
My java program will not compile. I receive the following error;
Test.java:9: error: incompatible types: int[] cannot be
converted to int
int size = new int [start];
//Java Program
import java.util.Scanner;
public class Test{
public static void main(String []args)
{
int start, num;
System.out.println("Enter the number of elements in a array :
");
start = STDIN_SCANNER.nextInt();
int size = new int [start];
System.out.println("Enter the elements of array where last element
must be '0' : ");
for(int i = 0; i < start; i++) {
size = STDIN_SCANNER.nextInt();
}
size = 0;
System.out.print("Enter position of a marker where starting index
is '0' : ");
num = STDIN_SCANNER.nextInt();
int p = checkMove(start, num, size);
if(p == 1) {
System.out.print("\nTrue");
} else {
System.out.print("\nFalse");
}
//System.out.println(Solvable(0,new
int[]{3,6,4,1,3,4,2,5,3,0}));
//System.out.println(Solvable(0,new int[]{3,1,2,3,0}));
}
public static int checkMove(int start,int num,int size){
if(start-num>=0&&start+num<=size)
{
return 3;
}
if(start-num>=0)
{
return 1;
}
if(start+num<size)
{
return 2;
}
return 0;
}
public static boolean checkSolve(int start,int[] squares,int[]
visited)
{
visited[start]=1;
if(start==squares.length-1)
return true;
int num=squares[start];
int p=checkMove(start,squares[start],squares.length-1);
boolean b1=false;
if(p>=2&&visited[start+num]==0)
{
b1=checkSolve(start+squares[start],squares,visited);
}
if(p==1&&visited[start-num]==0)
{
b1=b1||checkSolve(start-squares[start],squares,visited);
}
return b1;
}
public static boolean Solvable(int start,int[] squares)
{
int[] visited=new int[squares.length];
return checkSolve(start,squares,visited);
}
public final static Scanner STDIN_SCANNER = new
Scanner(System.in);
}
CODE:
import java.util.Scanner; public class Test{ public static void main(String []args) { int start, num; System.out.println("Enter the number of elements in a array : "); start = STDIN_SCANNER.nextInt(); //there was a syntax error on this line //Array declarations follow the following format: // int[] variable = new int[start]; int[] size = new int [start]; System.out.println("Enter the elements of array where last element must be '0' : "); for(int i = 0; i < start; i++) { //there was a Syntax Error on this line, when you want to enter elements in the array //each element in the array is accessed using the index notation size[i] //size[i] returns the address of the ith element in the array size[i] = STDIN_SCANNER.nextInt(); } //there was a syntax error here, variable size was already used as an array //it could not be used again as an integer, so the variable name is changed int size1 = 0; System.out.print("Enter position of a marker where starting index is '0' : "); num = STDIN_SCANNER.nextInt(); int p = checkMove(start, num, size1); if(p == 1) { System.out.print("\nTrue"); } else { System.out.print("\nFalse"); } //System.out.println(Solvable(0,new int[]{3,6,4,1,3,4,2,5,3,0})); //System.out.println(Solvable(0,new int[]{3,1,2,3,0})); } //other lines in the code did not have an syntax error public static int checkMove(int start,int num,int size){ if(start-num>=0&&start+num<=size) { return 3; } if(start-num>=0) { return 1; } if(start+num<size) { return 2; } return 0; } public static boolean checkSolve(int start,int[] squares,int[] visited) { visited[start]=1; if(start==squares.length-1) return true; int num= squares[start]; int p=checkMove(start,squares[start],squares.length-1); boolean b1=false; if(p>=2&&visited[start+num]==0) { b1=checkSolve(start+squares[start],squares,visited); } if(p==1&&visited[start-num]==0) { b1=b1||checkSolve(start-squares[start],squares,visited); } return b1; } public static boolean Solvable(int start,int[] squares) { int[] visited=new int[squares.length]; return checkSolve(start,squares,visited); } public final static Scanner STDIN_SCANNER = new Scanner(System.in); }
______________________________________________
CODE IMAGES:
_____________________________________________________
OUTPUT:
____________________________________________________
Feel free to ask any questions in the comments section
Thank You!