Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Scrivere un programma Java che legga da un file di input alcune sequenze di valori
- interi e determini, per ogni sequenza, se esiste una coppia di numeri il cui prodotto
- รจ un numero dispari. Si supponga che il numero di elementi pe...r ogni sequenza non
- sia superiore a 10 e che ogni elemento della sequenza abbia un valore compreso tra
- 0 e 100.
- */
- import java.util.Scanner;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- public class OddProduct {
- static String nome;
- static String name;
- public static void main (String args[])
- {
- boolean dispari=false;
- FileReader in =null;
- FileWriter out=null;
- Scanner input = new Scanner(System.in);
- System.out.print("Inserisci il nome del file di input: ");
- name = input.next();
- Scanner s = new Scanner(System.in);
- System.out.print("Inserisci il nome del file di output: ");
- nome = s.next();
- BufferedReader br=null;
- BufferedWriter bw=null;
- String [] str1=null;
- try
- {
- in=new FileReader(name);
- out=new FileWriter(nome);
- br=new BufferedReader(in);
- bw=new BufferedWriter(out);
- String str="";
- while((str=br.readLine())!=null)
- {
- dispari=false;
- str1=str.split(" ");
- for(int i=0;i<str1.length-1;i++)
- for(int j=i+1;j<str1.length;j++)
- {
- //Analizziamo la stringa
- if(((Integer.parseInt(str1[i]))*(Integer.parseInt(str1[j])))%2!=0)
- {
- dispari=true;
- bw.write("1");
- bw.newLine();
- i=str1.length;
- break;
- }
- }
- // bw.write("1"); // Errore Mio!!!!!!!!!!!!!
- if(!dispari)
- {
- bw.write("0");
- bw.newLine();
- }
- }
- in.close();
- bw.close();
- out.close();
- br.close();
- //bw.close();
- }
- catch( Exception e)
- {
- System.out.println("Il file che stai cercando di copiare non si trova nella directory giusta!");
- System.out.println("Ricorda che il file che cerchi di aprire si deve trovare qui: " + System.getProperty("user.dir"));
- Scanner t = new Scanner(System.in);
- System.out.print("Inserisci la directory corretta nel seguente modo: /../../.. :");
- String dir;
- dir = t.next();
- System.out.println(dir);
- System.out.println (e.getMessage());
- }
- finally
- {
- // in.close();
- // bw.close();
- // out.close();
- // br.close();
- System.out.println("**Done!**");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement