I'm getting a "Cannot be resolved or is not a field" error on a variable in one of my Java classes, and I don't understand why... I've had a look online, but can't find anything that really explains why I'm getting it. The error is happening in the following for loop:
int i;
getFilterConditions();
for(i = 0; i < sitesToBeFiltered.size(); i++){
if(sitesToBeFiltered.get(i) == filter1Value){
Gui.displayFilteredOutput.append("\n");
Gui.displayFilteredOutput.append("EID: [" + sitesToBeFiltered.get(i) + ", " + applicationsToBeFiltered.get(i) + ", " + IDsToBeFiltered.get(i) + "]. ");
Vector3Double filteredEntityPosition =
Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + sitesToBeFiltered.get(i).positionsToBeFilteredX.get(i));
}
}
It's being generated on the positionsToBeFilteredX.get(i) variable at the end of the for loop. I have defined that variable as a global variable at the top of the class using the line:
public static ArrayList<Double> positionsToBeFilteredX = new ArrayList<Double>();
To explain what I'm trying to do here:
I have a program that is reading the PDUs that are being sent/ received over a network, and storing both the PDUs themselves, and information held by each of the PDUs in a number of ArrayLists. What I'm trying to do with this code, is to take a value entered by the user on a form (stored in the filter1Value integer variable), and check whether that value is equal to any of the elements in a particular ArrayList (sitesToBeFiltered).
So, I am looping through the sitesToBeFiltered ArrayList, and checking every element to see whether it is exactly equal to the value of filter1Value. If it is, I am then appending some text about the matching ArrayList element to a JTextArea (displayFilteredOutput).
One of the things I want to add to the JTextArea is the X position of the matching element (which was added to the positionsToBeFilteredX when it was found that that the element matched the user's search criteria.
So what I'm trying to do with the last line of code is to append the X coordinate (stored in an array of X coordinates) of the matching element in the sitesToBeFiltered ArrayList, to the displayFilteredOutput JTextArea, but for some reason, I'm getting this "cannot be resolved, or is not a field" compile error on the variable.
Can anyone explain to me why this is? I suspect that I am not referencing the X coordinate of the element matching the filter value correctly, but I'm not sure how I should be doing this... Could someone point me in the right direction?
Your code is written as though positionsToBeFiltered is a field in the object returned by sitesToBeFiltered.get(i). Evidently it isn't.
Should have seen it sooner: the issue was because I was trying to assign the value to a variable that was of an incompatible type. To solve this, I just needed to append the value to the JTextArea in the Gui without assigning it to a variable first: i.e. instead of writing
Vector3Double filteredEntityPosition = Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + positionsToBeFiltered.get(i);
I just needed to write:
Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + positionToBeFiltered.get(i) + "]. ");
Related
I'm very new to java and coding in general, so I apologize if this is a simple thing. I want to know if it's possible to add a variable within a string that will be randomized when the item itself is chosen from the ArrayList of options.
I've already created the objects, put them in an ArrayList, and have a method that randomly calls indices from the ArrayList and prints their attributes. However, some (not all) I would like to randomize a part of the "name" attribute from another separate Array. Is there a way to do this? If possible, I'd like it to be randomized each time its called, so in the case that I call that particular object twice from the ArrayList, its "name" attribute would not be identical (unless by chance the same suffix is called from the random name Array twice).
//Doesn't need randomization
Item r1 = new Item("Static Name", 1000);
// (X) needs to be either "Title", "Callsign", or "Identifier"
Item r2 = new Item("Random (X)", 500);
UPDATE:
I followed DevilsHnd's suggestion, but it doesn't seem to replace anything.
//declared in class
String[] suffix = {"Title","Callsign","Nickname"};
//as part of toString()
if (name.contains("(X)")) {
name.replace("(X)", [new Random().nextInt(((suffix.length - 1) - 0) + 1) + 0]);
My toString still prints out "Random (X)" instead of a desired "Random Title".
I am able to get it to work if I remove the if function and use a static value for the replacement, like name = name.replace("(X)","Title");, but I still can't figure out how to get it to replace it with a value from the suffix array.
I'm not sure if I get what you mean but you may get an idea from the following:
String[] attributes = {"Title", "Callsign", "Identifier"};
String itemString = "Random (X)";
if (itemString.contains("(X)")) {
itemString = itemString.replace(
"(X)",
attributes[new Random().nextInt(((attributes.length - 1) - 0) + 1) + 0]
);
}
System.out.println(itemString);
Item r2 = new Item(itemString, 500);
I've got an ArrayList called PhotoArrayList. It contains strings like "picture1.jpg, picture2.png, picture3.gif etc." There is like 50 or 60 strings in this ArrayList. I need to add path of their folder in the beginning like "mnt/sdcard0/Pictures/picture1.jpg etc." So, I'm using following code
Integer PhotoFileAmount = PhotoArray.length; //PhotoArray and PhotoArrayList are same
for(int i=0; i < PhotoFileAmount; i++){
String PhotoFileAndPath = (PhotoFolder + '/' + PhotoArrayList.get(i));
PhotoArrayList.remove(PhotoArrayList.get(i));
PhotoArrayList.add(PhotoFileAndPath);
}
But I'm getting a strange result. The beginning of PhotoArrayList is unchanged while it's middle part is okay and last part gets the path twitce. Like "picture1.jpg, mnt/sdcard0/Pictures/picture2.png, mnt/sdcard0/Pictures/mnt/sdcard0/Pictures/picture3.gif
If you want to change elements of a ArrayList, use the set method to assign a new value to the element at a given position:
int PhotoFileAmount = PhotoArray.length; // use int here to avoid unnecessary boxing / unboxing
for(int i=0; i < PhotoFileAmount; i++){
String PhotoFileAndPath = (PhotoFolder + '/' + PhotoArrayList.get(i));
PhotoArrayList.set(i, PhotoFileAndPath);
}
If you use remove and add you not only change the indices of the elements in the list; even if you make it work it's very inefficient, since all remaining elements in the list have to be moved every time remove is called.
ArrayList.add(E Object) adds the Object at the at the end of the list.
You are currently removing an item from where ever it is in the list, then adding a new version at the end of the list. As i approaches PhotoFileAmount your .get(i) statement is going to start retrieving the revised objects that you have added at the end.
I have to write a program for a Boggle-like game, and I currently have it check each letter below the current one to see if they make a word. So for a board like so:
W O Y R
F U M F
H T R V
I G S W
The only word it would find is "OUT" going from top to bottom. When it finds part of a word it puts that letter into a string and sets it to null so it won't use a letter twice in the same word (the full algorithm has to be able to search in multiple directions). I use a stack to keep track of the coordinates of the letters I've used so I can backtrack, and every time I pop the stack I take the last letter of the string and put it back into the board in its original position. But the issue is that if multiple letters are removed, it places them all in the same index, overwriting the previous one. So in the case of "OUT" the board ends up looking like this after replacing the three letters:
W null Y R
F null M F
H O R V
I G S W
I've gone through my code and tried rewriting it twice but it always does this. Do you have any insight as to why this is happening?
private void checkNeighbors(LetterCoor center){
String check = out;
while (!path.empty()){
if(center.getDirec()==0){//If the direction to check is down
System.out.println("Bottom");
if((center.getRow())+1<sideLength && board[(center.getRow())+1][center.getCol()]!=null){//makes sure the space below is !null and !out of bounds
check+=board[center.getRow()+1][center.getCol()];
System.out.println("Checking " + check);
if(isValidWord(check)){//checks if string is part of the lexicon
center.nextNeighbor();
board[center.getRow()+1][center.getCol()]=null;
center = new LetterCoor(center.getRow()+1, center.getCol(), 0);
System.out.println("push " + check.substring(check.length()-1));
path.push(center);
out=check;
}
else{
center=(LetterCoor) path.pop();
center.nextNeighbor();
path.push(center);
}
}//end of null if
else{
System.out.println("Null or end of board");
center=(LetterCoor) path.pop();
center.nextNeighbor();
path.push(center);
}
}//end of direc 0 if
else{
System.out.println("pop " + out.substring(out.length()-1,out.length()));
center=(LetterCoor) path.pop();
center.nextNeighbor();
board[center.getRow()][center.getCol()]=out.substring(out.length()-1,out.length());
out=out.substring(0,out.length()-1);
if (center.getDirec()<1){
path.push(center);
}
}
System.out.println("Current string is " + out);
}//end of while loop
}
If you need any clarification of my code please let me know.
Also, as clarification the LeterCoor object stores three ints. The first is the row index of the letter, the second is the column index and the third indicates which direction it is searching in (0=down, 1=down right, 2=right, etc)
I ended up coming across the solution on my own. The issue was with my LetterCoor object. Eclipse required the variables be set as static since I had the object class in a separate file, so when I updated the coordinate data in one LetterCoor object, it set the data for every LetterCoor object to that coordinate. I resolved this by moving the object class into the same file as this class and removing the static declaration from the variables.
I'm trying to get a program so that it loops and adds up the sum of an array. My code appears to be working, with the exception that it states that the text[j] in adding = adding + text[j] is an incompatible type (I'm assuming data type). Earlier in the code, I have int adding = 0;. This is the erroneous code:
for (int j=0;j<=total;j++){
adding = adding + text[j];
System.out.println(text[j]);
}
where total is the limiting factor. If I put:
for (int j=0;j<= total;j++){
adding = adding + j;
System.out.println(text[j]);
}
the program compiles but gives 45, which is incorrect.
Why is this happening? Thanks!
The answer actually turned out to be outside the code given. I had set my array to be a String, not an int as it should have been.
If your text[] is String[] or char[] as the name suggests then I believe you are trying to update text[] elements with suffix j or adding, which you can write as:
If it is char[] then write
text[j] = (char)(adding + (int)text[j]);
If it is String[] then write
text[j]= text[j]+adding;
as required. It all depends on what is the data type of text[] and what are you trying to achieve?
Also as suggested in one of the answers, if total is length of the array, then change the comparison to < to avoind ArrayIndexOutOfBoundsException
Your second example, adds j into adding but prints text[j] value, which is nothing to do with the addition of adding and j.
I am quite new to Android / java development. I have a problem (probaly very basic) that I could not solve even after trying several workarounds and internet researches...
public double P_N_comp[][] = new double[16][40];
I populate the 16*40 values by using a function
Then I want to display the very last value of my array :
Double last_P_N = P_N[15][39]; // This is the line where i get the error
TextView myTextView3 = (TextView) findViewById(R.id.mytextview3);
myTextView3.setText("Last P_N value" + last_P_N);
The error says "The type of the expression must be an array type but it resolved to double"
So if I understand, Java wants to have an array on both sides of the expression (line with error).
But why ?
As far as I understand P_N[15][39] refers to the last value of the array which is a double (and thus, is not an array). And last_P_N has just been declared as a double....
Thank you in advance for your help !
Here:
Double last_P_N = P_N[15][39];
What's the P_N? The name of your array is P_N_comp.
Are you sure you don't mix anything there?
I see your array is declared:
double P_N_comp[][] = new double[16][40];
But you are trying to assing:
Double last_P_N = P_N[15][39];
I believe there's a typo. = P_N_comp[15][39] should be assigned instead, not P_N[15][39]