shawon_majid

fileIOJAVA

Jul 19th, 2022 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.shawon.strings;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.util.Scanner;
  11.  
  12.  
  13. /**
  14.  *
  15.  * @author shawonmajid
  16.  */
  17. public class StringTest {
  18.     public static void main(String[] args) {
  19.        
  20.         Scanner in = null;
  21.        
  22.         try {
  23.             File input = new File("input.txt");            
  24.             in = new Scanner(input);
  25.         } catch (FileNotFoundException ex) {
  26.             System.out.println("Couldn't Open file");
  27.         }
  28.        
  29.         int ans = 0;
  30.         while(in.hasNext()){
  31.             String cur = in.next();
  32.             if(!cur.equals("end")){
  33.                 ans += Integer.parseInt(cur);
  34.             }
  35.         }
  36.        
  37.         System.out.println(ans);
  38.  
  39.     }
  40. }
  41.  
Add Comment
Please, Sign In to add comment