Advertisement
KahnClifford

Untitled

Dec 7th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class createStringOnFile {
  4.     static String[] name1 = {"ahmed","Mohamed","Ali"};
  5.     static String[] name2 = {"Ali","momen","Sena"};
  6.     static String[] name3 = {"Ramy","Yasser","Mariem"};
  7.     static String[] filenames = {"file1","file2","file3"};
  8.     static String[][] names = {name1,name2,name3};
  9.     static Formatter writer;
  10.    
  11.     public static void main(String args[]) {       
  12.         CreateFiles(filenames);
  13.     }
  14.  
  15.  
  16.     public static void CreateFiles(String[] filename) {
  17.         for(int x =0;x<filename.length;x++) {
  18.             try {
  19.                 writer = new Formatter(filename[x]+".txt");
  20.                 WriteToFile(names);
  21.             }catch(Exception e) {
  22.                 System.out.println("Error :"+e);
  23.             }
  24.         }
  25.     }
  26.    
  27.     public static void WriteToFile(String[][] names) {
  28.         try {
  29.             for(int x=0;x<names.length;x++) { // creates variable x and assign it to value 0; if x is less than name.length(the length of String[][] names which is 3) then go to next loop; increment x by 1 after the second for loop.
  30.  
  31.                 for(int y=0;y<names[x].length;y++) { // creates variable y and assign it to value 0; if y is less than name[x].length(the length of String[][] names with index x(0,1,2) which is 3) then go to next loop; incremenent y by 1 after the after the loop.
  32.  
  33.                     writer.format("%s "+(y == names[x].length-1 ? "%n" : ""), names[x][y]);
  34.                 }
  35.             }
  36.             writer.close();
  37.         }catch(Exception e) {
  38.             System.out.println("Error writing to file: "+e);
  39.         }
  40.     }
  41.    
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement