Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.io.UnsupportedEncodingException;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class prog1 {
  10.  
  11. public static int fib(int n) {
  12. if (n <= 2) {
  13. return 1;
  14. } else {
  15. return fib(n - 1) + fib(n - 2);
  16. }
  17. }
  18.  
  19. public static void programm1(String fileName) throws IOException {
  20. //Reads numbers from file
  21. int[] numbrid = new int[4];
  22. int i = 0;
  23. File file = new File(fileName+".in");
  24. Scanner scanner = new Scanner(file);
  25.  
  26. while (scanner.hasNextLine()) {
  27. int line = Integer.parseInt(scanner.nextLine());
  28. numbrid[i] = line;
  29. i++;
  30. }
  31.  
  32. scanner.close();
  33.  
  34. //prints out numbers
  35. for (int k = 0; k < numbrid.length; k++) {
  36. System.out.println(numbrid[k]);
  37. }
  38.  
  39. //Writes the Fibonacci numbers into the file
  40. File file2 = new File(fileName + ".out");
  41. PrintWriter writer = new PrintWriter(file2, "UTF-8");
  42. // if file doesn't exist, then creates it
  43. if (!file2.exists()) {
  44. file2.createNewFile();
  45. }
  46.  
  47. for (int n = 0; n < numbrid.length; n++) {
  48. writer.println(fib(numbrid[n]));
  49. }
  50. writer.close();
  51. System.out.println("done");
  52. }
  53.  
  54.  
  55. public static void main(String[] args) throws IOException {
  56. String a = "numbrid";
  57. programm1(a);
  58. }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement