Advertisement
Guest User

Untitled

a guest
Nov 24th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. import java.io.FileInputStream; ex3
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6.  
  7. public class Ex3
  8. {
  9. static ArrayList<String> PeopleOver18()
  10. {
  11. ArrayList<String> list = new ArrayList<String>();
  12. FileInputStream f = null;
  13. try
  14. {
  15. f = new FileInputStream("clients.txt");
  16. Scanner s = new Scanner(f);
  17.  
  18. while(s.hasNext())
  19. {
  20. String nume = s.next();
  21. String prenume = s.next();
  22. int varsta = s.nextInt();
  23.  
  24. if(varsta>=18)
  25. list.add(prenume+" "+nume);
  26. }
  27.  
  28. }
  29. catch (FileNotFoundException e)
  30. {
  31. System.out.println("Cannot find file! \n"+e.getMessage());
  32. }
  33. finally
  34. {
  35. try {
  36. f.close();
  37. }
  38. catch (IOException e)
  39. {
  40. System.out.println("File not found");
  41. e.printStackTrace();
  42. }
  43. }
  44. return list;
  45. }
  46.  
  47. static ArrayList<String> PeopleUnder18()
  48. {
  49. ArrayList<String> list = new ArrayList<String>();
  50. FileInputStream f = null;
  51.  
  52. try
  53. {
  54. f = new FileInputStream("clients.txt");
  55. Scanner s = new Scanner(f);
  56.  
  57. while(s.hasNext())
  58. {
  59. String nume = s.next();
  60. String prenume = s.next();
  61. int varsta = s.nextInt();
  62.  
  63. if(varsta<18)
  64. list.add(prenume+" "+nume);
  65. }
  66.  
  67. }
  68. catch (FileNotFoundException e)
  69. {
  70. System.out.println("Cannot find file! \n"+e.getMessage());
  71. }
  72. finally
  73. {
  74. try {
  75. f.close();
  76. }
  77. catch (IOException e)
  78. {
  79. System.out.println("File not found");
  80. e.printStackTrace();
  81. }
  82. }
  83. return list;
  84.  
  85. }
  86.  
  87. public static void main(String[] args)
  88. {
  89. System.out.println("Numar persoane majore: "+PeopleOver18().size());
  90. System.out.println(PeopleOver18());
  91. System.out.println("Numar persoane minore: "+PeopleUnder18().size());
  92. System.out.println(PeopleUnder18());
  93. }
  94. }
  95.  
  96. import java.io.BufferedReader; ex2
  97. import java.io.FileInputStream;
  98. import java.io.FileNotFoundException;
  99. import java.io.IOException;
  100. import java.io.InputStreamReader;
  101. import java.util.Scanner;
  102.  
  103. public class Ex2
  104. {
  105. public static boolean FindUser(String user)
  106. {
  107. boolean found = false;
  108.  
  109. try
  110. {
  111. FileInputStream f = new FileInputStream("users.txt");
  112. Scanner s = new Scanner(f);
  113. String aux ="";
  114. while(s.hasNext() && found == false)
  115. {
  116. aux = s.next();
  117. if(user.compareTo(aux)==0)
  118. found = true;
  119. s.next();
  120. }
  121. }
  122. catch (FileNotFoundException e)
  123. {
  124. System.out.println(e.getMessage());
  125.  
  126. }
  127.  
  128. return found;
  129. }
  130.  
  131. static boolean ValidateLogin(String user,String pass)
  132. {
  133. boolean found = false;
  134. FileInputStream f = null;
  135. try
  136. {
  137. f = new FileInputStream("users.txt");
  138. Scanner s = new Scanner(f);
  139. String aux ="";
  140. String loginString = user+" "+pass;
  141. while(s.hasNext() && found == false)
  142. {
  143. aux = s.nextLine();
  144. if(aux.compareTo(loginString)==0)
  145. found = true;
  146. }
  147. }
  148. catch (FileNotFoundException e)
  149. {
  150. System.out.println(e.getMessage());
  151.  
  152. }
  153. finally
  154. {
  155. try
  156. {
  157. f.close();
  158. }
  159. catch (IOException e)
  160. {
  161. System.out.println("File not found");
  162. e.printStackTrace();
  163. }
  164. }
  165.  
  166. return found;
  167.  
  168. }
  169.  
  170. public static void main(String[] args)
  171. {
  172. String user="";
  173. String pass="";
  174.  
  175.  
  176. try
  177. {
  178. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  179.  
  180. int i=0;
  181. boolean logged = false;
  182. while(i<5 && logged == false)
  183. {
  184. System.out.println("User: ");
  185. user = stdin.readLine();
  186.  
  187. System.out.println("Password: ");
  188. pass = stdin.readLine();
  189.  
  190. if(ValidateLogin(user, pass))
  191. {
  192. System.out.println("Acces permis!");
  193. logged = true;
  194. }
  195. else if (FindUser(user))
  196. {
  197. System.out.println("Parola gresita!");
  198. int j=0;
  199. boolean ok = false;
  200. while (j<3 && ok==false)
  201. {
  202. System.out.println("Password: ");
  203. pass = stdin.readLine();
  204.  
  205. if(ValidateLogin(user, pass))
  206. ok=true;
  207. j++;
  208. }
  209. if (!ok)
  210. System.out.println("Cont blocat!");
  211. }
  212. i++;
  213. }
  214. if(!logged)
  215. System.out.println("Nu ai cont! Inregistreaza-te!");
  216. }
  217. catch(NumberFormatException ex)
  218. {
  219. System.out.println(ex.getMessage());
  220. }
  221. catch(IOException ex)
  222. {
  223. System.out.println(ex.getMessage());
  224. }
  225. }
  226. }
  227.  
  228.  
  229. import java.io.BufferedReader; ex1
  230. import java.io.FileNotFoundException;
  231. import java.io.FileReader;
  232. import java.io.FileWriter;
  233. import java.io.IOException;
  234. import java.io.InputStreamReader;
  235. import java.util.Scanner;
  236.  
  237. public class Ex1
  238. {
  239. static int[][] ReadMatrix()
  240. {
  241. int[][] mat = null;
  242. int rows = 0,cols = 0;
  243.  
  244. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  245.  
  246.  
  247.  
  248. try
  249. {
  250. System.out.println("Number of rows: ");
  251. rows = Integer.parseInt(stdin.readLine());
  252. System.out.println("Number of cols: ");
  253. cols = Integer.parseInt(stdin.readLine());
  254.  
  255. mat = new int[rows][cols];
  256.  
  257. for(int i=0;i<rows;i++)
  258. for(int j=0;j<cols;j++)
  259. {
  260. System.out.println("mat["+i+"]["+j+"] = ");
  261. mat[i][j]=Integer.parseInt(stdin.readLine());
  262. }
  263.  
  264. }
  265. catch (NumberFormatException e)
  266. {
  267. System.out.println("Cannot parseInt \n"+e.getMessage());
  268. }
  269. catch(NullPointerException e)
  270. {
  271. System.out.println(e.getMessage());
  272. }
  273. catch (IOException ex)
  274. {
  275. System.out.println(ex.getMessage());
  276. }
  277.  
  278.  
  279. return mat;
  280. }
  281.  
  282. static int[][] ReadMatrixFromFile(String fileName)
  283. {
  284. int [][] mat=null;
  285. int cols=0, rows =0;
  286.  
  287. FileReader f =null;
  288.  
  289.  
  290. try
  291. {
  292. f = new FileReader(fileName);
  293. Scanner s = new Scanner(f);
  294.  
  295. rows = s.nextInt();
  296. cols = s.nextInt();
  297.  
  298. mat = new int[rows][cols];
  299.  
  300. int i=0,j=0;
  301. while(s.hasNextInt())
  302. {
  303.  
  304. if(j<cols)
  305. {
  306. mat[i][j++] =s.nextInt();
  307.  
  308. }
  309. else
  310. {
  311. j=0;
  312. i++;
  313. mat[i][j++] = s.nextInt();
  314.  
  315. }
  316. }
  317.  
  318.  
  319. }
  320. catch(NumberFormatException ex)
  321. {
  322. System.out.println("Cannot parseInt \n"+ex.getMessage());
  323. }
  324. catch (FileNotFoundException e)
  325. {
  326. System.out.println("Cannot find file. \n"+e.getMessage());
  327. }
  328. catch (IOException e)
  329. {
  330. System.out.println(e.getMessage());
  331. }
  332. finally
  333. {
  334. try
  335. {
  336. f.close();
  337. }
  338. catch (IOException e)
  339. {
  340. System.out.println("File not found");
  341. e.printStackTrace();
  342. }
  343. }
  344.  
  345. return mat;
  346. }
  347.  
  348. static int[][] MultiplyMatrix(int[][] mat1,int[][] mat2)
  349. {
  350. int[][] result = null;
  351.  
  352. try
  353. {
  354.  
  355.  
  356. if(mat1[0].length!=mat2.length)
  357. {
  358. System.out.println("Cannot multiply matrix.");
  359. }
  360. else
  361. {
  362. result = new int[mat1.length][mat2[0].length];
  363.  
  364. for(int i=0;i<mat1.length;i++)
  365. for(int j=0;j<mat2[0].length;j++)
  366. for(int k=0;k<mat1[0].length;k++)
  367. {
  368. result[i][j] += mat1[i][k] * mat2[k][j];
  369. }
  370. }
  371. }
  372. catch(NullPointerException ex)
  373. {
  374. System.out.println("Matrix is null \n"+ex.getMessage());
  375. }
  376.  
  377. return result;
  378. }
  379.  
  380. static void DisplayMatrix(int[][] mat)
  381. {
  382. try
  383. {
  384. System.out.println("Matricea este : ");
  385.  
  386. for (int i=0;i<mat.length;i++)
  387. {
  388. for(int j=0;j<mat[0].length;j++)
  389. System.out.print(mat[i][j]+" ");
  390. System.out.println();
  391. }
  392. }
  393. catch(NullPointerException ex)
  394. {
  395. System.out.println("Matrix is null \n"+ex.getMessage());
  396. }
  397. }
  398.  
  399. static void WriteMatrixToFile(int[][] mat,String fileName)
  400. {
  401. FileWriter f = null;
  402. try
  403. {
  404.  
  405. f = new FileWriter(fileName);
  406.  
  407. for (int i=0;i<mat.length;i++)
  408. {
  409. for(int j=0;j<mat[0].length;j++)
  410. {
  411. String aux = String.valueOf(mat[i][j])+" ";
  412. f.append(aux);
  413. }
  414. f.append("\r\n");
  415. }
  416. }
  417. catch(NullPointerException ex)
  418. {
  419. System.out.println("Matrix is null \n"+ex.getMessage());
  420. }
  421. catch (IOException e)
  422. {
  423. System.out.println("Cannot find file \n"+e.getMessage());
  424. }
  425. finally
  426. {
  427. try
  428. {
  429. f.close();
  430. }
  431. catch (IOException e)
  432. {
  433. System.out.println(e.getMessage());
  434. }
  435. }
  436. }
  437.  
  438. public static void main(String[] args)
  439. {
  440. int[][] mat1 = ReadMatrixFromFile("in.txt");
  441. DisplayMatrix(mat1);
  442. int[][] mat2 = ReadMatrix();
  443. DisplayMatrix(mat2);
  444. DisplayMatrix(MultiplyMatrix(mat1, mat2));
  445. WriteMatrixToFile(MultiplyMatrix(mat1, mat2),"out.txt");
  446.  
  447. }
  448. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement