Question

In: Computer Science

My java program will not compile. I receive the following error; Test.java:9: error: incompatible types: int[]...

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);

}

Solutions

Expert Solution

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!


Related Solutions

What kind of error is incompatible types? Compilation error, runtime error, or semantic error? Why does...
What kind of error is incompatible types? Compilation error, runtime error, or semantic error? Why does incompatible types error happen? How would you fix this error using wildcards? Language Java. incompatible types: List<String> cannot be converted to List<Object>
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I...
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I want to be able to scan and output which number appears the most and the least. int x =1000 int[] array = new array[x] for(int i = 0 ; i < x; i++){ array[i] = random.nextInt(101); }
I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
How do I fix the "error: bad operand types for binary operator '*' " in my...
How do I fix the "error: bad operand types for binary operator '*' " in my code? What I am trying to do: double TotalPrice = TicketPrice * NoOfTickets;       My code: import javax.swing.*; /*provides interfaces and classes for different events by AWT components*/ import java.awt.event.*; import javax.swing.JOptionPane; //TicketReservation.java class TicketReservation { public static void main(String args[]) { /*Declare JFrame for place controls.*/ JFrame f= new JFrame("Movie Ticket Reservation");                                   /*Declare JLabels*/ JLabel...
My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number;...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number; Scanf (“%d”, & number); if (number % 2 ==0) { printf (“Even\n”); } else { printf(“Odd\n”); } Return 0; }
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
Write a java program calls the following methods: a. printStars(): Takes an int (n) as parameter...
Write a java program calls the following methods: a. printStars(): Takes an int (n) as parameter and prints n stars (*) using for loop. ex. 6 ******
int main() { system("ls"); return 0; } Please compile the above program, and change its owner...
int main() { system("ls"); return 0; } Please compile the above program, and change its owner to root, and make it a Set-UID program. Can you let this Set-UID program run your code instead of /bin/ls? If you can, is your code running with the root privilege? Describe and explain your observations.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT