Guest User

Untitled

a guest
Feb 15th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. import java.io.*;
  4. import static java.lang.System.*;
  5.  
  6.  
  7. class Animals{
  8.  
  9. String name;
  10. String color;
  11. int age;
  12.  
  13. Animals( String name, String color, int age ) {
  14.  
  15. this.name = name;
  16. this.color = color;
  17. this.age = age;
  18. }
  19. }
  20.  
  21. public class Program {
  22.  
  23.  
  24. public static void main ( String[] args ) throws IOException {
  25.  
  26.  
  27. ArrayList<Animals> u = new ArrayList<Animals>();
  28.  
  29. Scanner uni = new Scanner(System.in);
  30.  
  31. out.println("N = ");
  32. int n = uni.nextInt();
  33.  
  34. String nume, color;
  35. int age;
  36.  
  37. for ( int i = 0; i < n; i++ ) {
  38.  
  39. out.println("Animal number " + (i+1) );
  40. out.println("Name - ");
  41. nume = uni.next();
  42. out.println("Color - ");
  43. color = uni.next();
  44. out.println("Age - ");
  45. age = uni.nextInt();
  46. u.add(new Animals(nume,color,age));
  47. }
  48.  
  49. writing(u);
  50.  
  51. }
  52.  
  53.  
  54. public static void writing ( ArrayList<Animals> a ) throws IOException {
  55.  
  56. FileWriter uni = new FileWriter("Animals.txt");
  57.  
  58. for (Animals i : a ) {
  59. uni.write(i.name + " " + i.color + " " + i.age);
  60. uni.write("\n");
  61. uni.close();
  62. }
  63.  
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment