Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. /* Scrivere un programma Java che legga da un file di input alcune sequenze di valori
  2. interi e determini, per ogni sequenza, se esiste una coppia di numeri il cui prodotto
  3. รจ un numero dispari. Si supponga che il numero di elementi pe...r ogni sequenza non
  4. sia superiore a 10 e che ogni elemento della sequenza abbia un valore compreso tra
  5. 0 e 100.
  6. */
  7. import java.util.Scanner;
  8. import java.io.FileReader;
  9. import java.io.FileWriter;
  10. import java.io.BufferedReader;
  11. import java.io.BufferedWriter;
  12. public class OddProduct {
  13.     static String nome;
  14.     static String name;
  15.     public static void main (String args[])
  16.     {
  17.         boolean dispari=false;
  18.         FileReader in =null;
  19.         FileWriter out=null;
  20.         Scanner input = new Scanner(System.in);
  21.         System.out.print("Inserisci il nome del file di input: ");
  22.         name = input.next();
  23.         Scanner s = new Scanner(System.in);
  24.         System.out.print("Inserisci il nome del file di output: ");
  25.         nome = s.next();
  26.         BufferedReader br=null;
  27.         BufferedWriter bw=null;
  28.         String [] str1=null;
  29.         try
  30.         {
  31.             in=new FileReader(name);
  32.             out=new FileWriter(nome);
  33.             br=new BufferedReader(in);
  34.             bw=new BufferedWriter(out);
  35.             String str="";
  36.  
  37.            
  38.             while((str=br.readLine())!=null)
  39.             {
  40.                
  41.                 dispari=false;
  42.                 str1=str.split(" ");
  43.                 for(int i=0;i<str1.length-1;i++)
  44.                     for(int j=i+1;j<str1.length;j++)
  45.                         {
  46.  
  47.                             //Analizziamo la stringa
  48.                             if(((Integer.parseInt(str1[i]))*(Integer.parseInt(str1[j])))%2!=0)
  49.                             {
  50.                                 dispari=true;
  51.                                 bw.write("1");
  52.                                 bw.newLine();
  53.                                 i=str1.length;
  54.                                 break;
  55.                             }
  56.                         }
  57.                 // bw.write("1");   // Errore Mio!!!!!!!!!!!!!
  58.                 if(!dispari)
  59.                 {
  60.                 bw.write("0");
  61.                 bw.newLine();
  62.                 }
  63.             }
  64.             in.close();
  65.             bw.close();
  66.             out.close();
  67.             br.close();
  68.             //bw.close();
  69.         }
  70.         catch( Exception e)
  71.         {
  72.             System.out.println("Il file che stai cercando di copiare non si trova nella directory giusta!");
  73.             System.out.println("Ricorda che il file che cerchi di aprire si deve trovare qui: " + System.getProperty("user.dir"));
  74.             Scanner t = new Scanner(System.in);
  75.             System.out.print("Inserisci la directory corretta nel seguente modo: /../../.. :");
  76.             String dir;
  77.             dir = t.next();
  78.             System.out.println(dir);
  79.             System.out.println (e.getMessage());
  80.         }
  81.        
  82.         finally
  83.         {
  84.         //  in.close();
  85.         //  bw.close();
  86.         //  out.close();
  87.         //  br.close();
  88.             System.out.println("**Done!**");
  89.         }
  90.  
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement