Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileInputStream; ex3
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Ex3
- {
- static ArrayList<String> PeopleOver18()
- {
- ArrayList<String> list = new ArrayList<String>();
- FileInputStream f = null;
- try
- {
- f = new FileInputStream("clients.txt");
- Scanner s = new Scanner(f);
- while(s.hasNext())
- {
- String nume = s.next();
- String prenume = s.next();
- int varsta = s.nextInt();
- if(varsta>=18)
- list.add(prenume+" "+nume);
- }
- }
- catch (FileNotFoundException e)
- {
- System.out.println("Cannot find file! \n"+e.getMessage());
- }
- finally
- {
- try {
- f.close();
- }
- catch (IOException e)
- {
- System.out.println("File not found");
- e.printStackTrace();
- }
- }
- return list;
- }
- static ArrayList<String> PeopleUnder18()
- {
- ArrayList<String> list = new ArrayList<String>();
- FileInputStream f = null;
- try
- {
- f = new FileInputStream("clients.txt");
- Scanner s = new Scanner(f);
- while(s.hasNext())
- {
- String nume = s.next();
- String prenume = s.next();
- int varsta = s.nextInt();
- if(varsta<18)
- list.add(prenume+" "+nume);
- }
- }
- catch (FileNotFoundException e)
- {
- System.out.println("Cannot find file! \n"+e.getMessage());
- }
- finally
- {
- try {
- f.close();
- }
- catch (IOException e)
- {
- System.out.println("File not found");
- e.printStackTrace();
- }
- }
- return list;
- }
- public static void main(String[] args)
- {
- System.out.println("Numar persoane majore: "+PeopleOver18().size());
- System.out.println(PeopleOver18());
- System.out.println("Numar persoane minore: "+PeopleUnder18().size());
- System.out.println(PeopleUnder18());
- }
- }
- import java.io.BufferedReader; ex2
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- public class Ex2
- {
- public static boolean FindUser(String user)
- {
- boolean found = false;
- try
- {
- FileInputStream f = new FileInputStream("users.txt");
- Scanner s = new Scanner(f);
- String aux ="";
- while(s.hasNext() && found == false)
- {
- aux = s.next();
- if(user.compareTo(aux)==0)
- found = true;
- s.next();
- }
- }
- catch (FileNotFoundException e)
- {
- System.out.println(e.getMessage());
- }
- return found;
- }
- static boolean ValidateLogin(String user,String pass)
- {
- boolean found = false;
- FileInputStream f = null;
- try
- {
- f = new FileInputStream("users.txt");
- Scanner s = new Scanner(f);
- String aux ="";
- String loginString = user+" "+pass;
- while(s.hasNext() && found == false)
- {
- aux = s.nextLine();
- if(aux.compareTo(loginString)==0)
- found = true;
- }
- }
- catch (FileNotFoundException e)
- {
- System.out.println(e.getMessage());
- }
- finally
- {
- try
- {
- f.close();
- }
- catch (IOException e)
- {
- System.out.println("File not found");
- e.printStackTrace();
- }
- }
- return found;
- }
- public static void main(String[] args)
- {
- String user="";
- String pass="";
- try
- {
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- int i=0;
- boolean logged = false;
- while(i<5 && logged == false)
- {
- System.out.println("User: ");
- user = stdin.readLine();
- System.out.println("Password: ");
- pass = stdin.readLine();
- if(ValidateLogin(user, pass))
- {
- System.out.println("Acces permis!");
- logged = true;
- }
- else if (FindUser(user))
- {
- System.out.println("Parola gresita!");
- int j=0;
- boolean ok = false;
- while (j<3 && ok==false)
- {
- System.out.println("Password: ");
- pass = stdin.readLine();
- if(ValidateLogin(user, pass))
- ok=true;
- j++;
- }
- if (!ok)
- System.out.println("Cont blocat!");
- }
- i++;
- }
- if(!logged)
- System.out.println("Nu ai cont! Inregistreaza-te!");
- }
- catch(NumberFormatException ex)
- {
- System.out.println(ex.getMessage());
- }
- catch(IOException ex)
- {
- System.out.println(ex.getMessage());
- }
- }
- }
- import java.io.BufferedReader; ex1
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- public class Ex1
- {
- static int[][] ReadMatrix()
- {
- int[][] mat = null;
- int rows = 0,cols = 0;
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- try
- {
- System.out.println("Number of rows: ");
- rows = Integer.parseInt(stdin.readLine());
- System.out.println("Number of cols: ");
- cols = Integer.parseInt(stdin.readLine());
- mat = new int[rows][cols];
- for(int i=0;i<rows;i++)
- for(int j=0;j<cols;j++)
- {
- System.out.println("mat["+i+"]["+j+"] = ");
- mat[i][j]=Integer.parseInt(stdin.readLine());
- }
- }
- catch (NumberFormatException e)
- {
- System.out.println("Cannot parseInt \n"+e.getMessage());
- }
- catch(NullPointerException e)
- {
- System.out.println(e.getMessage());
- }
- catch (IOException ex)
- {
- System.out.println(ex.getMessage());
- }
- return mat;
- }
- static int[][] ReadMatrixFromFile(String fileName)
- {
- int [][] mat=null;
- int cols=0, rows =0;
- FileReader f =null;
- try
- {
- f = new FileReader(fileName);
- Scanner s = new Scanner(f);
- rows = s.nextInt();
- cols = s.nextInt();
- mat = new int[rows][cols];
- int i=0,j=0;
- while(s.hasNextInt())
- {
- if(j<cols)
- {
- mat[i][j++] =s.nextInt();
- }
- else
- {
- j=0;
- i++;
- mat[i][j++] = s.nextInt();
- }
- }
- }
- catch(NumberFormatException ex)
- {
- System.out.println("Cannot parseInt \n"+ex.getMessage());
- }
- catch (FileNotFoundException e)
- {
- System.out.println("Cannot find file. \n"+e.getMessage());
- }
- catch (IOException e)
- {
- System.out.println(e.getMessage());
- }
- finally
- {
- try
- {
- f.close();
- }
- catch (IOException e)
- {
- System.out.println("File not found");
- e.printStackTrace();
- }
- }
- return mat;
- }
- static int[][] MultiplyMatrix(int[][] mat1,int[][] mat2)
- {
- int[][] result = null;
- try
- {
- if(mat1[0].length!=mat2.length)
- {
- System.out.println("Cannot multiply matrix.");
- }
- else
- {
- result = new int[mat1.length][mat2[0].length];
- for(int i=0;i<mat1.length;i++)
- for(int j=0;j<mat2[0].length;j++)
- for(int k=0;k<mat1[0].length;k++)
- {
- result[i][j] += mat1[i][k] * mat2[k][j];
- }
- }
- }
- catch(NullPointerException ex)
- {
- System.out.println("Matrix is null \n"+ex.getMessage());
- }
- return result;
- }
- static void DisplayMatrix(int[][] mat)
- {
- try
- {
- System.out.println("Matricea este : ");
- for (int i=0;i<mat.length;i++)
- {
- for(int j=0;j<mat[0].length;j++)
- System.out.print(mat[i][j]+" ");
- System.out.println();
- }
- }
- catch(NullPointerException ex)
- {
- System.out.println("Matrix is null \n"+ex.getMessage());
- }
- }
- static void WriteMatrixToFile(int[][] mat,String fileName)
- {
- FileWriter f = null;
- try
- {
- f = new FileWriter(fileName);
- for (int i=0;i<mat.length;i++)
- {
- for(int j=0;j<mat[0].length;j++)
- {
- String aux = String.valueOf(mat[i][j])+" ";
- f.append(aux);
- }
- f.append("\r\n");
- }
- }
- catch(NullPointerException ex)
- {
- System.out.println("Matrix is null \n"+ex.getMessage());
- }
- catch (IOException e)
- {
- System.out.println("Cannot find file \n"+e.getMessage());
- }
- finally
- {
- try
- {
- f.close();
- }
- catch (IOException e)
- {
- System.out.println(e.getMessage());
- }
- }
- }
- public static void main(String[] args)
- {
- int[][] mat1 = ReadMatrixFromFile("in.txt");
- DisplayMatrix(mat1);
- int[][] mat2 = ReadMatrix();
- DisplayMatrix(mat2);
- DisplayMatrix(MultiplyMatrix(mat1, mat2));
- WriteMatrixToFile(MultiplyMatrix(mat1, mat2),"out.txt");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement