Modifying one object is changing value in a second object - java

State st = sc.que.remove();
System.out.println(st.getMoves() +" - Goal: " + Arrays.toString(st.getGoal()) + "- Puzzle: " + Arrays.toString(st.getPuzzle()));
State ss = new State(st.getPuzzle(), st.getSpace(), st.getMoves(), st.getGoal());
ss.moveUp();
System.out.println(st.getMoves() +" - Goal: " + Arrays.toString(st.getGoal()) + "- Puzzle: " + Arrays.toString(st.getPuzzle()));
Basically I have one state, I print off it's value. Then I create a second state using the value of the first to create an exact copy. I modify the second state using the method moveUp() which swaps a couple elements in an array withine the State. Then we I reprint the value from the first state the one that was not changed they are different.

What are the members of State? If they are objects, you don't copy them by using getMember() but passing a reference to them to the second constructor. If you then call a method that changes a member of the first object, the identical member is changed in the second object, too.

Related

Need help removing item from ArrayList

Im attempting to remove an object form an array list but if fails everytime.
Im using a function to add and a seperate one to remove. The add function is working but the remove is not. I cant see where it is going wrong and failing.
public void unregister( Basics classToRemove){
if(checkIsRegistered(classToRemove)){
getClassList().remove(classToRemove);
System.out.println(getClassList().remove(classToRemove));
System.out.println("You have unregistered from " + classToRemove.className + " class.");
setTotalTuition();
} else{
System.out.println("\nYou are not currently registered for " + classToRemove.className + " class.\n");
}
}
public void register(Basics classToAdd){
if(!checkIsRegistered(classToAdd)){
getClassList().add(classToAdd);
System.out.println("You have registered for " + classToAdd.className + " class.");
setTotalTuition();
}else{
System.out.println("\nYou are already registered for " + classToAdd.className + " class.\n");
}
}
I have attempted using this.arrayList to remove the object and using getters to get the object and remove it.
You can not just print a class-object with System.out.printline. I assume you want to print the attributes of the class, however you are printing the memory address of the object.
Additionally, this doesn't make sense to me:
getClassList().remove(classToRemove);
System.out.println(getClassList().remove(classToRemove));
You can't access and print an Object you just removed.
If this is not helpful, consider providing a minimal example and explaining what exactly is not working.

PDFlib TET get name of the current color

My need is to read the color of a text with PDFlib TET.
As a basis I'm using this PDFlib example: https://www.pdflib.com/tet-cookbook/tet_and_pdflib/search_and_replace_text/
Before both result.add(new rectangle(...)) calls I'm trying to read the color like this:
String csname = tet.pcos_get_string(doc, "colorspaces[" + tet.colorspaceid + "]/name");
if ("Separation".equals(csname)) {
String type = tet.pcos_get_string(doc, "type:colorspaces[" + tet.colorspaceid + "]/colorantname");
System.out.println(type);
if (StringUtils.equalsIgnoreCase("name", type)) {
System.out.println(tet.pcos_get_string(doc, "colorspaces[" + tet.colorspaceid + "]/colorantname"));
}
}
Unfortunately tet.colorspaceid is always 0.
But the correct colorspaceid is 6 (with "correct" = the index of the color the text actually is written with). I know the indexes because I iterated over all colorspaces like this and for i=6 the system prints the name of the intended color:
String type = tet.pcos_get_string(doc, "type:colorspaces[" + i + "]/colorantname");
if (StringUtils.equalsIgnoreCase("name", type)) {
System.out.println(tet.pcos_get_string(doc, "colorspaces[" + i + "]/colorantname"));
}
What do I need to do for tet.colorspaceid being the id of the colorspace of the currently found word fragment?
Or am I completely wrong and TET reads the color somehow else?
Found it - the solution is method print_color_value in this example: https://www.pdflib.com/tet-cookbook/text/glyphinfo/
Just copy method print_color_value, return csname (or colorantname in the if blocks) and rename the method to e.g. getColorValue.
If needed throw away the formatter stuff.

Is it possible to randomize a single word within a String attribute within an object from an array of possible options?

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);

Java variable cannot be resolved or is not a field

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) + "]. ");

Writing basic method that uses multidimensional array in Java

I have this little bit of code
_grid[4][4].setText(_square[4][4].getTopColor() + ": "
+ _square[4][4].getHeight());
Eventually in my program, the text will change because the value of get.Height will change. Is there a way to write a simple program that sets text based on the coordinates of the multidimensional array?
So if the method was called, updateText, I could just do _grid[4][4].updateText(); and it would be the same as the code above. Or if I did _grid[0][12].updateText(), it would do the same as this:
_grid[0][12].setText(_square[0][12].getTopColor() + ": "
+ _square[0][12].getHeight());
It's easy enough to refactor that line into a method in the same class.
private void updateText(int row, int col) {
_grid[row][col].setText(_square[row][col].getTopColor() + ": "
+ _square[row][col].getHeight());
}
If you want to make it a method of the grid items, you'll have to give more details. Do the items in the grid know about their corresponding _squares?

Categories