Finding Uniqueness (duplicates) - java
Im having trouble with my isUnique method. Either I'm making a logical error or a syntax error, or both. I have to make sure that every user input I get is Unique. Everything else I have done is correct but that method. I was using the debugger and I've noticed that the "number" doesn't change as the user input changes but I am new and kinda lost. Assigment: Enter 5 numbers and test for validity and uniqueness. If not valid do not count towards 5 numbers. If not unique count towards 5 numbers but count number of unique and print "not unique" if not unique.
import java.util.Scanner;
public class Assignment4Part2 {
public static void main(String[] args) {
int[] numbers = new int[5];
System.out.println("Enter an integer (50-100): ");
Scanner input = new Scanner(System.in);
int uniqueCount = 0;
for (int i = 0; i < numbers.length;) {
{
numbers[i] = input.nextInt();
if (isValid(numbers[i]) == true) {
i++;
if (isUnique(numbers, numbers[i]) == true) {
uniqueCount++;
System.out.printf("Unique so far: %d ", uniqueCount);
}
if (isUnique(numbers, numbers[i]) == false) {
System.out.println("That's not unique.\n");
}
}
}
}
for (int i = 0; i < numbers.length; i++)
{
System.out.print(numbers[i] + " ");
}
}
public static boolean isValid(int array) {
if (array <= 100 & array >= 50) {
return true;
} else {
System.out.println(" ***Invalid Number\n");
return false;
}
}
public static boolean isUnique(int[] array, int numbers) {
for (int i = 0; i < array.length; i++) {
if (array[i] == numbers) {
return false;
} else {
return true;
}
}
return false;
}
}
some things i notice:
for (int i = 0; i < numbers.length;) {
{
numbers[i] = input.nextInt();
if (isValid(numbers[i]) == true) {
//i++; // in case you increment here, you do it before is unqique is true... so next isUnique is of an emtpy (null) numbers[i]
if (isUnique(numbers, numbers[i]) == true) {
uniqueCount++;
System.out.printf("Unique so far: %d ", uniqueCount);
//continue here
i++;
}// why not an else?
//if (isUnique(numbers, numbers[i]) == false) {
else {
System.out.println("That's not unique.\n");
}
}
}
}
and the is unique....
public static boolean isUnique(int[] array, int numbers) {
// here the given number is already in the array, so never unique....
// this will always return false, because the input is already in the array
for (int i = 0; i < array.length; i++) {
if (array[i] == numbers) {
return false;
} else {
return true;
}
}
return false;
}
so use:
for (int i = 0; i < numbers.length;) {
{
// use a temp value before putting it in the array
int input = input.nextInt();
if (isValid(input) == true) {
//i++; // in case you increment here, you do it before is unqique is true... so next isUnique is of an emtpy (null) numbers[i]
if (isUnique(numbers, input) == true) {
uniqueCount++;
numbers[i] = input
System.out.printf("Unique so far: %d ", uniqueCount);
//continue here
i++;
}// why not an else?
//if (isUnique(numbers, numbers[i]) == false) {
else {
System.out.println("That's not unique.\n");
}
}
}
}
and in the is unique function....
if (array[i] == numbers) {
while not all numbers are filled in... this compares:
if(null == numbers){
so will be shorter to add in front:
if(array[i]==null){
break;
}
because the rest is still empty
The error is in the for in your method isUnique, during the first iteration you compare your the given number with the first value and if they dont match you already return true so the loop can't check any of the remaining numbers.
Just remove the else parte in that if and change the last return out of the loop to a true instead of false.
This way only until you checked the the whole array you will be sure the number is unique.
I would also sugest you send to that methis uniqueCount so you dont check the whole array, but just the amount of numbers already registered.
Related
Java maze won't print
I have to make a maze for a java assignment, and I was able to finish most of it. I was provided with an outline of the code that had all the methods. Can someone help me? My issue is that the maze wont print out, and I can't figure out why. package maze; public class Maze { private char direction; private int r; // x position of the mouse private int c; //y position of the mouse private boolean exitFound = false; public Maze(int[][] arrMaze) { this.r = arrMaze.length - 1; this.c = 0; } //Prints out the maze without solution public void displayMaze(int[][] arrMaze) { //display the maze putting blank spaces where there are 1's in the array and putting //another symbol where there are 0's to show the maze without the solution for(int i=0; i<arrMaze.length; i++){ System.out.println(" "); for(int j=0; j<arrMaze[i].length; j++){ if(arrMaze[i][j] == 0){ System.out.print("#"); } if(arrMaze[i][j] == 1) { System.out.print(" "); } if(arrMaze[i][j] == 2){ System.out.print("#"); } if(arrMaze[i][j] == 3){ System.out.println("~"); } } } } //displays the Maze with the path taken public void displayPath(int[][] arrMaze) { //show the user how far the mouse has gone since the start. //The path the mouse has gone will be filled in but the path ahead will not. for (int i = 0; i < arrMaze.length; i++) { System.out.println(" "); for (int j = 0; j < arrMaze[i].length; j++) { if (arrMaze[i][j] == 3) { System.out.print("#"); } else if (arrMaze[i][j] == 2) { System.out.print("~"); } else if (arrMaze[i][j] == 0) { System.out.print("#"); } else { } } } } public boolean takeStep(int[][] newMaze) { // moveNorth(newMaze) for (int i = 0; i < newMaze.length; i++) { System.out.println(" "); for (int j = 0; j < newMaze[i].length; j++) { if (newMaze[r][c] == 3) { moveNorth(newMaze); System.out.print("~"); } else if (newMaze[r][c] == 2) { System.out.print("#"); } else { } } } return isAnExit(newMaze); } public void moveNorth(int[][] arrMaze) { //complete the code here /*method will check for a 0 or a 1 in the position above the current position * and then if not a 0 will change the current position to the row above it, but in the same column. */ if (arrMaze[r][c - 1] != 0) { arrMaze[r][c - 1] = 3; arrMaze[r][c + 1] = 2; } else { moveSouth(arrMaze); } displayPath(arrMaze); } public void moveSouth(int[][] arrMaze) { //method will check for a 0 or a 1 in the position below the current position and then if not a 0 //it will change the current position to the row below it, but in the same column. if (arrMaze[r][c + 1] != 0) { arrMaze[r][c + 1] = 3; arrMaze[r][c + 1] = 2; } else { moveNorth(arrMaze); } displayPath(arrMaze); } public void moveEast(int[][] arrMaze) { //method will check for a 0 or a 1 in the position to the right of  the current position and then if //not a 0 will change the current position to the column to the right but the same row. if (arrMaze[r + 1][c] != 0) { arrMaze[r + 1][c] = 3; arrMaze[r - 1][c] = 2; } else { moveWest(arrMaze); } displayPath(arrMaze); } public void moveWest(int[][] arrMaze) { //method will check for a 0 or a 1 in the position to the left of  the current position and then if //not a 0 will change the current position to the column to the left but the same row. if (arrMaze[r - 1][c] != 0) { arrMaze[r - 1][c] = 3; arrMaze[r + 1][c] = 2; } else { } displayPath(arrMaze); } private boolean isAnExit(int[][] arrMaze) { //method will return true if the user arrives into the last column of the array because there is only one //location in the last column that is a 1, so if the user reaches the array[i].length then that means that it found an exit. if (arrMaze[r][c] > arrMaze.length) { exitFound = true; } else { exitFound = false; } return exitFound; } //finds the path without stopping at every step //method will show the complete path from start to finish of the maze and the suggested route to the end. public void findExit(int[][] arrMaze) { if (arrMaze[r][c] > arrMaze.length) { for (int i = 0; i < arrMaze.length; i++) { takeStep(arrMaze); } } } } This is the test code. I was provided the test code, and I haven't changed it. package maze; import java.util.Scanner; public class TestMaze { public static void main(String[] args) { int[][] mazeArray = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, {0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1}, {0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0}, {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; Maze myMaze = new Maze(mazeArray); boolean keepAsking = true; Scanner scan = new Scanner(System.in); String input = ""; myMaze.displayPath(mazeArray); System.out.println("Maze"); do { System.out.println("T = Take a step | S = Show path | Q = Quit"); System.out.print("Enter command: "); input = scan.nextLine(); input.trim(); input.toLowerCase(); if(input.equals("t")) { keepAsking = !myMaze.takeStep(mazeArray); System.out.println("Which direction would you like to go? N, S, E, W?"); String direction = scan.nextLine(); if(direction.equalsIgnoreCase("n")) myMaze.moveNorth(mazeArray); if(direction.equalsIgnoreCase("s")) myMaze.moveSouth(mazeArray); if(direction.equalsIgnoreCase("e")) myMaze.moveEast(mazeArray); if(direction.equalsIgnoreCase("w")) myMaze.moveWest(mazeArray); } else if(input.equals("s")) { myMaze.findExit(mazeArray); keepAsking = false; } else if(input.equals("q")) { keepAsking = false; } else { System.out.println("ERR: Invalid input"); } } while(keepAsking); System.out.println("Quitting program..."); scan.close(); } }
You need to call displayMaze() (in displayPath()) to print it. Currently, you're not calling the method, meaning that your code will never print the maze as it's no being instructed to. Also, where are you assigning values to r and c? I think you meant to use i and j in your displayPath() method ([i][j] instead of [r][c]).
I imagine you're throwing an error somewhere because your r and c values are undefined everywhere they are used. Try adding code to initialize and update these values, then you should see your maze print.
putting different value numbers into array, if the input number is already in array "Retry"
the following code can only request to retry if the consecutive input number is the same like 1, 1 or 2, 2. But it won't request to retry if the input is like 1, 2, 1 or 2, 1, 3, 2. import java.util.Scanner; public class exercise { public static void main(String[] args) { Scanner scan = new Scanner (System.in); int[] a = new int[5]; boolean found = false; int i = 1; System.out.println("Enter Number.\n>>>"); a[0] = scan.nextInt(); while(i<5) { System.out.println("Enter number.\n>>>"); int num = scan.nextInt(); for (int j=0; j<i; j++) // to determine same number is already in array or not { if(a[j] == num) { found = false; } else { found = true; } } if (!found) { System.out.println("Retry"); } else { a[i] = num; i++; } } for (int a1 : a) System.out.println(a1); } }
Once you find the element in the array, break the loop. You are iterating even after you find the element in the array. Your inner loop should be like this: for (int j=0; j<i; j++) { if(a[j] == num) { found = false; break; // break once you find the element. } else { found = true; } }
The problematic part inside your code is this for (int j=0; j<i; j++){ { if(a[j] == num){ found = false; } else { found = true; } } After noticing that the number is already contained in the array, you should immediately break the for loop. Please check also the logic in your code: if(a[j] == num){ found = false; } sounds strange... As you have found the element, I would have expected found = true; Anyway,I suggest to move this check to a separate method. You can write a method boolean contains(int value, int[] array) that consists of a loop iterating over the elements of the array. If one of the elements is equal to value, return true. The code could look like this: boolean contains(int value, int[] array) { for(int j=0; j<.array.length;j++){ if (value==array[j]){ return true; } } return false; }
If an array is not full add a value to the end
So i'm working on some code that is suppose to check if all my array slots are full and if there not add my new grade to the next slot in the array and return true and if it is full return false. I'm confused on how to check if my array(grades) is full or not.. I have something like this but not quite sure if its correct. The probelm is for some reason it seems to be only adding one grade. This is also effecting my score which is producing ? as a number which is something i have never seen before. public boolean addGrade(int newGrade) { for (int i = 0; i < grades.length; i++) { if (grades[i] == -1)//or should i use 0 { grades[i] = newGrade; numGrades++; return true; } } return false; } score method (all its suppose to do is compute and return the score: score total / totalGrades) public double computeScore() { double total = 0; for (int i = 0; i < grades.length; i++) { total += grades[i]; } return total / totalGrades; }
You could even tighten up a tad by incrementing inside of the array brackets: public boolean addGrade(int newGrade) { if (numGrades < grades.length) { grades[numGrades++] = newGrade; return true; } return false; }
since you are maintaining a counter variable numGrades hence use it directly to insert a new item at desired location. public boolean addGrade(int newGrade) { if(newGrades<grades.length){ grades[numGrades] = newGrade; numGrades++; return true; } return false; } Note: Each value in an array is by default zero.
No. You shouldn't do what you're doing. You have numGrades, I think you are should use it to track the correct position in addGrade()! if (numGrades < grades.length) { grades[numGrades] = newGrade; numGrades++; return true; } return false; Also then public double computeScore() { double total = 0; if (numGrades == 0) { return total; } if (numGrades > grades.length) { numGrades = grades.length; } for (int i = 0; i < numGrades; i++) { total += grades[i]; } return total / numGrades; }
Sorted / Not sorted - How do I print the answer to the console?
I've got a program that checks whether a list is sorted or not. How do I print the answer? (i.e "The list is sorted", "The list is not sorted"). public class CheckList { public static void main(String[] args) { int[] myList = new int[10]; // Read in ten numbers Scanner input = new Scanner(System.in); System.out.println("Enter ten numbers: "); for (int i = 0; i < myList.length; i++) { myList[i] = input.nextInt(); } } //Check if list is sorted public static boolean isSorted(int[] myList) { if (myList[0] > 1) { for (int i = 1; i < myList[0]; i++) if (myList[i] > myList[i + 1]) return false; } return true; } }
Just call the method inside a if: if(isSorted(myList)) { System.out.println("Array is sorted"); } else { System.out.println("Array is not sorted"); } Anyway, your isSorted method won't work, i would make something like this: //checks if array is sorted in ascending order public static boolean isSorted(int[] myList) { if(myList == null) return false; //just checking for (int i = 0; i < myList.length - 1; i++) { if (myList[i] > myList[i + 1]) { return false; } } return true; }
Just call the method after your for loop for (int i = 0; i < myList.length; i++) { myList[i] = input.nextInt(); } if(isSorted(myList)) { System.out.println("The list is sorted"); } else { System.out.println("The list is not sorted"); }
Can't Figure These Compiler Errors Out (Java) [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance. Closed 9 years ago. Improve this question Here's my whole code. I'm getting java syntax errors, duplicate methods, and I just can't seem to figure most of these out. Any help is appreciated. This is a program designed to simulate manipulating a lock. Also, first time poster here so if you need anything else from me please let me know. Thanks. EDIT: Added errors at the bottom. Also, I'm using DrJava.jar as my program to implement this. EDIT2: Updated revised code and current errors. import java.util.Scanner; public class Lock{ private static int first; private static int second; private static int third; private Boolean isClosed; // private static int[] dial = new int[40]; NO LONGER IMPLEMENTING ARRAY private static int activeNumber = 0; public int revoCount = 0; public int count; public int tempFirst; public int tempSecond; public int tempThird; private Boolean isClockwise; private int desiredNumber; Scanner in = new Scanner(System.in); /**for (int i = 0; i < 40; i++) { NO LONGER IMPLEMENTING ARRAY dial[i] = i; } **/ //***************************************************************************************************** public Lock() { first = 1; second = 2; third = 3; } //***************************************************************************************************** public void alterCombo(int x, int y, int z) { first = x; second = y; third = z; System.out.println("The correct combination needed has been changed to: " + first + ", " + second + ", " + third); return; } //***************************************************************************************************** public void turnLock() { System.out.print("This is a lock that goes from 0 to 39. You must turn the knob clockwise first, then counterclockwise twice, "); System.out.print("then clockwise for the final input. Specify how many revolutions you want (Positive number indicates "); System.out.println("COUNTER CLOCKWISE. Negative number indicates CLOCKWISE."); turnFirstNumber(); turnSecondNumber(); turnThirdNumber(); System.out.println("The combination you chose was: " + tempFirst + ", " + tempSecond + ", and " + tempThird + "."); return; } //***************************************************************************************************** public void closeLock() { if (isClosed) System.out.println("Lock is already closed."); else { System.out.println("Lock has been closed."); isClosed = true; } return; } //***************************************************************************************************** public void openLock() { if ((turnFirstNumber()) && (turnSecondNumber()) && (turnThirdNumber()) && (isClosed)) //if all 3 passed and lock is not open already { isClosed = false; System.out.println("Your combination is correct and the lock has been opened."); return; } else if (!isClosed) //lock's already open { System.out.println("The lock is already open."); return; } else if ((!turnFirstNumber()) && (turnSecondNumber()) && (turnThirdNumber())) //first wrong { System.out.println("The first number you input is incorrect."); return; } else if ((!turnFirstNumber()) && (!turnSecondNumber()) && (turnThirdNumber())) //first and second wrong { System.out.println("The first 2 numbers you input are incorrect."); return; } else if ((!turnFirstNumber()) && (turnSecondNumber()) && (!turnThirdNumber())) //first and third wrong { System.out.println("The first and last numbers you input are incorrect."); return; } else if ((turnFirstNumber()) && (turnSecondNumber()) && (!turnThirdNumber())) //third wrong { System.out.println("The last number you input is incorrect."); return; } else if ((turnFirstNumber()) && (!turnSecondNumber()) && (!turnThirdNumber())) //second and third wrong { System.out.println("The second and last numbers you input are incorrect."); return; } else if ((turnFirstNumber()) && (!turnSecondNumber()) && (turnThirdNumber())) //second is wrong { System.out.println("The second number you input is incorrect."); return; } else { System.out.println("Your entire combination is INCORRECT. Please try again."); //all wrong return; } } //***************************************************************************************************** public void lockStatus() { if (isClosed) { System.out.println("Closed"); return; } else { System.out.println("Open"); return; } } //***************************************************************************************************** public static int getActiveNumber() { return activeNumber; } //***************************************************************************************************** private boolean turnFirstNumber() { System.out.print("What is your desired direction and number of revolutions? (Positive number is counterclockwise, negative number is clockwise): "); count = in.nextInt(); if (count > 0) isClockwise = false; else if (count < 0) isClockwise = true; else { throw new InvalidInputException("Your desired direction of spinning the lock is invalid. Please choose a number other than 0."); return false; } System.out.print("\nWhat is your desired first number?: "); desiredNumber = in.nextInt(); if (!isClockwise) //user desires countercockwise revolution { do { for (int i = 0; i < (count * 40); i++) { activeNumber++; if (activeNumber > 39) activeNumber = 0; if (activeNumber == desiredNumber) revoCount++; } } while ((activeNumber != desiredNumber) && (revoCount < count)); } else if (isClockwise) //user desires clockwise revolution { do { for (int i = 0; i < (Math.abs(count) * 40); i++) { activeNumber--; if (activeNumber < 0) activeNumber = 39; if (activeNumber == desiredNumber) revoCount++; } } while ((activeNumber != desiredNumber) && (revoCount < Math.abs(count))); } tempFirst = activeNumber; if ((activeNumber == first) && (count < 0)) //if first number is correct and user picked correct orientation and revolutions return true; else return false; revoCount = 0; } //***************************************************************************************************** private boolean turnSecondNumber() { System.out.print("What is your desired direction and number of revolutions? (Positive number is counterclockwise, negative number is clockwise): "); count = in.nextInt(); if (count > 0) isClockwise = false; else if (count < 0) isClockwise = true; else { throw new InvalidInputException("Your desired direction of spinning the lock is invalid. Please choose a number other than 0."); return false; } System.out.print("\nWhat is your desired second number?: "); desiredNumber = in.nextInt(); if (!isClockwise) //user desires countercockwise revolution { do { for (int i = 0; i < (count * 40); i++) { activeNumber++; if (activeNumber > 39) activeNumber = 0; if (activeNumber == desiredNumber) revoCount++; } } while ((activeNumber != desiredNumber) && (revoCount < count)); } else if (isClockwise) //user desires clockwise revolution { do { for (int i = 0; i < (Math.abs(count) * 40); i++) { activeNumber--; if (activeNumber < 0) activeNumber = 39; if (activeNumber == desiredNumber) revoCount++; } } while ((activeNumber != desiredNumber) && (revoCount < Math.abs(count))); } tempSecond = activeNumber; if ((activeNumber == second) && ((count == "+2") || (count == "2"))) //if second number is correct and user picked correct orientation and revolutions return true; else return false; revoCount = 0; } //***************************************************************************************************** private boolean turnThirdNumber() { System.out.print("Enter '1' to twist the dial counterclockwise until you reach your desired number. Enter '-1' to twist the dial clockwise until you reach your desired number.: "); count = in.nextInt(); if ((count == "1") || (count == "+1")) isClockwise = false; else if (count == "-1") isClockwise = true; else { throw new OutOfBoundsException("You are not supposed to do a full revolution on the third number of the combination. Now you have to restart."); return false; } System.out.print("\nWhat is your desired third and final number?: "); activeNumber = in.nextInt(); activeNumber = Math.abs(activeNumber); tempThird = activeNumber; /** if (!isClockwise) //user desires countercockwise revolution { do { activeNumber++; if (activeNumber > 39) activeNumber = 0; } while (activeNumber != desiredNumber) } else if (isClockwise) //user desires clockwise revolution { do { for (int i = 0; i < (count * 40); i++) { activeNumber--; if (activeNumber < 0) activeNumber = 39; } while (activeNumber != desiredNumber) } REDUNDANT. Same result, more work.*/ if (activeNumber > 39) { throw new OutOfBoundsException("You desire a number that is not on the lock. The lock goes from 0 to 39. Try again."); return false; } if ((activeNumber == third) && (isClockwise)) //if third number is correct and user picked correct orientation and revolutions return true; else return false; } //***************************************************************************************************** } These are the errors: 9 errors found: File: C:\Users\Alex\Java Source Files\Lock.java [line: 165] Error: InvalidInputException cannot be resolved to a type File: C:\Users\Alex\Java Source Files\Lock.java [line: 219] Error: InvalidInputException cannot be resolved to a type File: C:\Users\Alex\Java Source Files\Lock.java [line: 254] Error: Incompatible operand types int and java.lang.String File: C:\Users\Alex\Java Source Files\Lock.java [line: 254] Error: Incompatible operand types int and java.lang.String File: C:\Users\Alex\Java Source Files\Lock.java [line: 267] Error: Incompatible operand types int and java.lang.String File: C:\Users\Alex\Java Source Files\Lock.java [line: 267] Error: Incompatible operand types int and java.lang.String File: C:\Users\Alex\Java Source Files\Lock.java [line: 269] Error: Incompatible operand types int and java.lang.String File: C:\Users\Alex\Java Source Files\Lock.java [line: 273] Error: OutOfBoundsException cannot be resolved to a type File: C:\Users\Alex\Java Source Files\Lock.java [line: 300] Error: OutOfBoundsException cannot be resolved to a type
Often you have to return a boolean (true or false), because you declared the method's return value as boolean, but you don't, so change it into void. Add this to your class block: public int revoCount; change line 266 et seq. to: if (count == 1) //there is no difference between int with the value 1 and +1 isClockwise = false; At last: remove the last return: class definitions don't end with return. This should remove the most errors, but you should take a look at how to define classes in Java.
Look at the information being shown in the compilation errors. For example These variable declarations in turnLock should be in the class block public boolean isClockwise = false; public int tempFirst = 0; public int tempSecond = 0; public int tempThird = 0; public int desiredNumber = 0; public int count = 0; revoCount is never declared private int revoCount; and you have a number of missing semi-colons on your do-while statements } while ((activeNumber != desiredNumber) && (revoCount < Math.abs(count))); add this ---------------------^ The compiler cannot locate the class InvalidInputException. Ensure it is on the compilation path. The same apply to OutOfBoundsException Remove any return statements that appear immediately after throw statements - these statements are unreachable, e.g. throw new InvalidInputException("Your desired direction of spinning the lock is invalid. Please choose a number other than 0."); return false;