Reputation: 163
My code is checking if a given string is in a certain format. The first char of the string has to be an uppercase letter and the rest of the string has to be any number that is from 1 to given dimension.
If the first char of the string contains a string from the alphabet array, then the code checks if the rest of the string contains a number from the numbers array. To be a valid coordinate both conditions must be true, if one of them is false it is not a valid coordinate. I want to return the boolean isValidCoordinate but however IntelliJ is telling me that I have to initialise isValid coordinate. Why do I have to initialise it, the boolean expression depends on the 'if' conditions.
Thanks.
public static boolean validCoordinate(String coordinate, int dimension) {
boolean isValidCoordinate;
String [] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int [] numbers = new int [dimension];
int one = 1;
for(int i = 0; i < dimension; i++){
numbers[i] = one + i;
}
for(int i = 0; i < dimension; i++){
if((Character.toString(coordinate.charAt(0))).contains(alphabet[i])) {
for(int j = 0; j < dimension; j++) {
if ((coordinate.substring(1)).contains(Integer.toString(numbers[j]))) {
isValidCoordinate = true;
}
else {
isValidCoordinate = false;
}
}
}
else {
isValidCoordinate = false;
}
}
return isValidCoordinate;
}
This is my final code:
public static boolean validCoordinate(String coordinate, int dimension) {
boolean isValidCoordinate;
String [] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int [] numbers = new int [dimension];
int one = 1;
for(int i = 0; i < dimension; i++){
numbers[i] = one + i;
}
for(int i = 0; i < dimension; i++){
if((Character.toString(coordinate.charAt(0))).contains(alphabet[i])) {
for(int j = 0; j < dimension; j++) {
if ((coordinate.substring(1)).contains(Integer.toString(numbers[j]))) {
isValidCoordinate = true;
}
else {
isValidCoordinate = false;
}
}
}
else {
isValidCoordinate = false;
}
}
return true;
}
Upvotes: 1
Views: 88
Reputation: 393771
It is possible that your for loop will never be entered (for example, if dimension == 0
). In that case, you will never assign a value to isValidCoordinate
, but you will attempt to return the value of that variable in the last statement of your method.
What would be the value of isValidCoordinate
in such case?
It wouldn't have any value.
Therefore, you are forced by the compiler to assign an initial value to isValidCoordinate
, to make sure that it has a value before it is accessed.
EDIT:
Following your comments, I suggest you eliminate the boolean
variable, and use return statements instead:
public static boolean validCoordinate(String coordinate, int dimension) {
String [] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int [] numbers = new int [dimension];
int one = 1;
for(int i = 0; i < dimension; i++){
numbers[i] = one + i;
}
for(int i = 0; i < dimension; i++){
if((Character.toString(coordinate.charAt(0))).contains(alphabet[i])) {
for(int j = 0; j < dimension; j++) {
if ((coordinate.substring(1)).contains(Integer.toString(numbers[j]))) {
return true;
}
}
}
}
return false;
}
This way you don't have to worry about breaking out of nested loops when the boolean
variable is set to true
.
Upvotes: 2
Reputation: 293
import java.util.regex.*;
public static boolean validCoordinate(String coordinate, int dimension)
{
Pattern p = Pattern.compile("[A-Z]+");
Matcher m = p.matcher(coordinate);
return m.matches();
}
Upvotes: 0
Reputation: 50809
All the cases inside the for
loop are covered, but not the case when dimension
is 0.
The method will go straight to return isValidCoordinate;
, where the variable isn't initialized yet.
Upvotes: 3