Java - For Loop instantly meeting requirements [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to populate a deck of cards, and I've come to an odd problem. I assume it has a simple solution but I can't find it for the life of me. Any help would be appreciated.
What's basically happening is, it enters the For loop, and the initialized int is instantly the size of the final required size. Even though it is initialized at 0.
/* Create Deck */
String[] deck = new String[52];
int y, z, i = 0;
for (z = 0; z < 3; z++); // Loop while fills deck array
{
for (y = 0; y < 12; y++);
{
deck[i] = CreateCard(y, z); //Trigger CreateCard method for each card
System.out.println(deck[i]);
i++;
}
}
When the code enters the line deck[i] = CreateCard(y, z);
The values of z is 3 and y is 12. Why does this not run through the entire deck?

You have semi column at the end of the for loop declaration. And you know when we have a semi column at the end of the loop, the body of the loop does not runs with each iteration and when the condition of for loop terminates then the body of the loop executes only once.
Corrected Code
/* Create Deck */
String[] deck = new String[52];
int y, z, i = 0;
for (z = 0; z < 3; z++) // Loop while fills deck array
{
for (y = 0; y < 12; y++)
{
deck[i] = CreateCard(y, z); //Trigger CreateCard method for each card
System.out.println(deck[i]);
i++;
}
}

Related

How to print out the odd index elements from a two dimensional array (java)? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
For any given dimension (here I just set it to 8 to make it easy) I want to print out the letters with odd indexes. So I am trying to get an output like: B D F H... and other letters depending on the dimension). I put a while loop to make the row 0 since I only want to print out the the odd letters on the first row and then inside the while loop I added a for loop to print out the columns (letters) with odd n. However I am getting a not an error:
error: not a statement
for (n = 1; n<dimension; n +2){
^
I am also unsure of where to put the loops to print out the odd letters.
This is my code so far:
public static void main(String[] args) {
int dimension = 8; // normally any given dimension (int dimension = Integer.parseInt(args[0]))
int n = dimension - 1;
int m = dimension -1;
char [] alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int [] nums = new int [dimension];
for (int i = 0; i < dimension; i ++) {
nums [i] = i + 1;
}
int [] [] position = new int [dimension] [dimension];
for (int row = 0; row < dimension; row ++) {
for (int col = 0; col < dimension; col ++) {
position [row] [col] = alphabet[col] + nums[row];
}
}
char p = (char)(position[m][n] - nums[n]);
while (m == 0) {
for (n = 1; n<dimension; n +2){
System.out.println(p); //odd letters on the first row
}
}
}
}
Edit: the program is compiling but the loop is not working so I am not getting any output. How can I fix it?
This is a simple syntax error.
for (n = 1; n<dimension; n +2){
Should be:
for (n = 1; n<dimension; n += 2){
The final part of the for statement is an operation that can be used to change the iterator (or do other operations). If you consider the following line of code:
n +2;
This is not a valid statement by itself. However, the following statement is valid:
n += 2;

I have a loop that displays 10 values: 1.0, 1.1,1.2, etc. how can I put those values into an array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a loop that displays 10 values: 1.0, 1.1, 1.2, etc.:
int i = 0;
int x = 10;
for(i;i>x;i++)
{
System.out.println(x);
}
but instead of displaying the values, I want to put them in an array. How do I do that?
How about:
// You want x ints.
int x = 10;
// Make an array big enough to hold x ints.
int[] array = new int[x];
// Loop x times.
for(int i = 0; i < x; i++) {
// Put the next number into the array.
array[i] = i;
}
first your way in writing for loop is need to be more clean
it should :
for(int i=0; i > x; i++){
System.out.println(x);
}
second your boolean condition in for loop isn't true because x=10 is always bigger than i=0 so it won't print any thing.
third to put the values in array :
simply define array : int[] numbers = new int[size of array];
then put each value inside the index i of array :
numbers[i] = i;
finally for loop will be like:
for(int i=0; i < x; i++){
numbers[i] = i;
}

Object not showing up anymore, I don't know why [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I had the enemy as a array[] but I recently changed it to an arrayList[] but for some reason nothing shows up anymore, any ideas?
ArrayList<Character> enemy = new ArrayList<Character>(20);
Character enty;
String[] eny = {"Eny_.png" , "eny2.png", "eny3.png"};
for (int i = 0; i < enemy.size(); i ++) {
int ranX = 700 + (int)(Math.random() * 2000);
int ranY = 100 + (int)(Math.random() * 360);
int randE = (int)(Math.random() * 2);
enty = new Character(ranX, ranY,0,0,100,25);
enty.setImage(eny[randE]);
enemy.add(i, enty);
}
for (int i = 0; i < enemy.size(); i ++) {
int ranSpeed = -1 + (int)(Math.random() * -2);
System.out.println(ranSpeed);
enemy.get(i).setVelX(ranSpeed);
enemy.get(i).getVelX();
repaint();
}
}
Because the 20 in
ArrayList<Character> enemy = new ArrayList<Character>(20)
Is the initial capacity, not the initial size. So,
for (int i = 0; i < enemy.size(); i ++) {
is not doing anything because enemy.size() is zero.
The constructor new ArrayList<Character>(20) does not create an ArrayList of size 20, it constructs an empty ArrayList with an initial capacity of 20. So, in your first loop, enemy.size() equals 0.
If you want to add 20 enemies, just use the constant 20 in your loop.

for loop is skipping to the end? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am fairly new to Java, but my for loop is instantly skipping to the highest possible value in the following code:
System.out.println(i);
for(i = 0; i <= (difficulty - 2); i++);{
System.out.println(i);
nextMineX = (int) (10*Math.random());
nextMineY = (int) (10*Math.random());
for(y = 0; y <= 14; y++){
System.out.println(y);
if(nextMineX == minesX[y] && nextMineY == minesY[y]){
i = i-1;
} else{
minesX[i] = nextMineX;
minesY[i] = nextMineY;
}
}
}
The first for loop is screwing up, while the nested one is running fine. the variable i is initialized as 0, and difficulty is at 16. the output of this excerpt is as follows:
0
14
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
If anyone can help me with his that would be extremely appreciated. Since I'm new, it is probably something small and basic that I'm overlooking.
The problem is the semicolon at the end of the second line. It's valid to have a for loop with no body. It's also valid to have standalone a block of code inside brackets (this defines a scope for variables--if you defined a variable inside the brackets, it wouldn't be available outside). So Java is interpreting the beginning of your code like this:
for(i = 0; i <= (difficulty - 2); i++); // for loop is done, so i = difficulty - 2
{
System.out.println(i);
...
Your for loop statement ends with the semicolon you have
for(i = 0; i <= (difficulty - 2); i++); <- incorrect
for(i = 0; i <= (difficulty - 2); i++){ <- correct
//body
}
For loop is terminating cause of semicolon ;
for(i = 0; i <= (difficulty - 2); i++); //semicolon terminating loop
{...}
so you should use for loop like this
for(i = 0; i <= (difficulty - 2); i++) //remove semicolon prevent to terminate
{...}

if function not working on my program [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
my code is trying to find same 3 letter on gene[] and print corresponding array on trait[]. but the "if functions tries to check if the whole array input is divisible by 3".. need help why my "if" function is not printing!!!
public class HelloWorld {
public static void main(String[] args) {
//args example: TTTGGTGGGTTC
String[] gene = {"TTT","TTC"........GGC","GGA","GGG"};
char[] trait = {'F','F','V','V'......,'E','G','G','G','G'};
String input = args[0];
int dome = input.length();
int x = 0;
int z = 2;
int b = 0;
for (int c = 0 ; c <= dome/3 ; c++){
String top = input.substring(x, z+1);
while (!top.equals(gene[b]) ){
b = b + 1;
}
System.out.print(trait[b] + " ");
x = x + 3;
z = z + 3;
b = 0;
}
if ( dome%3 == 0){
System.out.print("no excess ");
}else{
System.out.print("*");
}
}
}
I think your for loop runs one index too many. Instead of
for (int c = 0 ; c <= dome/3 ; c++)
try
for (int c = 0 ; c < dome/3 ; c++)
If your if/else part is not working then,there might be an exception is thrown from inside For loop.
Because the for loop updates nothing,by which the if/else part are affected.

Categories

Resources