From 8067025a30223fdc185e5969656264231d12ac0c Mon Sep 17 00:00:00 2001 From: Amira20208045 Date: Sun, 7 May 2023 00:43:45 +0300 Subject: [PATCH 1/9] Task 1 added --- README.md | 2 -- Task1-InputMismatchException/.gitignore | 29 +++++++++++++++++++ Task1-InputMismatchException/.idea/.gitignore | 8 +++++ Task1-InputMismatchException/.idea/misc.xml | 6 ++++ .../.idea/modules.xml | 8 +++++ Task1-InputMismatchException/.idea/vcs.xml | 6 ++++ .../InputMismatchException.iml | 11 +++++++ Task1-InputMismatchException/src/Main.java | 25 ++++++++++++++++ 8 files changed, 93 insertions(+), 2 deletions(-) delete mode 100644 README.md create mode 100644 Task1-InputMismatchException/.gitignore create mode 100644 Task1-InputMismatchException/.idea/.gitignore create mode 100644 Task1-InputMismatchException/.idea/misc.xml create mode 100644 Task1-InputMismatchException/.idea/modules.xml create mode 100644 Task1-InputMismatchException/.idea/vcs.xml create mode 100644 Task1-InputMismatchException/InputMismatchException.iml create mode 100644 Task1-InputMismatchException/src/Main.java diff --git a/README.md b/README.md deleted file mode 100644 index a974d78..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# JobHacker-System -This repo for JobHacker Community tasks. diff --git a/Task1-InputMismatchException/.gitignore b/Task1-InputMismatchException/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/Task1-InputMismatchException/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Task1-InputMismatchException/.idea/.gitignore b/Task1-InputMismatchException/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/Task1-InputMismatchException/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Task1-InputMismatchException/.idea/misc.xml b/Task1-InputMismatchException/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/Task1-InputMismatchException/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Task1-InputMismatchException/.idea/modules.xml b/Task1-InputMismatchException/.idea/modules.xml new file mode 100644 index 0000000..d9a6d36 --- /dev/null +++ b/Task1-InputMismatchException/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Task1-InputMismatchException/.idea/vcs.xml b/Task1-InputMismatchException/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Task1-InputMismatchException/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Task1-InputMismatchException/InputMismatchException.iml b/Task1-InputMismatchException/InputMismatchException.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Task1-InputMismatchException/InputMismatchException.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Task1-InputMismatchException/src/Main.java b/Task1-InputMismatchException/src/Main.java new file mode 100644 index 0000000..5bf6d16 --- /dev/null +++ b/Task1-InputMismatchException/src/Main.java @@ -0,0 +1,25 @@ +import java.util.InputMismatchException; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int num1 , num2 ; + boolean isValidInput = false; + + while (!isValidInput) { + try { + System.out.print("Enter first integer: "); + num1 = scanner.nextInt(); + System.out.print("Enter second integer: "); + num2 = scanner.nextInt(); + + System.out.println("You entered " + num1 + " and " + num2 + "\nThe summation is: " + (num1 + num2)); + isValidInput = true; + } catch (InputMismatchException e) { + System.out.println("Invalid input, Enter integers only."); + scanner.nextLine(); + } + } + } +} From 2576a71fb1845a5f3361550a0e7c6437ed93b2fd Mon Sep 17 00:00:00 2001 From: Amira20208045 Date: Sun, 7 May 2023 00:52:38 +0300 Subject: [PATCH 2/9] task 2 added --- Task 2-Word Count/.gitignore | 29 +++++++++++++++++++++++++ Task 2-Word Count/.idea/.gitignore | 8 +++++++ Task 2-Word Count/.idea/misc.xml | 6 +++++ Task 2-Word Count/.idea/modules.xml | 8 +++++++ Task 2-Word Count/.idea/vcs.xml | 6 +++++ Task 2-Word Count/Lincoln.txt | 1 + Task 2-Word Count/Task 2-Word Count.iml | 11 ++++++++++ Task 2-Word Count/src/Main.java | 21 ++++++++++++++++++ 8 files changed, 90 insertions(+) create mode 100644 Task 2-Word Count/.gitignore create mode 100644 Task 2-Word Count/.idea/.gitignore create mode 100644 Task 2-Word Count/.idea/misc.xml create mode 100644 Task 2-Word Count/.idea/modules.xml create mode 100644 Task 2-Word Count/.idea/vcs.xml create mode 100644 Task 2-Word Count/Lincoln.txt create mode 100644 Task 2-Word Count/Task 2-Word Count.iml create mode 100644 Task 2-Word Count/src/Main.java diff --git a/Task 2-Word Count/.gitignore b/Task 2-Word Count/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/Task 2-Word Count/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Task 2-Word Count/.idea/.gitignore b/Task 2-Word Count/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/Task 2-Word Count/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Task 2-Word Count/.idea/misc.xml b/Task 2-Word Count/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/Task 2-Word Count/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Task 2-Word Count/.idea/modules.xml b/Task 2-Word Count/.idea/modules.xml new file mode 100644 index 0000000..300f29f --- /dev/null +++ b/Task 2-Word Count/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Task 2-Word Count/.idea/vcs.xml b/Task 2-Word Count/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Task 2-Word Count/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Task 2-Word Count/Lincoln.txt b/Task 2-Word Count/Lincoln.txt new file mode 100644 index 0000000..2bf7482 --- /dev/null +++ b/Task 2-Word Count/Lincoln.txt @@ -0,0 +1 @@ +Java is a programming language. \ No newline at end of file diff --git a/Task 2-Word Count/Task 2-Word Count.iml b/Task 2-Word Count/Task 2-Word Count.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Task 2-Word Count/Task 2-Word Count.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Task 2-Word Count/src/Main.java b/Task 2-Word Count/src/Main.java new file mode 100644 index 0000000..e928bb6 --- /dev/null +++ b/Task 2-Word Count/src/Main.java @@ -0,0 +1,21 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + File file = new File("Lincoln.txt"); + int wordCount = 0; + try { + Scanner scanner = new Scanner(file); + while (scanner.hasNext()) { + scanner.next(); + wordCount++; + } + scanner.close(); + System.out.println("The file " + file.getName() + " contains " + wordCount + " words."); + } catch (FileNotFoundException e) { + System.out.println("File not found, Check the name of the file or the file may be deleted or moved"); + } + } +} From 846cb8f9e29748380e1f75df64999c2efbb9e1e1 Mon Sep 17 00:00:00 2001 From: Amira20208045 Date: Sun, 7 May 2023 01:27:49 +0300 Subject: [PATCH 3/9] Task 3 added --- Task 3/.gitignore | 29 ++++++++++++++++++++ Task 3/.idea/.gitignore | 8 ++++++ Task 3/.idea/misc.xml | 6 ++++ Task 3/.idea/modules.xml | 8 ++++++ Task 3/.idea/vcs.xml | 6 ++++ Task 3/Scores.txt | 2 ++ Task 3/Task 3.iml | 11 ++++++++ Task 3/src/FileManipulation.java | 47 ++++++++++++++++++++++++++++++++ Task 3/src/Main.java | 16 +++++++++++ 9 files changed, 133 insertions(+) create mode 100644 Task 3/.gitignore create mode 100644 Task 3/.idea/.gitignore create mode 100644 Task 3/.idea/misc.xml create mode 100644 Task 3/.idea/modules.xml create mode 100644 Task 3/.idea/vcs.xml create mode 100644 Task 3/Scores.txt create mode 100644 Task 3/Task 3.iml create mode 100644 Task 3/src/FileManipulation.java create mode 100644 Task 3/src/Main.java diff --git a/Task 3/.gitignore b/Task 3/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/Task 3/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Task 3/.idea/.gitignore b/Task 3/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/Task 3/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Task 3/.idea/misc.xml b/Task 3/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/Task 3/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Task 3/.idea/modules.xml b/Task 3/.idea/modules.xml new file mode 100644 index 0000000..40d4bd4 --- /dev/null +++ b/Task 3/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Task 3/.idea/vcs.xml b/Task 3/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Task 3/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Task 3/Scores.txt b/Task 3/Scores.txt new file mode 100644 index 0000000..27afc0a --- /dev/null +++ b/Task 3/Scores.txt @@ -0,0 +1,2 @@ +34 34 54 14 32 24 31 34 53 74 22 29 +4 34 14 14 32 24 31 34 53 74 22 29 diff --git a/Task 3/Task 3.iml b/Task 3/Task 3.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Task 3/Task 3.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Task 3/src/FileManipulation.java b/Task 3/src/FileManipulation.java new file mode 100644 index 0000000..3d10f44 --- /dev/null +++ b/Task 3/src/FileManipulation.java @@ -0,0 +1,47 @@ +import java.io.*; +import java.net.URL; + +public class FileManipulation { + File readFromWebToFile(String urlString) throws IOException { + URL url = new URL(urlString); + BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); + FileWriter writer = new FileWriter("Scores.txt"); + + String line; + while ((line = reader.readLine()) != null) { + writer.write(line + "\n"); + } + reader.close(); + writer.close(); + return new File("Scores.txt"); + } + + int getSum(File file) throws IOException{ + BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + String line; + int sum = 0; + while ((line = bufferedReader.readLine()) != null) { + String[] scores = line.split(" "); + for (String score : scores) { + sum += Integer.parseInt(score); + } + } + bufferedReader.close(); + return sum; + } + + int getAverage(File file) throws IOException{ + BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + String line; + int sum = 0 , count = 0; + while ((line = bufferedReader.readLine()) != null) { + String[] scores = line.split(" "); + for (String score : scores) { + sum += Integer.parseInt(score); + count++; + } + } + bufferedReader.close(); + return sum/count; + } +} diff --git a/Task 3/src/Main.java b/Task 3/src/Main.java new file mode 100644 index 0000000..c4c4438 --- /dev/null +++ b/Task 3/src/Main.java @@ -0,0 +1,16 @@ +import java.io.File; +import java.io.IOException; + +public class Main { + + public static void main(String[] args) { + FileManipulation file = new FileManipulation(); + try{ + File f = file.readFromWebToFile("http://liveexample.pearsoncmg.com/data/Scores.txt"); + System.out.println("The summation of words: " + file.getSum(f)); + System.out.println("The average of words: " + file.getAverage(f)); + }catch (IOException e){ + System.out.println(e.getMessage()); + } + } +} \ No newline at end of file From d3df488c5f7d942f493b8be84b0440bbebde4602 Mon Sep 17 00:00:00 2001 From: Amira20208045 Date: Tue, 9 May 2023 21:11:15 +0300 Subject: [PATCH 4/9] File class replaced by FileInputStream --- Task 2-Word Count/src/Main.java | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/Task 2-Word Count/src/Main.java b/Task 2-Word Count/src/Main.java index e928bb6..19b9149 100644 --- a/Task 2-Word Count/src/Main.java +++ b/Task 2-Word Count/src/Main.java @@ -1,21 +1,17 @@ -import java.io.File; -import java.io.FileNotFoundException; -import java.util.Scanner; +import java.io.*; public class Main { public static void main(String[] args) { - File file = new File("Lincoln.txt"); - int wordCount = 0; try { - Scanner scanner = new Scanner(file); - while (scanner.hasNext()) { - scanner.next(); - wordCount++; - } - scanner.close(); - System.out.println("The file " + file.getName() + " contains " + wordCount + " words."); - } catch (FileNotFoundException e) { - System.out.println("File not found, Check the name of the file or the file may be deleted or moved"); + FileInputStream file =new FileInputStream("Lincoln.txt"); + byte[] bytes = new byte[(int) file.available()]; + file.read(bytes); + String content = new String(bytes); + String[] words = content.split("\\s+"); + int wordCount = words.length; + System.out.println("The Lincoln file contains " + wordCount + " words."); + } catch (IOException e){ + System.out.println(e.getMessage()); } } } From 7ccd9bf4d5975bbefa6e7db56514db6e2848a460 Mon Sep 17 00:00:00 2001 From: Amira20208045 Date: Tue, 9 May 2023 21:28:20 +0300 Subject: [PATCH 5/9] FileInputStream used --- Task 3/Scores.txt | 2 +- Task 3/src/FileManipulation.java | 69 +++++++++++++++----------------- Task 3/src/Main.java | 6 +-- 3 files changed, 37 insertions(+), 40 deletions(-) diff --git a/Task 3/Scores.txt b/Task 3/Scores.txt index 27afc0a..939764e 100644 --- a/Task 3/Scores.txt +++ b/Task 3/Scores.txt @@ -1,2 +1,2 @@ 34 34 54 14 32 24 31 34 53 74 22 29 -4 34 14 14 32 24 31 34 53 74 22 29 +4 34 14 14 32 24 31 34 53 74 22 29 \ No newline at end of file diff --git a/Task 3/src/FileManipulation.java b/Task 3/src/FileManipulation.java index 3d10f44..b332f54 100644 --- a/Task 3/src/FileManipulation.java +++ b/Task 3/src/FileManipulation.java @@ -1,47 +1,44 @@ import java.io.*; import java.net.URL; +import java.util.*; public class FileManipulation { - File readFromWebToFile(String urlString) throws IOException { - URL url = new URL(urlString); - BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); - FileWriter writer = new FileWriter("Scores.txt"); + String readFromWebToFile(String url) throws IOException { + URL fileUrl = new URL(url); + InputStream is = fileUrl.openStream(); + String filePath = "Scores.txt"; + FileOutputStream fos = new FileOutputStream(filePath); - String line; - while ((line = reader.readLine()) != null) { - writer.write(line + "\n"); - } - reader.close(); - writer.close(); - return new File("Scores.txt"); + byte[] buffer = new byte[1024]; + int len; + while ((len = is.read(buffer)) != -1) { + fos.write(buffer, 0, len); + } + is.close(); + fos.close(); + return filePath; } - int getSum(File file) throws IOException{ - BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); - String line; - int sum = 0; - while ((line = bufferedReader.readLine()) != null) { - String[] scores = line.split(" "); - for (String score : scores) { - sum += Integer.parseInt(score); - } - } - bufferedReader.close(); - return sum; + int getSum(String filePath) throws IOException{ + FileInputStream file =new FileInputStream(filePath); + Scanner scanner = new Scanner(file); + int sum = 0; + while (scanner.hasNextInt()) { + int score = scanner.nextInt(); + sum += score; + } + return sum; } - int getAverage(File file) throws IOException{ - BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); - String line; - int sum = 0 , count = 0; - while ((line = bufferedReader.readLine()) != null) { - String[] scores = line.split(" "); - for (String score : scores) { - sum += Integer.parseInt(score); - count++; - } - } - bufferedReader.close(); - return sum/count; + int getAverage(String filePath) throws IOException{ + FileInputStream file =new FileInputStream(filePath); + Scanner scanner = new Scanner(file); + int sum = 0 , count = 0; + while (scanner.hasNextInt()) { + int score = scanner.nextInt(); + sum += score; + count++; + } + return sum / count; } } diff --git a/Task 3/src/Main.java b/Task 3/src/Main.java index c4c4438..a0d9c7e 100644 --- a/Task 3/src/Main.java +++ b/Task 3/src/Main.java @@ -6,9 +6,9 @@ public class Main { public static void main(String[] args) { FileManipulation file = new FileManipulation(); try{ - File f = file.readFromWebToFile("http://liveexample.pearsoncmg.com/data/Scores.txt"); - System.out.println("The summation of words: " + file.getSum(f)); - System.out.println("The average of words: " + file.getAverage(f)); + String path = file.readFromWebToFile("http://liveexample.pearsoncmg.com/data/Scores.txt"); + System.out.println("The summation of words: " + file.getSum(path)); + System.out.println("The average of words: " + file.getAverage(path)); }catch (IOException e){ System.out.println(e.getMessage()); } From 89c2c8952fc4ee21b3d78b508986754c2b03e494 Mon Sep 17 00:00:00 2001 From: Amira20208045 Date: Tue, 9 May 2023 21:32:00 +0300 Subject: [PATCH 6/9] Scanner class used --- Task 2-Word Count/src/Main.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Task 2-Word Count/src/Main.java b/Task 2-Word Count/src/Main.java index 19b9149..05f42f8 100644 --- a/Task 2-Word Count/src/Main.java +++ b/Task 2-Word Count/src/Main.java @@ -1,15 +1,18 @@ import java.io.*; +import java.util.*; public class Main { public static void main(String[] args) { try { FileInputStream file =new FileInputStream("Lincoln.txt"); - byte[] bytes = new byte[(int) file.available()]; - file.read(bytes); - String content = new String(bytes); - String[] words = content.split("\\s+"); - int wordCount = words.length; - System.out.println("The Lincoln file contains " + wordCount + " words."); + + Scanner scanner = new Scanner(file); + int count = 0; + while (scanner.hasNext()) { + String word = scanner.next(); + count++; + } + System.out.println("The Lincoln file contains " + count + " words."); } catch (IOException e){ System.out.println(e.getMessage()); } From bc9e64cbcdb900a95b9cc886f6df8a729fbde11b Mon Sep 17 00:00:00 2001 From: Amira Taha <74466997+amira921@users.noreply.github.com> Date: Tue, 9 May 2023 21:35:30 +0300 Subject: [PATCH 7/9] Update Main.java --- Task 2-Word Count/src/Main.java | 1 - 1 file changed, 1 deletion(-) diff --git a/Task 2-Word Count/src/Main.java b/Task 2-Word Count/src/Main.java index 05f42f8..5ca0d7b 100644 --- a/Task 2-Word Count/src/Main.java +++ b/Task 2-Word Count/src/Main.java @@ -9,7 +9,6 @@ public static void main(String[] args) { Scanner scanner = new Scanner(file); int count = 0; while (scanner.hasNext()) { - String word = scanner.next(); count++; } System.out.println("The Lincoln file contains " + count + " words."); From fbba89a1b653f03cc9b1caef68a2585c7b956abb Mon Sep 17 00:00:00 2001 From: amira921 Date: Sun, 6 Aug 2023 02:12:41 +0300 Subject: [PATCH 8/9] changes requested done --- Task 2-Word Count/Lincoln.txt | 5 ++++- Task 2-Word Count/src/Main.java | 19 ++++++++++++++----- Task 3/src/FileManipulation.java | 8 ++++---- Task 3/src/Main.java | 6 +++--- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Task 2-Word Count/Lincoln.txt b/Task 2-Word Count/Lincoln.txt index 2bf7482..e07ecc7 100644 --- a/Task 2-Word Count/Lincoln.txt +++ b/Task 2-Word Count/Lincoln.txt @@ -1 +1,4 @@ -Java is a programming language. \ No newline at end of file +Java is /a -programming ,language. +Java is _easy. +the file contains 8 + diff --git a/Task 2-Word Count/src/Main.java b/Task 2-Word Count/src/Main.java index 5ca0d7b..6d67607 100644 --- a/Task 2-Word Count/src/Main.java +++ b/Task 2-Word Count/src/Main.java @@ -1,19 +1,28 @@ import java.io.*; -import java.util.*; +import java.util.Scanner; public class Main { public static void main(String[] args) { try { - FileInputStream file =new FileInputStream("Lincoln.txt"); - + File file = new File("Lincoln.txt"); Scanner scanner = new Scanner(file); int count = 0; while (scanner.hasNext()) { + scanner.next(); count++; } - System.out.println("The Lincoln file contains " + count + " words."); - } catch (IOException e){ + scanner.close(); + + BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); + String str = "the file contains " + count + '\n'; + writer.write(str); + writer.newLine(); + writer.close(); + + } catch (FileNotFoundException e) { System.out.println(e.getMessage()); + } catch (IOException e) { + throw new RuntimeException(e); } } } diff --git a/Task 3/src/FileManipulation.java b/Task 3/src/FileManipulation.java index b332f54..a336a03 100644 --- a/Task 3/src/FileManipulation.java +++ b/Task 3/src/FileManipulation.java @@ -3,7 +3,7 @@ import java.util.*; public class FileManipulation { - String readFromWebToFile(String url) throws IOException { + File readFromWebToFile(String url) throws IOException { URL fileUrl = new URL(url); InputStream is = fileUrl.openStream(); String filePath = "Scores.txt"; @@ -16,10 +16,10 @@ String readFromWebToFile(String url) throws IOException { } is.close(); fos.close(); - return filePath; + return new File(filePath); } - int getSum(String filePath) throws IOException{ + int getSum(File filePath) throws IOException{ FileInputStream file =new FileInputStream(filePath); Scanner scanner = new Scanner(file); int sum = 0; @@ -30,7 +30,7 @@ int getSum(String filePath) throws IOException{ return sum; } - int getAverage(String filePath) throws IOException{ + int getAverage(File filePath) throws IOException{ FileInputStream file =new FileInputStream(filePath); Scanner scanner = new Scanner(file); int sum = 0 , count = 0; diff --git a/Task 3/src/Main.java b/Task 3/src/Main.java index a0d9c7e..c4c4438 100644 --- a/Task 3/src/Main.java +++ b/Task 3/src/Main.java @@ -6,9 +6,9 @@ public class Main { public static void main(String[] args) { FileManipulation file = new FileManipulation(); try{ - String path = file.readFromWebToFile("http://liveexample.pearsoncmg.com/data/Scores.txt"); - System.out.println("The summation of words: " + file.getSum(path)); - System.out.println("The average of words: " + file.getAverage(path)); + File f = file.readFromWebToFile("http://liveexample.pearsoncmg.com/data/Scores.txt"); + System.out.println("The summation of words: " + file.getSum(f)); + System.out.println("The average of words: " + file.getAverage(f)); }catch (IOException e){ System.out.println(e.getMessage()); } From a678105161f8aaf4cbcab78c6488677204256495 Mon Sep 17 00:00:00 2001 From: amira921 Date: Fri, 11 Aug 2023 18:49:08 +0300 Subject: [PATCH 9/9] group in one folder --- {Task 2-Word Count => Week 5/Task 2-Word Count}/.gitignore | 0 {Task 2-Word Count => Week 5/Task 2-Word Count}/.idea/.gitignore | 0 {Task 2-Word Count => Week 5/Task 2-Word Count}/.idea/misc.xml | 0 {Task 2-Word Count => Week 5/Task 2-Word Count}/.idea/modules.xml | 0 {Task 2-Word Count => Week 5/Task 2-Word Count}/.idea/vcs.xml | 0 {Task 2-Word Count => Week 5/Task 2-Word Count}/Lincoln.txt | 0 .../Task 2-Word Count}/Task 2-Word Count.iml | 0 {Task 2-Word Count => Week 5/Task 2-Word Count}/src/Main.java | 0 {Task 3 => Week 5/Task 3}/.gitignore | 0 {Task 3 => Week 5/Task 3}/.idea/.gitignore | 0 {Task 3 => Week 5/Task 3}/.idea/misc.xml | 0 {Task 3 => Week 5/Task 3}/.idea/modules.xml | 0 {Task 3 => Week 5/Task 3}/.idea/vcs.xml | 0 {Task 3 => Week 5/Task 3}/Scores.txt | 0 {Task 3 => Week 5/Task 3}/Task 3.iml | 0 {Task 3 => Week 5/Task 3}/src/FileManipulation.java | 0 {Task 3 => Week 5/Task 3}/src/Main.java | 0 .../Task1-InputMismatchException}/.gitignore | 0 .../Task1-InputMismatchException}/.idea/.gitignore | 0 .../Task1-InputMismatchException}/.idea/misc.xml | 0 .../Task1-InputMismatchException}/.idea/modules.xml | 0 .../Task1-InputMismatchException}/.idea/vcs.xml | 0 .../Task1-InputMismatchException}/InputMismatchException.iml | 0 .../Task1-InputMismatchException}/src/Main.java | 0 24 files changed, 0 insertions(+), 0 deletions(-) rename {Task 2-Word Count => Week 5/Task 2-Word Count}/.gitignore (100%) rename {Task 2-Word Count => Week 5/Task 2-Word Count}/.idea/.gitignore (100%) rename {Task 2-Word Count => Week 5/Task 2-Word Count}/.idea/misc.xml (100%) rename {Task 2-Word Count => Week 5/Task 2-Word Count}/.idea/modules.xml (100%) rename {Task 2-Word Count => Week 5/Task 2-Word Count}/.idea/vcs.xml (100%) rename {Task 2-Word Count => Week 5/Task 2-Word Count}/Lincoln.txt (100%) rename {Task 2-Word Count => Week 5/Task 2-Word Count}/Task 2-Word Count.iml (100%) rename {Task 2-Word Count => Week 5/Task 2-Word Count}/src/Main.java (100%) rename {Task 3 => Week 5/Task 3}/.gitignore (100%) rename {Task 3 => Week 5/Task 3}/.idea/.gitignore (100%) rename {Task 3 => Week 5/Task 3}/.idea/misc.xml (100%) rename {Task 3 => Week 5/Task 3}/.idea/modules.xml (100%) rename {Task 3 => Week 5/Task 3}/.idea/vcs.xml (100%) rename {Task 3 => Week 5/Task 3}/Scores.txt (100%) rename {Task 3 => Week 5/Task 3}/Task 3.iml (100%) rename {Task 3 => Week 5/Task 3}/src/FileManipulation.java (100%) rename {Task 3 => Week 5/Task 3}/src/Main.java (100%) rename {Task1-InputMismatchException => Week 5/Task1-InputMismatchException}/.gitignore (100%) rename {Task1-InputMismatchException => Week 5/Task1-InputMismatchException}/.idea/.gitignore (100%) rename {Task1-InputMismatchException => Week 5/Task1-InputMismatchException}/.idea/misc.xml (100%) rename {Task1-InputMismatchException => Week 5/Task1-InputMismatchException}/.idea/modules.xml (100%) rename {Task1-InputMismatchException => Week 5/Task1-InputMismatchException}/.idea/vcs.xml (100%) rename {Task1-InputMismatchException => Week 5/Task1-InputMismatchException}/InputMismatchException.iml (100%) rename {Task1-InputMismatchException => Week 5/Task1-InputMismatchException}/src/Main.java (100%) diff --git a/Task 2-Word Count/.gitignore b/Week 5/Task 2-Word Count/.gitignore similarity index 100% rename from Task 2-Word Count/.gitignore rename to Week 5/Task 2-Word Count/.gitignore diff --git a/Task 2-Word Count/.idea/.gitignore b/Week 5/Task 2-Word Count/.idea/.gitignore similarity index 100% rename from Task 2-Word Count/.idea/.gitignore rename to Week 5/Task 2-Word Count/.idea/.gitignore diff --git a/Task 2-Word Count/.idea/misc.xml b/Week 5/Task 2-Word Count/.idea/misc.xml similarity index 100% rename from Task 2-Word Count/.idea/misc.xml rename to Week 5/Task 2-Word Count/.idea/misc.xml diff --git a/Task 2-Word Count/.idea/modules.xml b/Week 5/Task 2-Word Count/.idea/modules.xml similarity index 100% rename from Task 2-Word Count/.idea/modules.xml rename to Week 5/Task 2-Word Count/.idea/modules.xml diff --git a/Task 2-Word Count/.idea/vcs.xml b/Week 5/Task 2-Word Count/.idea/vcs.xml similarity index 100% rename from Task 2-Word Count/.idea/vcs.xml rename to Week 5/Task 2-Word Count/.idea/vcs.xml diff --git a/Task 2-Word Count/Lincoln.txt b/Week 5/Task 2-Word Count/Lincoln.txt similarity index 100% rename from Task 2-Word Count/Lincoln.txt rename to Week 5/Task 2-Word Count/Lincoln.txt diff --git a/Task 2-Word Count/Task 2-Word Count.iml b/Week 5/Task 2-Word Count/Task 2-Word Count.iml similarity index 100% rename from Task 2-Word Count/Task 2-Word Count.iml rename to Week 5/Task 2-Word Count/Task 2-Word Count.iml diff --git a/Task 2-Word Count/src/Main.java b/Week 5/Task 2-Word Count/src/Main.java similarity index 100% rename from Task 2-Word Count/src/Main.java rename to Week 5/Task 2-Word Count/src/Main.java diff --git a/Task 3/.gitignore b/Week 5/Task 3/.gitignore similarity index 100% rename from Task 3/.gitignore rename to Week 5/Task 3/.gitignore diff --git a/Task 3/.idea/.gitignore b/Week 5/Task 3/.idea/.gitignore similarity index 100% rename from Task 3/.idea/.gitignore rename to Week 5/Task 3/.idea/.gitignore diff --git a/Task 3/.idea/misc.xml b/Week 5/Task 3/.idea/misc.xml similarity index 100% rename from Task 3/.idea/misc.xml rename to Week 5/Task 3/.idea/misc.xml diff --git a/Task 3/.idea/modules.xml b/Week 5/Task 3/.idea/modules.xml similarity index 100% rename from Task 3/.idea/modules.xml rename to Week 5/Task 3/.idea/modules.xml diff --git a/Task 3/.idea/vcs.xml b/Week 5/Task 3/.idea/vcs.xml similarity index 100% rename from Task 3/.idea/vcs.xml rename to Week 5/Task 3/.idea/vcs.xml diff --git a/Task 3/Scores.txt b/Week 5/Task 3/Scores.txt similarity index 100% rename from Task 3/Scores.txt rename to Week 5/Task 3/Scores.txt diff --git a/Task 3/Task 3.iml b/Week 5/Task 3/Task 3.iml similarity index 100% rename from Task 3/Task 3.iml rename to Week 5/Task 3/Task 3.iml diff --git a/Task 3/src/FileManipulation.java b/Week 5/Task 3/src/FileManipulation.java similarity index 100% rename from Task 3/src/FileManipulation.java rename to Week 5/Task 3/src/FileManipulation.java diff --git a/Task 3/src/Main.java b/Week 5/Task 3/src/Main.java similarity index 100% rename from Task 3/src/Main.java rename to Week 5/Task 3/src/Main.java diff --git a/Task1-InputMismatchException/.gitignore b/Week 5/Task1-InputMismatchException/.gitignore similarity index 100% rename from Task1-InputMismatchException/.gitignore rename to Week 5/Task1-InputMismatchException/.gitignore diff --git a/Task1-InputMismatchException/.idea/.gitignore b/Week 5/Task1-InputMismatchException/.idea/.gitignore similarity index 100% rename from Task1-InputMismatchException/.idea/.gitignore rename to Week 5/Task1-InputMismatchException/.idea/.gitignore diff --git a/Task1-InputMismatchException/.idea/misc.xml b/Week 5/Task1-InputMismatchException/.idea/misc.xml similarity index 100% rename from Task1-InputMismatchException/.idea/misc.xml rename to Week 5/Task1-InputMismatchException/.idea/misc.xml diff --git a/Task1-InputMismatchException/.idea/modules.xml b/Week 5/Task1-InputMismatchException/.idea/modules.xml similarity index 100% rename from Task1-InputMismatchException/.idea/modules.xml rename to Week 5/Task1-InputMismatchException/.idea/modules.xml diff --git a/Task1-InputMismatchException/.idea/vcs.xml b/Week 5/Task1-InputMismatchException/.idea/vcs.xml similarity index 100% rename from Task1-InputMismatchException/.idea/vcs.xml rename to Week 5/Task1-InputMismatchException/.idea/vcs.xml diff --git a/Task1-InputMismatchException/InputMismatchException.iml b/Week 5/Task1-InputMismatchException/InputMismatchException.iml similarity index 100% rename from Task1-InputMismatchException/InputMismatchException.iml rename to Week 5/Task1-InputMismatchException/InputMismatchException.iml diff --git a/Task1-InputMismatchException/src/Main.java b/Week 5/Task1-InputMismatchException/src/Main.java similarity index 100% rename from Task1-InputMismatchException/src/Main.java rename to Week 5/Task1-InputMismatchException/src/Main.java