I have this issue where my code runs fine on the latest version of java but not on my college computers because they're on an older Java API (not that old but not the latest version). Here is the code and my task:
'Write a program that takes as input a number n and a text string filename and writes n lines to the file where each line is of the form: i: sqrt(i) sqrt(i) sqrt(i). The first column ranges from 1..n whilst the first square root is to one decimal place, the second is to two decimal places and the third is to three decimal places.'
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CSLab5 {
public static void writeFile(final String filePath, final int n) throws IOException {
String path = filePath.concat("file.txt");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) {
final String format = "%-10s %-10.1f %-10.2f %-10.3f %n";
for(int i=1;i<=n;i++){
final double root = Math.sqrt(i);
writer.write(String.format(format,i+":",root ,root,root));
}
}
}
}
I am not sure how I can modify the code to compliment the code for the older version 1.6 I believe. I get errors on the try statement. thanks