I'm working with the following:
private char [][] board;
<various lines of code>
//Sequential searching for an index that has yet to be
//changed from the default char character
for(int i = 0; i< board.length; i++)
{
for(int j = 0; j>board[i].length; j++)
{
//Here I get the error "incomparable types: char and char[]
if('\u0000' == board[j])
System.out.println("Game unfinished.");
}
//Here I get the error "incomparable types: char and char[]
if('\u0000' == board[j])
System.out.println("Game unfinished.");
}
else return 'T';
In essence, I want to traverse the array, doing either one of two things:
If all array indexes are occupied by either 'X' or 'O' then return 'T' OR
If an array index is found with '\u0000' do:
System.out.print("Game unfinished.");
First, you'll want to add a null check, just in case. Then, fix the for condition as #libik indicated. Finally, reference the cell by its i and j indecies. e.g:
for(int i = 0; i < board.length; i++) {
//Always check for nulls, unless you're 100% certain of the data
if (board[i] != null) {
for(int j = 0; j < board[i][j].length; j++) {
//Here I get the error "incomparable types: char and char[]
if('\u0000' == board[i][j]) {
System.out.println("Game unfinished.");
}
}
}
Regarding the second if statement and the return 'T', these don't make sense as they stand. Did you want to return 'T' if no '\u0000' chars where found in the array? If so, create a boolean flag, initialize it to false, then set it on the else condition of the if statement above. Outside of the loop, add a second if to see if the flag was ever set and return 'T'.
You have two mistakes.
First change j>board[i].length to j<board[i].length
Then you have to compare char to char, in 2D char, you have to specify both dimension, to get char, specify only one means you only specify row or column of chars (thus it is array of char).
Comparing should look like this : if('\u0000' == board[i][j])
Related
can you please explain me this code? i am not able to understand the use of buffer array. how does value at every index become zero?
public static boolean isAnagram(String input1, String input2) {
if(input1 == null || input2 == null || (input1.length() != input2.length())){
return false;
} else {
int[] buffer = new int[26];
for(int i=0; i < input1.length(); i++){
buffer[input1.charAt(i) - 'a']++;
buffer[input2.charAt(i) - 'a']--;
}
for(int j=0; j < buffer.length; j++){
if(buffer[j] != 0) return false;
}
return true;
}
}
buffer holds a use counter for each lower-case character value used in the strings, all zero initially.
Each counter is then incremented for each use of a character in input1, decremented for each use of a character in input2, so if all characters are used the same number of times, all counters will become zero in the end.
This function will likely crash if called with strings that have anything but lower case letters in them.
Sorry if this is a duplicate but I've looked at a lot of answers and none of them seemed to apply (ex. i start my for loop at 0, instead of one which is a common mistake). This is a method used in an anagram word game. Please help me, I've been at this for five hours straight and I think I'm hallucinating from sleep deprivation.
EDIT: The error occurs at the line ugh.remove(thing.charAt(i));
public boolean anagramOfLetterSubset(String thing, ArrayList<Character> reference) {
ArrayList<Character> ugh = new ArrayList<Character>();
for (int h = 0; h < reference.size(); h++) {
ugh.add(reference.get(h));
}
for (int i = 0; i < thing.length(); i++) { //cycles through the letters in the word
for (int f = 0; f < reference.size(); f++) { //cycles through the characters in the reference arraylist
if ((reference.get(f) == thing.charAt(i)) && (reference.indexOf(thing.charAt(i)) != -1)) { //sees if the letter and the character match
ugh.remove(thing.charAt(i)); //removes first instance of character
}
}
}
if (ugh == reference)
return false; // change the value returned
else
return true;
}
List has two remove methods: remove(int), which removes an element at a given index; and remove(Object) which finds and removes a given object from the list.
If you call remove(thing.charAt(i)), the argument is a char. A char is not an object, but it can be widened to an integer, so it is remove(int) that gets called. The character will be taken to indicate an index in your list (hence the exception).
To call remove(Object) instead, try
ugh.remove((Character) thing.charAt(i));
public static void getCharCountArray(String str)
{
for (int i = 0; i < str.length(); i++)
{
count[str.charAt(i)]++;
}
}
How is count array getting the count of the character. Working of the increment operator?
count is an indexed array of integers.
Each index of this array is a char.
The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
In your loop, str.charAt(i) return the char of your String str for the current iteration.
You get the previous count for the current char of your String with the expression :
count[str.charAt(i)]
and you increment this value with the ++ operator.
we could rewrite your code like this :
for (int i = 0; i < str.length(); i++)
{
char currentChar = str.charAt(i);
int previousCharCount = count[currentChar];
int currentCharCount = previousCharCount + 1;
count[currentChar] = currentCharCount;
}
Your line : count[str.charAt(i)]++; does the same things, but in a simpler and more readable way.
The ++ operator, doesn't operate an increment on the array (non sense) but operates an increment on the integer value at the position char.
I have two exact copies of code here, except one has '<' in the for loops while the other has '<='. Could someone please explain why I get the index out of bounds exception when I use '<=', but then it works fine with '<'
Error code:
for(int i = 0; i <= str.length(); i++) {
int count = 0;
char currentChar = str.charAt(i);
for(int j = 0; j <= str.length(); j++) {
if (currentChar == str.charAt(j) ) {
count++;
Working code:
for(int i = 0; i < str.length(); i++) {
int count = 0;
char currentChar = str.charAt(i);
for(int j = 0; j < str.length(); j++) {
if (currentChar == str.charAt(j) ) {
count++;
If I don't use <= how will it compare the last character in the string?
Valid String indexes in Java, just like the indexes in any array, go from zero to length minus one. So clearly if you set up your condition to go up to i <= str.length(), you'll get outside the string.
Remember that a String on the inside is nothing more than a char[], and again: the valid indexes go from 0 to length-1. This is a convention, followed by many other programming languages that decided to start counting from zero instead of one.
Because you cannot access str.chatAt(str.length()) without throwing a exception.
a < b means "a is less than b" and it will be false when a equals to b.
a <= b means "a is less than or equals to b" and it will be true when a equals to b.
To compare the last character in the string, write some code to do so, compile and run.
bool res = currentChar == str.charAt(str.length() - 1); // assuming str has string with one character or more
str.length() returns the number of characters in the String. So "String".length() returns 6.
Now, when using indices, you start with zero. so "String".charAt(0) returns 'S'. "String".charAt(6) gives you a StringIndexOutOfBoundsException because the last character in "String" is at index 5.
String indexes begin at 0. str.length() returns how many elements are in your array. if you have a string
"dog"
"dog".length() = 3,
'd':0, 'o':1, 'g':2.
Since your for loop initializes i to 0, the working loop goes through indexes 0-2, which is 3 values, while the non-working one goes 0-3, and references a null, and str.charAt(3) does not exist.
I created a method that needs to check the range of a multidimensional array and make sure each value in the 2D array is smaller than or equal to the length of the array.
public static boolean method(int[][] solution){
//check rows for 1-N
for (int i=0; i < solution.length; i++){
if (solution[i] > solution.length)
return false; //check rows
}
//check columns for 1 - N
for (int j = 0; j<solution.length; j++){
//get a column in the one dimensional array
int[] column = new int[solution.length];
for (int i=0; i < solution.length; i++){
column[i] = solution[i][j];
}
if (column[i] > solution.length)
return false; //check columns
}
return true;
}
However, the errors I receive are follows:
Program.java:99: error: bad operand types for binary operator '>'
if (solution[i] > solution.length)
^
first type: int[]
second type: int
Program.java:110: error: cannot find symbol
if (column[i] > solution.length)
^
symbol: variable i
location: class Program
2 errors
Perhaps for the first error i need to get the array value instead of comparing the array? Not sure..
First, you can't compare an array with an int value
if (solution[i] > solution.length) // solution is a 2-d array and thus solution[i] is a 1-d array
// which can't be compared with an int value
and second, the i was declared in the for loop, and thus it's not visible outside its scope.
for (int i=0; i < solution.length; i++){
column[i] = solution[i][j];
} // scope of i is over here
if (column[i] > solution.length) // that's why you can't access `i` here
return false;
You either need to use j or declare the i before the for, so that you can use it after it or probably, move the if inside the inner for loop(and that seems to be the right way for your problem as well).
You are using i outside the for loop, maybe you want to use j instead.
Its because, solution is a 2d array.
int[][] solution;
and to get this work it should be,
if(solution[][] > solution.length)
I suppose this much code should suffice
public static boolean method(int[][] solution) {
// check rows for 1-N
for (int i = 0; i < solution.length; i++) {
for (int j = 0; j < solution[i].length; i++) {
if (solution[i][j] > solution.length)
return false; // check rows
}
}
return true;
}