Android: Database Fields not populating through a loop - java

I am currently trying to make a function to prepopulate fields and determine if they have been set up or not. I need to add a total of 46 entries into my database so I figured a for loop would come in handy:
public void prepopulate() {
Locker setup = new Locker(this);
for (int x = 46; x < 47; x++) {
setup.open();
String lockerNumber = "Locker" + x;
setup.createLockerEntry(lockerNumber, 0);
setup.close();
}
I
Any ideas, maybe a for loop wont work?

if you want to add 46 entries the why are you using for (int x = 46; x < 47; x++). This will work only for one entry.
Just change you for loop like
for (int x = 0; x < 46; x++) //or any appropriate condition

Related

Extracting one portion of 3D array

Apologies for what is probably a simple question but I am just a beginner and trying to learn what I can of Java.
I currently have a 3D array (array[][][]). What I want to do is loop through the array and save the third value to a variable.
for (int x = 0; x < 100; x++) {
for (int y = 0; y < 100; y++) {
(array[x][y][value]);
int savedValue = value;
I guess I have something very small wrong but would be very grateful if somebody could help me out.

How to print only the last element of an array?

I am trying to print the last element of my array. The code can be seen below:
double [] results = new double[21];
double t = 9600;
for(int y = 0; y < 21; y++) {
results[y] = t;
t *= 1.04;
System.out.println(results[results.length - 1]);
}
However, when I attempt to run this, I get this result:
0.0 (printed 20 times in a row)
...
21034.782173120842
I do not know why it is printing out 20 zero's, and then the answer I want (21034.78). I thought that by doing results[results.length - 1], only the last element of the array would be printed. I have a suspicion that this has to do with the loop, but I do not know why or how to fix it.
Any help or advice would be greatly appreciated. Thank you!
You need to put the System.out.println outside the for loop, or else you will always print 0.0 because the last index of the array isn't filled yet.
double [] results = new double[21];
double t = 9600;
for(int y = 0; y < 21; y++) {
results[y] = t;
t *= 1.04;
}
System.out.println(results[results.length - 1]);
Output: 21034.782173120842
put your System.out.println , out of loop.
for(int y = 0; y < 21; y++) {
** YOUR LOGIC **
}
System.out.println(results[results.length - 1]);
You need to move the print statement outside the loop..
double [] results = new double[21]; double t = 9600;
for(int y = 0; y < 21; y++) {
results[y] = t;
t *= 1.04;
}
System.out.println(results[results.length - 1]);
You need to make a slight alteration. Here is one thing that you can do:
double [] results = new double[21];
double t = 9600;
for(int y = 0; y < results.length; y++) {
results[y] = t;
t *= 1.04;
System.out.println(results[y]);
}
You can print the current index [y] each time through the loop. Or else you're always printing index 21 which isn't filled yet and will repeatedly print 0 until it is filled. The current iteration of the loop [y] will always be the last index that actually has a value in it, but the last index won't actually be filled with a value until your last iteration through the loop which explains your error here.

Why is it allowed to declare a variable in a for loop?

I'm a student currently learning java at school (Beginner) and I was wondering about something.
I have a basic knowledge of coding from other languages and I don't understand a particular thing in Java.
If I were to declare a variable (let's use an int as a example) inside a loop wouldn't that mean that I'm declaring the same variable over and over?
This is what I mean:
for (int i = 0; i < 3; i ++) {
int x = 5;
}
Isn't it the same thing as this? (This one is incorrect)
int x = 5;
int x = 5;
If not, Why? Both of them are / declare the same variable twice, though I know that in loops the variable is local and can't be used outside of the loop (I don't think thats the issue though).
I also know that you can't declare the same variable twice so I don't understand how the first example is legal.
Thanks so much :D
This Question has be solved, Thanks to everyone that helped :D
for (int i = 0; i < 3; i ++) {
int x = 5;
}
is actually equivalent to:
{
int x = 5;
}
{
int x = 5;
}
{
int x = 5;
}
Each x variable is declared in a separate scope.
scope is in one iteration, after end of loop, scope doesn't exist.
simple example:
for (int i = 0; i < 4; i++) {
int x = 0;
System.out.println(x);
x++;
}
output:
0
0
0
0
Lets take 1st expression
for (int i = 0; i < 3; i ++) {
int x = 5;
}
Here scope of variable x is within the loop block
So each time a new loop starts scope is already released.
So there is no error
If you change the same code to like this
for (int i = 0; i < 3; i ++) {
int x = 5;
int x = 5;
}
Now there will be an error as x is already in scope and you are again trying to define.
This is allowed
for (int i = 0; i < 3; i ++) {
int x = 5;
x = 5;
}
as you are resigning the variable
the variable x has life time that begin at when you declare it and ends at the closing block for the for-loop.
in other word x is born when you enter new iteration and dies when iteration ends (or dies at the end of block that contains it)
In each iteration of your loop, a variable x of type int is "created" and assigned the value 5. As soon as the iteration finishes, this variable is "destroyed" and by the next iteration the cycle starts again.
So there are never 2 variables by the same name in the same scope.
It is generally good practice to make the variable avayable to the smallest context possible: if you only need it inside the loop declaring it inside it is considered good practice.
For the loop declaration case, your value gets reassigned basically.
This might proove interesting to read and add further informations on the matter: Declaring variables inside or outside of a loop
This is also a legal thing to do
for (int i= 0; i < 1000000000; i++) {
int j = i & i+1 ;
}
These are legal because at the end of the loop the declared variable "ceases to exists" in order to be reformed the next execution
Well, when you declare int x = 5; inside the loop, you're declaring it in a local scope (which starts from its { to }) so the variable x will be destroyed when it gets out of its scope which means for the next loop iteration, it's a new x so that's why you can do that and since it's destroyed when it gets out of scope it can't be used/seen outside the scope.
Just to clarify the concept of allowing variable declaration in a loop, I've declared the same variable again in the loop, and got error "A local variable or function named 'x' is already defined in this scope"(I wrote this code in C#, but you should get similar message in Java):
If I declare the variable in a different scope outside the loop, there will be no error as the scope is different:
for (int i = 0; i < 3; i++)
{
int x = 5;
}
{
int x = 5;
}
If we couldn't declare variables in loop, we had to write the following code:
{
int x = 5;
// Do something with x=5
}
{
int x = 5;
// Do something with x=5
}
{
int x = 5;
// Do something with x=5
}
But with loop, we can just write the following instead of writing 3 scopes:
for (int i = 0; i < 3; i++)
{
int x = 5;
// Do something three times with x=5
}

Passing a variable into an ActionListener

Ok, so I couldn't find exactly what I needed anywhere else, so here goes.
How would I go about passing a variable into a function inside an ActionListener()? This is my code.
for(int y = 0; y < 5; y ++) {
for(int x = 0; x < 5; x ++) {
currentRect = (y * 5) + x;
mainButtons.get(currentRect).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
answerField.setText(questions.get(currentRect).getAnswer());
}
});
}
}
The error is on line six, where it underlines "currentRect", and then complains about this,
Cannot refer to a non-final variable currentRect inside an inner class defined in a different method
What is does is iterate through a group of JButtons (javax.swing) , and then assign the appropriate action listeners based on their position in the ArrayList, however, I can't seem to pass the variable storing their positions into the actionlistener itself, so I'm stuck. I realised while im writing this that I should really use a foreach, so ill change that later. (just a little note to who was gonna point that out) I realize that I cannot also make that variable final, as it is changed during the for loop. I also tried passing the value of the currentRect ((y * 5) + x) into the functions, but to no avail.
Thanks,
andrewgies17
The solution is simple, simply declare a new final variable inside of your block:
for(int y = 0; y < 5; y ++) {
for(int x = 0; x < 5; x ++) {
final int currentRect = (y * 5) + x;
or if you need the value of currentRect outside of the block:
for(int y = 0; y < 5; y ++) {
for(int x = 0; x < 5; x ++) {
currentRect = (y * 5) + x;
final int currentRect2 = currentRect;
and you use currentRect2 instead of currentRect in your anonymous class. This is counterintuitive because it looks like that the final variable is beeing re-initialised each time but in fact, you must see this as a new variable that is initialised only once for each loop of the block.
However, this is not the most intelligent way of solving this problem; because you are creating a brand new anonymous class for each button just for the purpose of storing a small value that is different between them. These can quickly add up on a limited device. A better idea would be to store the value of the currentRect as a tag for each button. This way, you can declare a single instance of the anonymous class in the outer class and reuse it for all buttons.

best way to check the indices around a certain index in a 2d array

I'm working on this bacteria life game thing I have to make.
Basically I have a 2d string array let's say 20 by 20.
What would be the best way to check all 8 spots around a certain index. Each index is suppose to represent a bacteria. For each bacteria(index) I have to check to see if any of the 8 spots around this index has another bacteria in it, if the index has a bacteria in it, it's represented simply by a "*", asterik.
What would be the best way to go about checking all 8 spots around each index, because based on what is in the indices around a certain index I have to make certain changes etc.
The only idea I have come up with is having a bunch of if statements to check all 8 spots, I was wondering if there is a better way to do this
ex:
row 1 - www , row 2 = wOw , row 3 - www ,
if I am at the O index, what would be the best way to check all the index spots around it for a certain string.
Sorry, I am not very good at explaining my problems, bad english :o.
thanks for any of the help.
so you have something like this
char[][] table = new char[20][20]
for(int i = 0; i < 20; i++) {
for(int j = 0; j < 20; j++) {
int surroundingBacteria = 0;
for(int x = max(i-1,0); x < min(20,i+1); x++) {
for(int y = max(i-1,0); y < min(20,i+1); y++) {
if(table[x][y] == '*') surroundingBacteria++;
}
}
switch(surroundingBacteria) {
// put your case logic here
}
}
}
Here is how I've accomplished this in the past:
for(int x = -1; x<=1; x++){
if ( i+x < xLength && i+x >= 0){
for(int y = -1; y<=1; y++){
if(j+y < yLength && j+y >= 0){
//logic goes here
}
}
}
}

Categories