Can't Figure These Compiler Errors Out (Java) [closed] - java

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;

Related

Binary search algorithm isn't returning variable

I'm very new to binary search and I attempted a code that would read values from a document and then the user can input a number to search for from the document, and through binary search, the number would be found. I'm having trouble now because the "low" variable that I initialize in the binary search section of my code is not being returned to my main code and there's an error that says "low can not be resolved to a variable".
Here is the code for my binary search:
static public int search (int[]numbers,int target, int count)
{
int high = numbers.length;
int low = -1;
int middle = (high+low)/2;
while(high-low>1)
{
count++;
middle = (high+low)/2;
if(numbers[middle]>target)
{
high = middle;
}
else if(numbers[middle]<target)
{
low = middle;
}
else
{
break;
}
System.out.println(numbers[middle]);
System.out.println(middle);
}
if(low == -1 || numbers[low]!=target)
{
low=-1;
return low;
}
else
{
return low;
}
}
And here is the code from my main code. The part with the if statements is where the error is showing up:
public static void main(String[] args) throws IOException {
DataInputStream input = new DataInputStream(System.in);
int [] numbers = new int [50000];
int target;
int count=0;
try
{
BufferedReader br = new BufferedReader(new FileReader("randNums.txt"));
for(int i=0;i<50000;i++)
{
numbers[i]=Integer.parseInt(br.readLine());
}
br.close();
Arrays.sort(numbers);
System.out.print("Choose a number between 1-100000000 to search for: ");
target = Integer.parseInt(input.readLine());
search(numbers, target,count);
if(low==-1)
{
System.out.println("The number was not on the list.");
}
else
{
System.out.println("The number is at position " + low);
System.out.println("It took " + count + " comparisons to find the number.");
}
}
You have to initialize low in main:
int low=search(numbers, target,count);
I have Already resolved this algorithm.
Try my code :
public static int guessNumber(int number) {
int first = 0;
int last = 1_000_000;
if (verify(first) == 0) {
count++;
return first;
}
if (verify(last) == 0) {
count++;
return last;
}
while (last > first && count <= 50) {
count += 1;
// get the middle of the range
int middle = (first + last) / 2;
if (verify(middle) == 0) {
return middle;
}
if (verify(middle) == 1) {
first = middle + 1;
if (verify(first) == 0) {
return first;
}
}else {
last = middle - 1;
if (verify(last) == 0)
return last;
}
}
return 0;
}
//Function verify(integer) => integer
public static int verify(int guess){
if (numberTobeGuessed > guess ) {
return 1;
}else if (numberTobeGuessed < guess) {
return -1;
}
return 0;
}
I recently found a solution for lazy peoples like me use below code
int position = Arrays.binarySearch(numbers , target);
here no need to sort, and array variable number integer variable target.

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.

Finding Uniqueness (duplicates)

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.

Netbeans is giving me "illegal start of expression" error message on my methods [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
All of my methods after the main are getting "illegal start of expression" error messages and I can't figure out why. Here is my code:
import java.util.Scanner;
public class CreditCardCounter {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// This program allows the user to enter multiple credit card numbers, then
//tallys and displays the number of each different brand of credit card entered.
// Create a new scanner.
Scanner input = new Scanner(System.in);
System.out.print("Enter your Card Number : ");
long input = sc.nextLong();
if (isValid(input) == true) {
System.out.println("\n*****Your card is Valid*****");
} else {
System.out.println("\n!!!!Your Card is not Valid !!!!! ");
}
public static boolean isValid(long number) {
int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
if ((total % 10 == 0) && (prefixMatched(number, 1) == true) && (getSize(number)>=13 ) && (getSize(number)<=16 )) {
return true;
} else {
return false;
}
}
public static int getDigit(int number) {
if (number <= 9) {
return number;
} else {
int firstDigit = number % 10;
int secondDigit = (int) (number / 10);
return firstDigit + secondDigit;
}
}
public static int sumOfOddPlace(long number) {
int result = 0;
while (number > 0) {
result += (int) (number % 10);
number = number / 100;
}
return result;
}
public static int sumOfDoubleEvenPlace(long number) {
int result = 0;
long temp = 0;
while (number > 0) {
temp = number % 100;
result += getDigit((int) (temp / 10) * 2);
number = number / 100;
}
return result;
}
public static boolean prefixMatched(long number, int d) {
if ((getPrefix(number, d) == 4)
|| (getPrefix(number, d) == 5)
|| (getPrefix(number, d) == 3)) {
if (getPrefix(number, d) == 3) {
System.out.println("\nVisa Card ");
} else if (getPrefix(number, d) == 5) {
System.out.println("\nMaster Card ");
} else if (getPrefix(number, d) == 3) {
System.out.println("\nAmerican Express Card ");
}
return true;
} else {
return false;
}
}
public static int getSize(long d) {
int count = 0;
while (d > 0) {
d = d / 10;
count++;
}
return count;
}
public static long getPrefix(long number, int k) {
if (getSize(number) < k) {
return number;
} else {
int size = (int) getSize(number);
for (int i = 0; i < (size - k); i++) {
number = number / 10;
}
return number;
}
}
}
You have a two problems that are preventing you from building.
First, input is defined as both a Scanner and a long. You can't have the same name for two different things like that. Based on the line where you initialize the long input, I'm guessing your Scanner is supposed to be named sc.
Next, you don't have a closing brace for your main method.
Fixing these two errors appears to remove the build errors. You'd still need to text your logic, however.
Seems that you forgot a curly bracket:
...
if (isValid(input) == true) {
System.out.println("\n*****Your card is Valid*****");
} else {
System.out.println("\n!!!!Your Card is not Valid !!!!! ");
}
} // <--- add this bracket

How to find Largest Prime Number, Smallest Factor except 1, Sum of Numbers

Herro, Um I'm not sure if this is how you use this site but uh lets get to it... So I need help on this project and I have to do this -
Input [][] Output
A - F -> Multiply by 3, Divide by 4
G - J -> Divide by 3 + 25
K - N -> Find Greatest Factor * 2
O - Q -> Find largest prime inclusive * 3
R - W -> Find Smallest Factor Except 1
X - Z -> Sum Numbers
So I was wondering if my first couple is correct, and need help on the empty space. So the Letter represents the number of the as in "Z" is the last letter so its 26, and "A" is the first so its 1. So if an responses... Thanks ! package Fun;
import java.util.Scanner;
public class Fun {
public static void main(String[] args) {
// TODO Auto-generated method stub
run();
}
public static void run()
{
input();
evaluateAlphabet();
evaluate();
}
public static void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Letter, please");
x = sc.next();
}
public static String x = " ";
public static int temp = 0;
public static int answer = 0;
public static void evaluateAlphabet()
{
if(x.equals("A"))
{
temp = 1;
}
else if (x.equals("B"))
{
temp = 2;
}
else if (x.equals("C"))
{
temp = 3;
}
else if (x.equals("D"))
{
temp = 4;
}
else if (x.equals("E"))
{
temp = 5;
}
else if (x.equals("F"))
{
temp = 6;
}
else if (x.equals("G"))
{
temp = 7;
}
else if (x.equals("H"))
{
temp = 8;
}
else if (x.equals("I"))
{
temp = 9;
}
else if (x.equals("J"))
{
temp = 10;
}
else if (x.equals("K"))
{
temp = 11;
}
else if (x.equals("L"))
{
temp = 12;
}
else if (x.equals("M"))
{
temp = 13;
}
else if (x.equals("N"))
{
temp = 14;
}
else if (x.equals("O"))
{
temp = 15;
}
else if (x.equals("P"))
{
temp = 16;
}
else if (x.equals("Q"))
{
temp = 17;
}
else if (x.equals("R"))
{
temp = 18;
}
else if (x.equals("S"))
{
temp = 19;
}
else if (x.equals("T"))
{
temp = 20;
}
else if (x.equals("U"))
{
temp = 21;
}
else if (x.equals("V"))
{
temp = 22;
}
else if (x.equals("W"))
{
temp = 23;
}
else if (x.equals("X"))
{
temp = 24;
}
else if (x.equals("Y"))
{
temp = 25;
}
else if (x.equals("Z"))
{
temp = 26;
}
else if (x.equals("Qwerty"))
{
temp = 27;
}
}
public static void evaluate()
{
if(temp>=1 && temp<= 6)
{
answer = (temp * 3)/4;
System.out.println("Answer is " + answer);
}
else if(temp >= 7 && temp<= 10)
{
answer = (temp/3) + 25;
System.out.println("Answer is " + answer);
}
else if(temp >= 11 && temp<= 14)
{
}
else if(temp>=15 && temp<= 17)
{
for(int i = temp; i>0; i--)
{
for(int j = 2; j <=i/2 + 1; j++)
{
if(i%j==0)
{
break;
}
if(j==i/2 + 1)
{
answer = i * 3;
}
}
}
System.out.println("Answer is " + answer);
}
else if(temp>=18 && temp<= 23)
{
answer = temp;
}
else if(temp>= 24 && temp<=26)
answer = (answer * 12)%26;
System.out.println("Answer is " + answer);
}
}
-Corruption
Here is a better approach for char to int conversion:
Assuming the string has at least 1 character(check for its length), you can get the temp by doing:
temp = x.getCharAt(0) - 'A' + 1;
or, safety first:
temp = 0;
if (x.matches("^[A-Z]{1}$") {
temp = x.getCharAt(0) - 'A' + 1;
}
What's happening here? Every character has an ASCII code, an integer. So, when you have 2 chars and you try to get the distance between them, the result is an int. 'A' - 'A' = 0(that's why i added a + 1), 'B' - 'A' = 1 and so on.
For the if condition, I am using a RegExp. ^ means start of the input, [A-Z]{1} means one of A-Z, $ means the end of the input. So, if it's an A-Z, temp will get a value, anything else won't make it in the if and your temp will remain 0 so you can easily test if you've got an A-Z or not.
That's all for code review, I won't give you solutions, you must work harder, use Google. You won't enjoy and learn if I give you everything ready for a copy paste.

Categories

Resources