I am current lost in that problem. I have read a lot here about other problems with ConcurrentModificationException, but can't resolve my problem. Maybe i don't see the wood for the trees. I hope you can help me. The method is
public void crop(PesNode n) {
for (int ii = n.getPostNodes().size()-1; ii >= 0; ii--) {
for (int i = ii-1; i >= 0; i--) {
PesNode n1=n.getPostNodes().get(i);
PesNode n2=n.getPostNodes().get(ii);
if (n1.getName().equals(n2.getName())) {
boolean merge=true;
for (PesNode node : n1.getPreNodes()) {
if (!n2.getPreNodes().contains(node)) {
merge=false;
}
}
for (PesNode node : n2.getPreNodes()) {
if (!n1.getPreNodes().contains(node)) {
merge=false;
}
}
if (merge) {
//Merge n1 and n2
//for(Iterator<PesArc> itPesArc = n2.getPost().iterator(); itPesArc.hasNext();) { PesArc a = itPesArc.next();
for(PesArc a : n2.getPost()) {
a.setFrom(n1);
n1.getPost().add(a);
}
//for(Iterator<PesArc> itPesArc = n2.getPost().iterator(); itPesArc.hasNext();) { PesArc a = itPesArc.next();
for(PesArc a : n2.getPre()) {
arcs.remove(a);
a.getFrom().getPost().remove(a);
}
//for(Iterator<Integer> it = n2.getCoset().iterator(); it.hasNext();) { int j = it.next();
for (int j : n2.getCoset()) {
if (!n1.getCoset().contains(j)) {
n1.getCoset().add(j);
}
}
nodes.remove(n2);
//n2.getPost().removeIf(Objects::isNull);
//n2.getPre().removeIf(Objects::isNull);
//n2.getCoset().removeIf(Objects::isNull);
i=0;
}
}
}
}
for (PesNode x : n.getPostNodes()) {
crop(x);
}
The errors comes from the "crop(x)" at the end and pops up at "a.setFrom(n1);"
I dont understand why this happens, because every for loop comes to its ends before the next one starts. I also tried to use iterators instead of the for loops, without any solution. What do I wrong?
You get ConcurrentModificationException because you try to remove element while you are using for operator. So I think the line in your code nodes.remove(n2); can lead to this exception. You can use Iterator or just to gather the elements for removal in another Collection and then remove it. For more information: https://www.baeldung.com/java-concurrentmodificationexception
This code visualises the A* algorithm. When it reaches the end of the algorithm, it pops with an alerts saying the path is found using
JOptionPane.showMessageDialog(null, "Path Found")
After clicking away the windows disappears but then gets rendered again in the top left corner without the option to close it. Also, the path to the goal is rendered after clicking away however, in the code it gets rendered before the alert appears. I am sure that the problem is in this method specifically in the way I draw or call the repaint method and not in the actual algorithm itself. Could someone please explain where I am going wrong with this?
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.renderGrid(g);
this.renderCloseCells(g);
this.renderOpenCells(g);
if (aStar.getOpenSet().size() > 0) {
int lowestCostIndex = 0;
for (int i = 0; i < aStar.getOpenSet().size(); i++){
if (aStar.getOpenSet().get(i).getF() < aStar.getOpenSet().get(lowestCostIndex).getF()){
lowestCostIndex = i;
}
}
Cell current = aStar.getOpenSet().get(lowestCostIndex);
if (aStar.getOpenSet().get(lowestCostIndex) == aStar.getEnd()){
Cell temp = current;
aStar.addItemPath(temp);
while (temp.getParent() != null){
aStar.addItemPath(temp.getParent());
temp = temp.getParent();
}
System.out.println("Done");
this.renderPath(g);
JOptionPane.showMessageDialog(null, "Path Found");
return;
}
aStar.removeCellOpenSet(current);
aStar.addCellCloseSet(current);
for (int i = 0; i < current.getNeighbors().size(); i++){
Cell neighbor = current.getNeighbors().get(i);
if (!aStar.getCloseSet().contains(neighbor)){
int tempG = current.getG() + 1;
if (aStar.getOpenSet().contains(neighbor)) {
if(tempG < neighbor.getG()){
neighbor.setG(tempG);
}
} else {
neighbor.setG(tempG);
aStar.addCellOpenSet(neighbor);
}
neighbor.setH(aStar.heuristic(neighbor, aStar.getEnd()));
neighbor.setF(neighbor.getG() + neighbor.getH());
neighbor.setParent(current);
}
}
} else {
JOptionPane.showMessageDialog(null, "Path Not Found!");
}
this.repaint();
}
I have been buried in this assignment for 2 days chasing down rabbit holes for possible solutions. I am beginner Java, so I am sure this shouldn't be as difficult as I am making it.
I trying to program the infamous Java Bean Machine... My professor want the Class Path to return a String Variable that only holds "R" "L" . to represent the path of the dropped ball.
Each ball should have its own Path... I can get the path... but I can not get the path to print in a string outside of the for/if statement.
Here are his instructions... in case you can see if I am interpreting this incorrectly.
Please help!! Thank you in advance for sifting through this....
my code so far ******** i have updated the code to reflect the suggestions.. Thank you... ***************** New problem is it repeats the series of letters in a line... I only need a string of 6 char ....(LRLLRL)
public class Path {
StringBuilder myPath;
public Path() {
myPath = new StringBuilder();
}
void moveRight() {
myPath.append("R");
}
void moveLeft() {
myPath.append("L");
}
public void fallLevels(int levels) {
levels = 6;
for (int i = 0; i < (levels); i++) {
if (Math.random() < 0.5) {
this.moveRight();
} else {
this.moveLeft();
}
}
}
public String getPath() {
System.out.print(myPath.toString());
return myPath.toString();
}
}
}
******Thank you all.. this class now returns the correct string for one ball...***************
here is my code so far for multiple balls... I can get a long continuous string of 6 character sequences... I need each sequence to be a searchable string...I am not sure if I need to alter the Path class or if its something in the simulateGame() method. I think I can take it after this hump... Thank you again....
public class BeanMachine {
int numberOfLevels;
int[] ballsInBins;
Path thePath = new Path();
public BeanMachine(int numberOfLevels) {
this.numberOfLevels = 6;
ballsInBins = new int[this.numberOfLevels + 1];
// this.numberOfLevels +
}
public void simulateGame(int number) {
//looping through each ball
for (int i = 0; i < numberOfLevels -1; i++) {
thePath.fallLevels(0);
}
thePath.getPath().toString();
}
*** this isn't the entire code for this class... I have to get this method correct to continue....
Problem with your code:
if (Math.random() < 0.5) {
**loop = this.myPath = "R";**
} else {
**loop = this.myPath ="L";**
}
Change this to:
if (Math.random() < 0.5) {
**loop = this.myPath + "R";**
} else {
**loop = this.myPath + "L";**
}
Just added ** to highlight where there is wrong in your code
I am aware there are multiple threads like my assignment below, but I just can't figure it out. I can't exactly figure out the mistake. Help would be appreciated.
I am trying to do this program:
Everything works fine unless I input the same chains or similar (for example ACTG and ACTG or ACTG and ACTGCCCC), when it tells me
string index out of range
This is that part of my code:
int tries=0;
int pos=-1;
int k;
for (int i=0; i<longDNA.length(); i++) {
tries=0;
k=i;
for (int j=0; j<shortDNA.length(); j++) {
char s=shortDNA.charAt(j);
char l=longDNA.charAt(k);
if (canConnect(s,l)) {
tries+=1;
k+=1;
}
}
if (tries==shortDNA.length()-1) {
pos=i-1;
break;
}
}
Let's call the two DNA strings longer and shorter. In order for shorter to attach somewhere on longer, a sequence of bases complementary to shorter must be found somewhere in longer, e.g. if there is ACGT in shorter, then you need to find TGCA somewhere in longer.
So, if you take shorter and flip all of its bases to their complements:
char[] cs = shorter.toCharArray();
for (int i = 0; i < cs.length; ++i) {
// getComplement changes A->T, C->G, G->C, T->A,
// and throws an exception in all other cases
cs[i] = getComplement(cs[i]);
}
String shorterComplement = new String(cs);
For the examples given in your question, the complement of TTGCC is AACGG, and the complement of TGC is ACG.
Then all you have to do is to find shorterComplement within longer. You can do this trivially using indexOf:
return longer.indexOf(shorterComplement);
Of course, if the point of the exercise is to learn how to do string matching, you can look at well-known algorithms for doing the equivalent of indexOf. For instance, Wikipedia has a category for String matching algorithms.
I tried to replicate your full code as fast as I could, I'm not sure if I fixed the problem but you don't get any errors.
Please try it and see if it works.
I hope you get this in time and good luck!
import java.util.Arrays;
public class DNA {
public static void main(String[] args) {
System.out.println(findFirstMatchingPosition("ACTG", "ACTG"));
}
public static int findFirstMatchingPosition(String shortDNA, String longDNA) {
int positionInLong = 0;
int positionInShort;
while (positionInLong < longDNA.length()) {
positionInShort = 0;
while(positionInShort < shortDNA.length()) {
String s = shortDNA.substring(positionInShort, positionInShort + 1);
if(positionInShort + positionInLong + 1 > longDNA.length()) {
break;
}
String l = longDNA.substring(positionInShort + positionInLong, positionInShort + positionInLong + 1);
if(canConnect(s, l)) {
positionInShort++;
if(positionInShort == shortDNA.length()) {
return positionInLong;
}
} else {
break;
}
}
positionInLong++;
if(positionInLong == longDNA.length()) {
return -1;
}
}
return -1;
}
private static String[] connections = {
"AT",
"TA",
"GC",
"CG"
};
private static boolean canConnect(String s, String l) {
if(Arrays.asList(connections).contains((s+l).toUpperCase())) {
return true;
} else {
return false;
}
}
}
I finally changed something with the k as Faraz had mentioned above to make sure the charAt does not get used when k overrides the length of the string and the program worked marvelously!
The code was changed to the following:
int tries=0;
int pos=-1;
int k;
for (int i=0; i<longDNA.length(); i++) {
tries=0;
k=i;
for (int j=0; j<shortDNA.length(); j++) {
if (k<longDNA.length()) {
char s=shortDNA.charAt(j);
char l=longDNA.charAt(k);
if ((s=='A' && l=='T') || (s=='T' && l=='A') || (s=='G' && l=='C') || (s=='C' && l=='G')) {
tries+=1;
k+=1;
}
}
}
if (tries==shortDNA.length()) {
pos=i;
break;
}
}
I am not sure how aesthetically pleasing or correct this excerpt is but - it completely solved my problem, and just 2 minutes before the deadline! :)
A huge thanks to all of you for spending some time to help me!!
I have an applet that I am trying to make read a file. It throws an exception, but I am passing it the correct path so I am not sure where I am going wrong. I am using this to read numbers and use those numbers to change a multidimensional array, if you were wondering. Heres the code:
public class Save {
public void loadSave(File loadPath) {
try {
Scanner loadScanner = new Scanner(loadPath);
while(loadScanner.hasNext()){
for(int y = 0; y < Screen.room.block.length;y++){
for(int x = 0; x < Screen.room.block[0].length;x++){
Screen.room.block[y][x].groundID = loadScanner.nextInt();
System.out.println(loadScanner.nextInt());
}
}
for(int y = 0; y < Screen.room.block.length;y++){
for(int x = 0; x < Screen.room.block[0].length;x++){
Screen.room.block[y][x].airID = loadScanner.nextInt();
}
}
}
loadScanner.close();
} catch (Exception e) { e.printStackTrace();}
}
}
How I access it:
save.loadSave(new File(frame.getClass().getResource("mission1.tdm").toString()));
Ok, I used the edited code up above and it still says that it cannot find the file, even though the error spits out the exact path that it is in.