First I would like to state that I am almost positive that this is a bug in Java, not a coding mistake by me.

This program is designed to print the first n (inputted) square triangular numbers (STN's). A STN is a number that is both a perfect square and a triangular number. You find triangular numbers by going: 1+2+3+4 etc. The first trangular numbers are: 1,3,6,10 and the first STN are: 1,36,1225 etc. I noticed that my program does print all the STN's, but starts failing when the values get higher. The part that's failing is the part that checks if a perfect square is a STN. Consider the following code:

```
while(x > total){ //Will run until total == x || total < x.
incrementer++; //Increments total with a higher number each time
total += incrementer; /* Total is always triangular numbers. x has to match total for it to be a square triangular number.*/
//System.out.println(total) //For troubleshooting, disabled
}
```

The first values of incrementer and total will be

incrementer: 1,2,3,4,5

total: 1,3,6,10

The whole program outputs the first STN's correctly, but starts failing when the numbers gets high. Here is the outputprinting 10 STN's:

Square triangular number # 1: 1

Square triangular number # 2: 36

Square triangular number # 3: 1225

Square triangular number # 4: 41616

Square triangular number # 5: 1413721

Square triangular number # 6: 18705624

Square triangular number # 7: 43099224

All the STN's from #6 and up is incorrect.

So, for the troubleshoot part. I have figured out that everything works as it should, except the while loop above, which starts failing after STN #5. So I made another program to check if and where this while loop is failing. Everything that is printed by`System.out.println(total)`

is read into this other program. In this program, each value is subtracted by the previous value. By doing this, we are finding out how much total in *incremented* by for each time it runs through the while loop. The result should be 1,2,3,4,5,6 and so on. It is correct for the first couple of thousand values, but it starts failing at 5792.0. Here is a copy from the output in the area where it starts failing:

5798.0

5796.0

5794.0

5796.0

5792.0

5792.0

5791.0

5790.0

5789.0

5788.0

As you can see, everything is going smoothly until it reaches 5792.0. Everything described so far is using floats. If I try with longs, it starts failing at STN #5, and turns negative at STN #9. For doubles and its, it works actually until #7, but then they reach their maximum value.

So my question to you guys is: Why is the compiler failing here? And can I code this in a different way to avoid the bug in the compiler?

In case you want to see my code, I will paste it here. Program number one is the one actually finding the STN's, while program number 2 is just for troubleshooting (finding out if/when/how the while loop is failing by printing how much total is incremented with for each time)

#1:

```
import java.util.*;
import java.lang.*;
import java.math.*;
public class FunLoops {
long x;
int a = 0;
long b = 1;
float incrementer = 0;
long total = 0;
Scanner in;
public FunLoops(){
in = new Scanner(System.in);
loops();
// lcm();
}
public void loops(){
Scanner in = new Scanner(System.in);
System.out.println("How many square triangular numbers do you want?");
int input = in.nextInt();
System.out.println();
/*if(input > 8){ //Limits square triangular numbers to 8, disabled.
input = 8;
System.out.println("Will print 8 of them");
}*/
while(a < input){ //Will run until desired number of square triangular numbers is found
// System.out.println("Power of: " + b); //For troubleshooting, disabled
x = b*b; //x will only be perfect squares of b.
b++;
while(x > total){ //will run until total == x || total < x.
incrementer++; //Increments total with a high number each time
total += incrementer; // Total has to match x at one point for it to be a perfect square.
//System.out.println(total) //For troubleshooting, disabled
}
if (total == x){ // Checks if perfect square IS a square triangular numbers.
a++; // stops the first while loop when it has reached input.
System.out.println(" Square triangular number # " + a + ": " + (int)x); // prints all square triangular numbers
}
} // End of first while loop
System.out.println();
} // end of loops()
```

# 2:

```
import java.io.*;
import java.util.*;
public class Store {
private ArrayList <Item> myStore = new ArrayList <Item>();
Scanner in;
public Store(){
readFile();
}
public void readFile(){
try{
float myNums;
in = new Scanner(new File("abc.txt")); //abc contains all the printed values of total in program #1
while(in.hasNextInt()){
myNums = in.nextFloat();
myStore.add(new Item(myNums));
}}catch(Exception e){
System.out.print(" Error: " + e.getMessage());
}
for(int i = 0; i < myStore.size()-1; i++){
float check1 = myStore.get(i+1).getNums()-myStore.get(i).getNums() //Prints every value - the prev.
System.out.println(check1);
}
}
}
```

______________________________________________________________________________________________

```
public class Item {
public float myNums;
public Item(float nums){
myNums = nums;
}
public float getNums(){
return myNums;
}
}
```

PS: I almost finished writing this, and then google chrome CRASHED(!) and i had to start over. Not fun.

*Edited
by joankim*