Changing Filenames(Strings) through loops - Java - java

I want to change images at a specific location, for the purpose of animation. Can I create a loop with for change in file names. File names are like Sprite1.png, Sprite2.png ...... Sprite10.png.
for(i = 1; i <= 10; i++)
{
Display("Sprite(i).png")
}
Is something like this possible? How?

Lets say there are n files. you can achieve desired output by using the following piece of code
for(i=1;i<=n;i++){
Display("Sprite"+i+".png")
}

Related

ArrayList with formatting

Struggling with my ArrayList code.
I needed to generate 20 random numbers between 1 and 200.
It works but when I show all in array list it, it all comes out horizontal
(ie: [190.9873874849,3.45694033,67.900034...] and SUPER long. It needs to have each number print out in a list format, with the only four digits after the decimal. (%10.4)
I tried this:
System.out.printf("%10.4", num[0]);
which works for the formatting but I can't seem to get the whole arraylist to work.
My array is this:
ArrayList<Number> num = new ArrayList<Number>();
for (int i = 0; i<20; i++) {
num.add(Math.random() *200 +1);
}
It works like I said for getting and listing the numbers -- but not in the "correct" way.
I also have to save the numbers to a file, and be able to let the user choose an index for them and tell them their number they chose from the file, which I am also struggling with.
Any help would be amazing!!!
[EDIT]
This worked (from Ole V.V.):
for (int i = 0; i<20; i++) {
num.add(Math.random() *200 +1);
System.out.printf("%10.4f%n", num.get(i));
[ADDITIONAL]
**I have to post here and can't make a new question because some jerk downvoted me? **
I have all of the numbers in my array read in and saved to a text file (with the right formatting!) but now I have to be able to ask the user for an index, and be able to find the object according to that index... but it isn't working!
I don't know how to assign indexes to variables when the numbers are no longer in array format (they're in a column similar to what is in the column).
Here are a couple of options. First, you may use a for loop like the one you already have:
for (int i = 0; i < 20; i++) {
System.out.printf("%10.4f%n", num.get(i));
}
A terser option is:
num.forEach(n -> System.out.printf("%10.4f%n", n));
In both case the output goes like
186.8143
129.4201
169.7405
...
Your format string, %10.4, was almost correct. You need to add f for formatting a floating-point number. I also added %n for a line break after the number. In Java num[i] works only for arrays, not for ArrayList, you need num.get(i).
You need to trim the extra digits by this simple trick.
double number = Math.random()*200 + 1;
number = (long)( number * 10000 );
number = number / 10000;

Figuring out if statement

So my project is about creating a table that adds statistic comparisons to a selection sort table. I've done the bulk of the work but am stuck at this one part that asks me to It should add "comparisons" to "totalComparisons". It should compare "comparisons" to "minComparisons" and set "minComparisons" to whichever is
smaller. It should also set "maxComparisons" in an analogous fashion. The words in quotes are variables. I know I need to write an if statement but I have no idea how to write it. Can somebody show me how to do the part where you compare "comparisons" to "minComparisons" and set "minComparisons" to whichever is
smaller.
My code so far:
private static void endStatistics(){
totalComparisons += comparisons;
if (comparisons ){
}
thanks for any help.
Try this:
if(comparisons < minComparisons)
minComparisons = comparison;
if (comparisons <= minComparisons ){
minComparisons = comparisons;
}
if (minComparisons <= comparisons ){
minComparisons = minComparisons;
}
You don't actually need the second if statement though, just to put it here to show you the whole picture.

Printing to Console Does Nothing in For Loops

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!

For loop counter in dot operator

I'm working in the Android ADT plugin for Eclipse.
What I try to do is this:
I have some drawables named from 0 to 150 and i want to get them into an array to use them.
As far as I know, to be usable, they have to be assigned with
"R.drawable.FilenameWithoutExtension".
To make the array creation easy and fast I want to use a for loop:
for (int i = 0; i <= 150; i++) {
imagesArray[i] = R.drawable.i;
}
But for now I get an error for that "R.drawable.i" because there is no file "i".
Is there a way to use the numeric value of "i" instead of the letter for the assignement "R.drawble."?
You can get a resource identifer from its name using the getIdentifier() method:
for (int i = 0; i <= 150; i++)
imagesArray[i] = getResources().getIdentifier(String.valueOf(i), "drawable", "your.package.name");
The file names must start with a letter because at build time the file names are converted to java identifiers.
After you do that you can use reflection access each of the members of R.drawable.

Java school project random error

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.

Categories