Question

Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean...

Why am I getting compile errors.

rowPuzzle.java:9: error: class, interface, or enum expected
public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited)
^
rowPuzzle.java:16: error: class, interface, or enum expected
}

//Java Program
import java.util.*;

// rowPuzzle helper function implementation
// File: rowPuzzle.cpp

// Header files section

// function prototypes
public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited)
{
// base case
// return true if the puzzle is solvable
if (index == squares.size() - 1)
{
   return true;
}

// mark the visited square
visited.set(index, true);

// shift to left side square
int leftShift = index - squares.get(index);
if (leftShift >= 0 && !visited.get(leftShift))
{
   // recursive case
   if (rowPuzzle(new ArrayList<Integer>(squares), leftShift, new ArrayList<Boolean>(visited)))
   {
       return true;
   }
}

// shift to right side square
int rightShift = index + squares.get(index);
if (rightShift < squares.size() && !visited.get(rightShift))
{
   // recursive case
   if (rowPuzzle(new ArrayList<Integer>(squares), rightShift, new ArrayList<Boolean>(visited)))
   {
       return true;
   }
}

// return false if the puzzle is unsolvable
return false;
}

// rowPuzzle function implementation

public static boolean rowPuzzle(ArrayList<Integer> squares)
{
ArrayList<Boolean> visited = new ArrayList<Boolean>(squares.size());

return rowPuzzle(new ArrayList<Integer>(squares), 0, new ArrayList<Boolean>(visited));
}

// start main function
public static void main(String[] args)
{
ArrayList<Integer> squares = new ArrayList<Integer>();
int value;

System.out.print("Enter a series of non-negative integers, end with 0:");
System.out.print("\n");
value = Integer.parseInt(ConsoleInput.readToWhiteSpace(true));

while (value != 0)
{
   squares.add(value);

   value = Integer.parseInt(ConsoleInput.readToWhiteSpace(true));
}

// call the rowPuzzle function and print the results
if (rowPuzzle(new ArrayList<Integer>(squares)))
{
   System.out.print("\nThe puzzle is solvable.");
   System.out.print("\n");
}
else
{
   System.out.print("\nThe puzzle is unsolvable.");
   System.out.print("\n");
}

}

//Helper class added by C++ to Java Converter:

package tangible;

//----------------------------------------------------------------------------------------
//   Copyright © 2006 - 2020 Tangible Software Solutions, Inc.
//   This class can be used by anyone provided that the copyright notice remains intact.
//
//   This class provides the ability to convert basic C++ 'cin' behavior.
//----------------------------------------------------------------------------------------
public final class ConsoleInput
{
   private static boolean goodLastRead = false;
   public static boolean lastReadWasGood()
   {
       return goodLastRead;
   }

   public static String readToWhiteSpace(boolean skipLeadingWhiteSpace)
   {
       String input = "";
       char nextChar;
       while (Character.isWhitespace(nextChar = (char)System.in.read()))
       {
           //accumulate leading white space if skipLeadingWhiteSpace is false:
           if (!skipLeadingWhiteSpace)
           {
               input += nextChar;
           }
       }
       //the first non white space character:
       input += nextChar;

       //accumulate characters until white space is reached:
       while (!Character.isWhitespace(nextChar = (char)System.in.read()))
       {
           input += nextChar;
       }

       goodLastRead = input.length() > 0;
       return input;
   }

   public static String scanfRead()
   {
       return scanfRead(null, -1);
   }

   public static String scanfRead(String unwantedSequence)
   {
       return scanfRead(unwantedSequence, -1);
   }

   public static String scanfRead(String unwantedSequence, int maxFieldLength)
   {
       String input = "";

       char nextChar;
       if (unwantedSequence != null)
       {
           nextChar = '\0';
           for (int charIndex = 0; charIndex < unwantedSequence.length(); charIndex++)
           {
               if (Character.isWhitespace(unwantedSequence.charAt(charIndex)))
               {
                   //ignore all subsequent white space:
                   while (Character.isWhitespace(nextChar = (char)System.in.read()))
                   {
                   }
               }
               else
               {
                   //ensure each character matches the expected character in the sequence:
                   nextChar = (char)System.in.read();
                   if (nextChar != unwantedSequence.charAt(charIndex))
                       return null;
               }
           }

           input = (new Character(nextChar)).toString();
           if (maxFieldLength == 1)
               return input;
       }

       while (!Character.isWhitespace(nextChar = (char)System.in.read()))
       {
           input += nextChar;
           if (maxFieldLength == input.length())
               return input;
       }

       return input;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In the first code you haven't declare the code inside class so it will through error

rowPuzzle.java:9: error: class, interface, or enum expected
public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited)
^
rowPuzzle.java:16: error: class, interface, or enum expected
}

So, you need make some class I took the class name as Main class and done some modification

Code:

import java.io.IOException;
import java.util.ArrayList;

public class Main {

// rowPuzzle helper function implementation
// File: rowPuzzle.cpp

// Header files section

    // function prototypes
    public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited)
    {
// base case
// return true if the puzzle is solvable
        if (index == squares.size() - 1)
        {
            return true;
        }

// mark the visited square
        visited.set(index, true);

// shift to left side square
        int leftShift = index - squares.get(index);
        if (leftShift >= 0 && !visited.get(leftShift))
        {
            // recursive case
            if (rowPuzzle(new ArrayList<Integer>(squares), leftShift, new ArrayList<Boolean>(visited)))
            {
                return true;
            }
        }

// shift to right side square
        int rightShift = index + squares.get(index);
        if (rightShift < squares.size() && !visited.get(rightShift))
        {
            // recursive case
            if (rowPuzzle(new ArrayList<Integer>(squares), rightShift, new ArrayList<Boolean>(visited)))
            {
                return true;
            }
        }

// return false if the puzzle is unsolvable
        return false;
    }

// rowPuzzle function implementation

    public static boolean rowPuzzle(ArrayList<Integer> squares)
    {
        ArrayList<Boolean> visited = new ArrayList<Boolean>(squares.size());

        return rowPuzzle(new ArrayList<Integer>(squares), 0, new ArrayList<Boolean>(visited));
    }

    // start main function
    public static void main(String[] args) throws IOException {
        ArrayList<Integer> squares = new ArrayList<Integer>();
        int value;

        System.out.print("Enter a series of non-negative integers, end with 0:");
        System.out.print("\n");
        value = Integer.parseInt(ConsoleInput.readToWhiteSpace(true));

        while (value != 0)
        {
            squares.add(value);

            value = Integer.parseInt(ConsoleInput.readToWhiteSpace(true));
        }

// call the rowPuzzle function and print the results
        if (rowPuzzle(new ArrayList<Integer>(squares)))
        {
            System.out.print("\nThe puzzle is solvable.");
            System.out.print("\n");
        }
        else
        {
            System.out.print("\nThe puzzle is unsolvable.");
            System.out.print("\n");
        }

    }
}
import java.io.IOException;

public final class ConsoleInput
{
    private static boolean goodLastRead = false;
    public static boolean lastReadWasGood()
    {
        return goodLastRead;
    }

    public static String readToWhiteSpace(boolean skipLeadingWhiteSpace) throws IOException {
        String input = "";
        char nextChar;
        while (Character.isWhitespace(nextChar = (char)System.in.read()))
        {
            //accumulate leading white space if skipLeadingWhiteSpace is false:
            if (!skipLeadingWhiteSpace)
            {
                input += nextChar;
            }
        }
        //the first non white space character:
        input += nextChar;

        //accumulate characters until white space is reached:
        while (!Character.isWhitespace(nextChar = (char)System.in.read()))
        {
            input += nextChar;
        }

        goodLastRead = input.length() > 0;
        return input;
    }

    public static String scanfRead() throws IOException {
        return scanfRead(null, -1);
    }

    public static String scanfRead(String unwantedSequence) throws IOException {
        return scanfRead(unwantedSequence, -1);
    }

    public static String scanfRead(String unwantedSequence, int maxFieldLength) throws IOException {
        String input = "";

        char nextChar;
        if (unwantedSequence != null)
        {
            nextChar = '\0';
            for (int charIndex = 0; charIndex < unwantedSequence.length(); charIndex++)
            {
                if (Character.isWhitespace(unwantedSequence.charAt(charIndex)))
                {
                    //ignore all subsequent white space:
                    while (Character.isWhitespace(nextChar = (char)System.in.read()))
                    {
                    }
                }
                else
                {
                    //ensure each character matches the expected character in the sequence:
                    nextChar = (char)System.in.read();
                    if (nextChar != unwantedSequence.charAt(charIndex))
                        return null;
                }
            }

            input = (new Character(nextChar)).toString();
            if (maxFieldLength == 1)
                return input;
        }

        while (!Character.isWhitespace(nextChar = (char)System.in.read()))
        {
            input += nextChar;
            if (maxFieldLength == input.length())
                return input;
        }

        return input;
    }
}

You need to import the package in which which package you are implementing the both the class.

I have resolved the complier error.

If it is not working let me know. It is working fine in my IDE.

Add a comment
Know the answer?
Add Answer to:
Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a Graph.java which I need to complete four methods in the java file: completeGraph(),...

    I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

  • This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = '...

    This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = ' '; private static final char UPPER_BOUND = '_'; private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1; /** * This method determines if a string is within the allowable bounds of ASCII codes * according to the LOWER_BOUND and UPPER_BOUND characters * @param plainText a string to be encrypted, if it is within the allowable bounds * @return true if all characters...

  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console {     private Scanner sc;     boolean isValid;     int i;     double d;        public Console()     {         sc = new Scanner(System.in);     }     public String getString(String prompt)     {         System.out.print(prompt);         return sc.nextLine();...

  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...

  • Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int...

    Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int col) This method does the following: compare the first character from word to the character in puzzle[row][col] if they match subtract one from row (to check the previous character in the same column) and continue comparing characters, by advancing to the next character in word else if puzzle[row][col] is NOT part of puzzle array or the character in the cell does not match the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT