How to make program output differently depending on number of runs? - java

This is not like a code help or code review thing.I am trying to make a counter for my program that can define the output of my program.For eg: If i want my program do specific thing after it has been run two times or else it should do another thing.Another example of what i am trying to say is that when i run my program first and second time it should print 3,but when i run it third time it should print 5.
I believe there are many ways already out there,but i don't know how to search such type of specific query on google.
I am working on complex program,where such type of thing is required,but i don't know where and how to start or look.I tried to make a counter,but every time the program runs,it initializes the value of counter and so it doesn't increment.

You have to maintain a state of the each time it runs. It means in the program, maintain a counter and store it in a file. You can do it in the following way.
If there is running state counter stored in a file, it means the application is run first time.
Before running the application, read the file which maintains running counter.
Based upon the running counter number like even , odd etc, you can write the logic.

You have to save the value in a file or some other storage method that persists after the program runs and ends.
Every time the program runs:
If there's no file, then create one and write the number 1 in it to show that the program has run once.
If there is already a file when you run the program, then read the number stored in it and interpret it as the number of previous runs. Write a number to the file that is 1 higher than the number of previous runs.

Related

Is there a way to run a program in Eclipse without it creating certain variables again every time I run the program?

Basically, I would like to create a "library" of sorts in another class that is created when I run the program's main class the first time. But instead of repeating the action when I click run for a second time, I want the manipulated library in the first run to be initialized. For example if I have a static array with capacity 5 and put the int 1 in index 0, the next time I run the entire thing, I want to start with a static array with 1 in index 0. Is this even possible (in Eclipse) and if so, how can I do this?
All of your program's variables and other state are discarded when it is terminated and reinitialized when it is launched again. (This is a common cause of bugs in Android apps, which are frequently discarded and then re-opened.) To save state between program runs, you need to store it in some place that's intended for this. Common choices include files, databases (H2 with a file-backed database might be a good option), Android SharedPreferences, servers, and more exotic tools such as prevalent systems.

Selenium/Java program speeding up overtime

I have written a Java program using Selenium HTMLUnitDriver. It consists of a few methods which basically first scrape a site for some elements, then interact with these elements, and then finally fills in and submits a form. I run these few methods a number of times and record the time between the beginning of the first method and the end of the last method each time. The run time seems to go down overtime. Why is this?

How to compare all the variables in two different Eclipse sessions

I am presently trying to find a bug in a long java code. This code reads an input text file and it runs many CFD simulations and then it collects results and analyzes them. In the input file there is a flag that only changes the order in which that bunch of completely independent threads is run. However, if I change this flag I have that the java program crashes after all the simulations are run, while it should actually behave the same. In order to debug this, I opened two different Eclipse sessions, and in each session I run the code. The only difference between the 2 sessions is the value of that flag I mentioned above. I have set up a breakpoint in a line after all the 37 threads (i.e. simulations) are run and results saved on file. That line is in a subroutine where only the main thread is active (all the other threads stopped) and it collects and elaborate further the results of the 37 simulations. I would like to have a way to quickly compare all the variables (in the present scope) of one Eclipse session with the other, in order to find possible differences. Saving them in a text file and comparing the 2 text files would make the trick for me. Do you know how I can do it, if even possible at all? Any other working solutions is also welcomed.

ATM program that loses data every execution

I am working on an ATM program. I have everything working and everything coded correctly. I press execute and can deposit/withdraw funds and have it be reflected with proper messages. When I hit execute again, everything is reset(account balances) and it shows nothing was withdrawn or deposited. How to I fix this? I was looking on here and thought maybe if I serialized some classes or something, it might work. It didn't.What can I do? When I hit execute, I want all of my deposits and withdrawals to still be effective. not lost.
You could make a menu-driven program where you can see all the changes you need from your program, but this only exist for the lifetime of the running program, once you restart the program you will sure loose the all previous content, If you don't want to loose the data after every execution you need a consistent storage like a database or file where you can read n write or update each time you run it

Java program that executes only when (non-user) input is received via a file

as part of my research project, I have written a Java search program that implements simulated annealing. However, this search doesn't only occur in the Java program as I am supposed to compute the overall cost function using another program written in C. So basically the search routine would be like this:
Example cost function: minimize {A + B}
where the first part of the expression, A, would be done in my program and the second part, B, would be the regularization term computed from a C program. I know it seems really strange that I have to do this but I don't have the time/resources/proper background to re-implement that regularization program as part of my Java program. Basically I am trying to search for the optimum vector of values through a two-part search.
I wish to output the current solution (as a formatted file which the C program understands) from Java and feed it to the C program to do its regularization (I have done this part). This is where I'm stuck at: While the C program is running, I wish to make my Java program wait until it receives input (in the form of a file) back from the C program. So basically my program gives input to the other program and then stops its execution (for the time being) until that C program has done its job and returned a file of values for re computation of values to be used in the next iteration of the annealing search.
I'm guessing I should use some sort of thread methods like wait and notify but I'm really not sure. It would be pretty straightforward if the program depended on user input. But the program, as it stands, executes only upon receiving the file input.
Please advise! And I apologise in advance if this question is verbose and confusing. I am wiling to clarify any questions. Thank You.
Basically this:
java runtime.getruntime() getting output from executing a command line program
When you're ready for the C part, you spawn the Process via Runtime.exec(), open the input and output stream, feed in your data in the format it recognizes, and then read back the eventual result from it's output stream and parse it.

Categories