Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to shorten my code using a loop. I have for example 5 zombies in my game. So I thought I could do this
Image zombie;
for(int i = 0; i < 5; i++){
if (zombie.getZombieRect().intersects(zombie + i + .getZombieRect())) {
}}
Why can this not be done? adding i to the end of zombie. zombie being an image. The oother variables are zombie1, zombie2 etc.
Thanks for all help.
This is what arrays are for:
Zombie zombies[] = {zombie, zombie1, zombie2, zombie3, zombie4};
for (int i = 0; i < zombies.length; i++) {
if (zombie.getZombieRect().intersects(zombies[i].getZombieRect())) {
}
}
Make an Array of Objects and then u can call them by using zombie[i] etc whatever you want to do.
The thing of adding you are trying to do is suitable in case of strings only
"zombie"+i;
etc.
To answer the question,
zombie + i
Is a compile-time error because java does not allow an Image object to be used in combination with an int in the '+' operator.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 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
Can you please help me make the following code work?
for(int a=0, b=0; a<101; b<102; a++; b++;) {
stuff
}
You got the initialization (first) part of the loop right.
The termination or condition (second) part of the loop should be evaluated to a boolean, so assuming you require an AND relation between the conditions on a and b, it becomes a<101 && b<102. You might want || (OR) instead, depending on your logic.
The increment (third) part of the loop should contain comma separated expressions (same as the initialization part which you already got right).
I also removed an extra ';' from the end.
for(int a=0, b=0; a<101 && b<102; a++, b++) { stuff }
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to android and i just want to create a dj player. but for that the starting step is mixing two files. The rough code for that i found on the following link but in that i did not understand how to code for buildShortArray(music1).
I already tried this code but got stuck in the above mentioned method's code.
thank you in advance for help.
Docs here:Mix two files audio wav on android use short array
The code from the link does not show the buildShortArray method.
You need to transform List<Short> into array short[]:
List<Short> music1 = ...;
short[] arrayMusic1 = buildShortArray(music1);
You could write the method buildShortArray like this:
public short[] buildShortArray(List<Short> list) {
short[] array = new short[list.size()];
for(int i = 0; i < list.size(); i++) {
array[i] = list.get(i);
}
return array;
}
However, i'd like to warn you, copy-pasting code is never a good idea.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to crate a loop which sums the arrays values which are within the interval (for instance 2-5). My main problem is getting from the first checked value of the array to the next one and so on. Thankyou in advance.
int x=0,y=0,s=0;
int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };
Scanner scan = new Scanner(System.in);
int b = scan.nextInt();//lowest value
int c = scan.nextInt(); //highest value
if (myArray[x][y]>b || myArray[x][y]<c)
s=s+myArray[x][y]
//then check next one
if (myArray[x][y]>b || myArray[x][y]<c)
s=s+myArray[x][y]
You need to put these in a nested for-loop. Otherwise, they are executed only once, with x=0 and y=0. So all you are doing is essentially:
if (myArray[0][0]>b || myArray[0][0]<c)
s=s+myArray[0][0]
So s could only possibly be 0 or the first element in the 2D array.
Now, this conditional:
if (myArray[x][y]>b || myArray[x][y]<c)
does not mean what you think it means. This would evaluate to true for all numbers as long as b is less than c. For the semantics you are looking for, you want an AND operator (&&).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
int i=0;
while(!a.isEmpty()) {
if(a.toCharArray()[i]==' ') {
System.out.println(a.toCharArray()[i]);
}
i++;
}
When I run this program it's giving me an error: index out of bounds. How can I solve it?
You're not changing a in the loop, so a.isEmpty() will not change and you'll continue looping until i goes out of bounds. Did you mean:
while (i < a.length()) {
And as an aside, a.toCharArray()[i] can (and should) simply be a.charAt(i) (as #MartijnCourteaux also points out).
There are already a couple of answers that give solutions to the problem. This answer is about how you could have found the problem yourself.
Most array index out of bounds errors in loops are due to incorrect loop condition, so that the condition goes on being true even after the index has reached the end of the loop. Your loop condition did not depend on i, so the only way it could change from true to false is if a were changing inside the loop.
If you expected a to change, you should have put System.out.println(a) inside the loop, and monitored the value of a. You would have seen it not change.
Alternatively, if the isEmpty test were a typo, and you meant to test something that depends on i, desk checking your loop condition should have found it.
You should change your loop to this instead:
char[] c=a.toCharArray();
for(i=0; i<c.length; i++){
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Basically, I have to write a method that determines whether all the characters in a string are the same, using only library String methods.
Use matches. Please read up the documentation of Pattern class to understand how to use matches function to check your input. It is quite useless for me to give you a straight answer, since you don't learn much from it.
That method will give you one-liner solution after you have understood regex.
public static void main(String[] args) {
String str = "aaaaa";
int count = str.length() - str.replaceAll(String.valueOf(str.charAt(0)), "").length();
if(count == str.length())
System.out.println("All characters in a given String are same");
}
Try this : This is the simplest and best solution for this kind of problem.
One way to solve this is without looping or recursion is by [ab]using String.replace1.
The actual implementation is left as an exercise.
1 It does not require that your code loops, but something must loop.