Advertisement
green1ant

2_1 java

Oct 18th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.    private static int inputN(){
  8.       int n;
  9.       boolean isCorrect;
  10.       n = 0;
  11.  
  12.       isCorrect = false;
  13.       Scanner sc = new Scanner(System.in);
  14.  
  15.       while(!isCorrect){
  16.          System.out.println("Enter n - amount of days in month");
  17.          n = sc.nextInt();
  18.          if(n < 28 || n > 31){
  19.             System.out.println("Incorrect input N");
  20.          }else{
  21.             isCorrect = true;
  22.          }
  23.       }
  24.       sc.close();
  25.       return n;
  26.    }
  27.  
  28.    private static int[] inputFromFile(int n){
  29.       String pathname = "";
  30.       int counter;
  31.       counter = 0;
  32.       int[] days;
  33.       days = new int[n];
  34.  
  35.       Scanner in = null;
  36.       try {
  37.          in = new Scanner(new File("input.txt"));
  38.       } catch (FileNotFoundException e) {
  39.          e.printStackTrace();
  40.       }
  41.       for(int i = 0; i < n; i++){
  42.          days[i] = in.nextInt();
  43.          if(days[i] < 0){
  44.             counter++;
  45.          }
  46.  
  47.       }
  48.  
  49.  
  50.       for(int i = 0; i < n; i++){
  51.          System.out.print(days[i] + ' ');
  52.       }
  53.  
  54.       in.close();
  55.       System.out.println("COUNTER = " + counter);
  56.       return days;
  57.    }
  58.  
  59.    public static void main(String[] args) throws FileNotFoundException {
  60.       int counter, n;
  61.  
  62.       counter = 0;
  63.       n = 0;
  64.       int[] days;
  65.  
  66.       System.out.println("This program...");
  67.  
  68.  
  69.       n = inputN();
  70.  
  71.       days = new int[n];
  72.  
  73.  
  74.       days = inputFromFile(n);
  75.  
  76.       System.out.println("There are " + counter + " days with temperature < 0 degrees");
  77.    }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement