https://www.dropbox.com/s/5iklxvhslh4kfe7/CS%203114.zip
There's some bug in my code for my school project that I just can't figure out. The link above is to my code for the project. The project instructions is in the P1.pdf file.
My error has something to do with this code:
/*
for (int i = 0; i < reactions.length; i++)
{
reactions[i].UpdateFireTime();
debugwriter.write(i + "| " + reactions[i].FireTime());
debugwriter.newLine();
}
debugwriter.newLine();
heap.build();
//*/
//*
for (int i = 0; i < table[reactionIndex].length; i++)
{
int rindex = table[reactionIndex][i];
reactions[rindex].UpdateFireTime();
}
for(int i = 0; i < reactions.length; i++)
{
debugwriter.write(i + "| " + reactions[i].FireTime());
debugwriter.newLine();
}
debugwriter.newLine();
heap.build();
//*/
The first for loop updates the firing time of every reaction, while the second for loop uses my table to update specific dependent reactions. My answers are correct for the first for loop but incorrect when I use the second one. I've tested to see which propensities change if I update every reactions firing time and the results match my table. This means the only difference is the -Math.log(Math.random()) factor. If I set the random number to a constant, then I get the same results using both loops. I've looked over my code many times and I just can't figure out what the problem could be. Can anyone help me out?
P.S.:
The .ltf files are just .txt files that are quite large. I use the .ltf to distinguish them from regular .txt files
The correct means for the DIMER example are: ~650 ~650 ~220
EDIT: The third loop is just for debugging purposes. The 2 loops I'm talking about are the 1st and 2nd one where the 1st one is the one that's commented out.
You don't need table[reactionIndex] in your first loop. Just use table.length-1 and you can use i as your spot in the index to loop through and do stuff with.
Related
There is a table with application numbers and user data. at the beginning of the table, the application number at the end is a green arrow. There can be an infinite number of fields in a table and it always changes.
I need to find the green arrow by the application number and click on it accordingly.
tried to do something like
String myorder = "629/0000/000000021059";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div/div[1]/table/tbody/tr/td[5]\n")).getText())){
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[1]/table/tbody/tr/td[18]/a/img\n")).click();
} if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/table/tbody/tr/td[18]/a/img\n")).click();
} if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[3]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[3]/table/tbody/tr/td[18]/a/img\n")).click();
and so on up to 100+
The first problem is that I really don't like the option of writing if else 100 times. And there is only one digit in the diva where the id of the application and one in the digit in the diva where the green arrow is different(img)
The second problem is that no matter how I insert break; when finding the right application, he clicks on the desired arrow, but after apparently trying to continue searching the page, but the page has already changed and the error falls
(WARNING: The server did not provide any stacktrace information)
if I use the code that was attached above or just an indication of the element (which goes right after the one that turned out to be correct and the page has changed) with a note that I can not find it, if I use else if with break;
tried to do so
for (int i = 1; i < 25; i++) {
String myorder = "629/6300/000000412067";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div["+ i++ +"]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div["+ i++ +"]/table/tbody/tr/td[18]/a/img\n")).click();
break;
}
But if the item is located, then it clicks on the very first arrow in the table, and not on the one in the same column as the application number
I would really appreciate your help!
XPaths are 1-based not 0, so you are correct in thinking you need to add one, however, these two are not equal as i++ modifies the value of i (even inline like you have it), which you do twice inside your loop. Therefore the 2nd iteration of your loop i will be 3, not 1 as expected. In your loop replace i++ with i + 1 and you should be good to go.
for (int i = 1; i < 25; i++) {
String myorder = "629/3500/000000329976";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[" + i + 1 + "]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[" + i + 1 + "]/table/tbody/tr/td[18]/a/img\n")).click();
break;
}
}
Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[31]/table/tbody/tr/td[5]
"}
So it was searching untill 31. And it's strange because i used for i < 25 and there are only 25 rows in the table.
but still dont work for me;(
I am creating a game board. I need it to reveal a the selected column after a user input, while the rest of the columns still print as "X". This game holds values I have set in each column, but does not print them on the screen. When the user selects a column, I need it to print showing the value that column is holding while the rest of the columns still print "X" so they do not reveal what they have. I am new to this, thank you for your help.
This is the function where I think the problem is. If you look, you will see that I have the if statement "if (isCovered) - then I want it to print the covered version. Then the "else" - which is where I want it to print just the one that was guessed as its actual value. I have tried multiple ways of achieving this with no luck. Is there are way to make it like (!isCovered)? But that doesn't work, because it states it needs to be an array and the function "!" does not work. Right now it just seems like it never prints the "else" statement at all. I have functions that take the user input and compare them to "isCovered" and they work correctly, because the piece moves on the board as it should. I just cannot get it to print the actual value instead of an "X". Thank you for any help and if further information would be helpful, please let me know. It is due today unfortunately I only had a few days to work on it and have been working constantly on it.
public static void PrintRevealBoard(int[][] myArray,Boolean[][] isCovered)
{
int i, j;
for (i = 0; i<myArray.length ; i++ ) { // array.length = max rows
System.out.print((i+1) + " ");
for(j = 0; j <myArray[0].length; j++) { // array[0].length = max
cols
if(isCovered[i][j]){
System.out.print(GetRollColorCovered(myArray[i][j]) + " ");
} else {
System.out.print(GetRollColor(myArray[i][j]) + " ");
}
}
your main module is kinda messy. And I don't know how GetRollColor(dice) works. Anyway as I understand you have a two dimensional array and you want to show only a specific value. Seems like u want to show the entire input column.
use this to update isRevealed() after the input of inputCol.
public static Boolean[][] updateRevealed(Boolean[][] isRevealed, int inputCol){
for(int i=0;i<isRevealed[inputCol].length;i++)
isRevealed[inputCol][i] = true;
return isRevealed;
}
update like this,
isRevealed = updateRevealed(isRevealed,inputCol);
your printRevealBoard is almost correct. Just remove the first line. It doesn't make sense and you don't want it as I see
int isRevealed = inputCol;
I don't know how your array looks like. But because of the first for loop u will definitely get an
index out of bounds exception
loop runs until I becomes myarray.length. and in the next loop you access index I of myArray. Exception will be thrown if I=myArray.length. u gotta fix it. If any problem occurs lemme know.
thankyou
edit:
try this for printRevealBoard
public static void printRevealBoard(char[][] myarray , Boolean[] []isRevealed){
for(int i=0;i<myarray.length;i++){
for(int j=0;j<myarray[0].length;j++){
if (isRevealed[i][j]) System.out.print(myArray[i][j] + " ");
else System.out.print("* ");
}
System.out.println();
}
}
Say I want to get the 'of' typed in the input by the user
I am making an app, not finishing off in this dumb output screen (This is only an example)
import java.util.*;
Scanner input = new Scanner(System.in);
String text = input.nextLine();
int check = 0;
for(int i = 0; i < text.length(); i++;){
if(text.substring(i, i + 2) .equals("of")){
check = 0;
}
}
If the user enters abofd, it surely recognises the of at position at 2-4.
But when i value is 4 it checks position from 4-6, but as position 6 is not present is not present it gives an error.
I know you are thinking me to set i < text.length() - 1 at line 5, but my original code needs to run until the end!
It is always a good idea to validate input coming from a user, but you are very much overcomplicating things! Even worse, you wrote down outright wrong code:
String text = input.nextLine();
int check = 0;
for(int i = 0; i < text.length(); i++;){
if(text.substring(i, i + 2) .equals("of")){
The above can't work! You see, i iterates from 0 to text LENGTH. But then you are using i+2 to get a substring from text. (so: say hello to your first ArrayIndexOutOfBoundsException).
Instead, you can do things like:
if (text.contains("of")) {
or
if (text.indexOf("of") >= 0) {
to find out if your string contains "of".
And to answer your question in the title: absolutely yes. Programming is about being a good craftsman to a very large degree. A good craftsman keeps all his tools and materials in order. He doesn't allow for mess, waste, ...
So, long story short: from day one, to all eternity: when writing code, you strive for a zero tolerance policy: no compiler errors, no warnings, nothing in your code that doesn't belong there!
I've tried several programs that involve printing to the console in for loops, but none of them have printed anything. I can't work out the problem, and I've boiled it down as simply as possible here:
for (int x=0; x==10; x++)
{
System.out.print("Test");
}
Like I said, absolutely nothing is printed to the console. Things outside of the for loop will print, and things affected by the for loop will print.
Perhaps it's something very simple, but I wouldn't know considering I'm relatively new to programming and Eclipse gives me no errors. Any help would be much appreciated, as this is plaguing my class files at the moment.
Thanks,
Daniel
Your for loop condition is wrong. You want the condition to be true to continue looping, and false to stop.
Try
for (int x=0; x < 10; x++)
For more information, here's the Java tutorial on for loops.
#rgettman gave the reason your code didn't work above.
The way the for loop works is in the brackets the first variable is where the loop starts (i.e. 'x=0'), the second variable is the condition (i.e. 'x<= 10'), and the third is what to do for each loop (i.e. 'x++').
You had "x==10" for the condition, so for the first scenario where x was equal to "0", the loop ended because it was NOT equal to "10". So you want it to be "x<=10" (x is less than or equal to 10); this will go through 11 loops.
rgettman is completely correct. A for loop should be used as so:
for is a type of loop in java that will take three parameters separated by semicolons ;
The first parameter will take a variable such as int i = 0; to create a simple integer at 0.
The second parameter will take a condition such as i < 10, to say while the i integer is less than
The third and final parameter will take an incrementing value like, i++, i--, i +=5, or something to that effect.
This part should like like for(int i = 0; i < 10; i++) so far.
Now you need the brackets { and }. Inside of the brackets you will perform an action. Like you wanted to print "test" to the console.
for(int i = 0; i < 10; i++) {
System.out.println("test");
}
This would print "test" 10 times into the console. If you wanted to see what number i was at, you could simply say,
for(int i = 0; i < 10; i++) {
System.out.println(i); // Current value of i
}
Hope this was of use to you!
I have a Project class that holds a name and various other attributes. Everytime a new Project is made it is added to an ArrayList.
ArrayList<Project> mProject = new ArrayList<Project>();
I have most of the code in a different question. I don't feel like I should ask it there as it has a different question on it. But if you want to check out more of the code it's here:
Can I use a method from a different class that I have added to an ArrayList?
And here is where I'm stuck. This is the first time I've used this type of loop before and I don't know what's going on. I have seen tutorials just pass over this loop with a "you should know what happens here" attituded. So if you could explain this loop that would be great also.
And on to the code:
System.out.println("Select one of the fallowing projects:");
for (Project proj : mProject){
int i= 1;
i++;
System.out.println( i + ": " + proj.returnName());
}
My output is this:
Select one of the fallowing projects:
2: Greg
2: Mike
It looks like it's not going though the code. So I'd love an explanation or a tutorial with this loop. Thanks.
If I use a regular loop how whould I go about using the methods from the mProject arraylist?
for ( int i = 0; i<= mProject.size() ; i++){
Project proj = mProject;// This won't compile.
if (choice == proj.returnName()){
}
}
If you move the int declaration out of the loop it will increment as you would expect, it is getting reassigned every time:
int i= 1;
for (Project proj : mProject){
i++;
System.out.println( i + ": " + proj.returnName());}
your int Variable gets initalised with 1 in the for each loop, then incremented. In the next iteration, it gets initialised with 1 again, then incremented.
int i= 1;
System.out.println("Select one of the fallowing projects:");
for (Project proj : mProject){
i++;
System.out.println( i + ": " + proj.returnName());
}
This should work properly
Its basically just a different way of writing a for loop, also known as a foreach loop.
http://download.oracle.com/javase/1,5.0/docs/guide/language/foreach.html
In your code example, the variable proj gets set to the next item in the list mProject. This happens until the list has nothing left to iterate through.
The variable i is local to the for loop, so it is being recreated each time, as others have said. In other words, every iteration of this loop will make i = 2, as you can see from your output.
See that you delcare the "int i = 1; " inside the loop.
So, every time the loop "loops", i gets the value 1 again.
It should be:
int i =1;
for(Project proj: mProject) {
i++;
System.out.println( i + ": " + proj.returnName());
}
See the "int i = 1 ; " out of the loop ?
This type of loop is known as a foreach loop in Java. Basically, it's shorthand for saying, "for each item in this array/collection/iterable, do ..."
The shorthand is nice, but the downside, as you have run into, is that you don't have access to the index or iterator inside the loop when you use this form. So you should either separately track the index (like you're already trying to do, see Terrell's answer for the correct way), or use standard loop syntax, e.g. for(int i=0; i<mProject.length; i++)
for (Project proj : mProject){
System.out.println( proj.returnName() );
}
means
For each Project in the Project Set mProject, which has been defined as:
{ GregProject, MikeProject }
Print out that project's returnName. proj is re-evaluated at each step of the loop, to point to the next Project in the Set.