Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class Solution {
  8.  
  9. public class Input {
  10. private final String name;
  11. private final Integer age;
  12.  
  13. public Input(String name, Integer age) {
  14. this.name = name;
  15. this.age = age;
  16. }
  17.  
  18. public String getName() {
  19. return this.name;
  20. }
  21.  
  22. public Integer getAge() {
  23. return this.age;
  24. }
  25. }
  26.  
  27. public void readFile() throws FileNotFoundException {
  28. File file = new File("/home/vibhav/input.txt");
  29. Scanner scanner = new Scanner(file);
  30.  
  31. List<Input> inputs = new ArrayList<Solution.Input>();
  32. while(scanner.hasNextLine()) {
  33. String nextLine = scanner.nextLine();
  34. String[] splits = nextLine.split(",");
  35. String name = splits[0];
  36. Integer age = Integer.parseInt(splits[1]);
  37.  
  38. Input input = new Input(name, age);
  39. inputs.add(input);
  40. }
  41.  
  42. for (Input input: inputs) {
  43. System.out.println("Name=" + input.getName());
  44. System.out.println("Age=" + input.getAge());
  45. }
  46.  
  47. scanner.close();
  48. }
  49.  
  50. public static void main(String[] args) throws FileNotFoundException {
  51. // TODO Auto-generated method stub
  52. Solution solution = new Solution();
  53. solution.readFile();
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement