-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package me.alex4386.gachon.sw14462.day23; | ||
|
||
import me.alex4386.gachon.sw14462.utils.Chainloader; | ||
|
||
public class Main { | ||
public static String chainloadTarget = "ex13_1b"; | ||
|
||
public static void main(String[] args) throws Throwable { | ||
String packageName = Main.class.getPackage().getName(); | ||
String chainLoadTargetClass = packageName + "." + chainloadTarget + ".Main"; | ||
|
||
try { | ||
Chainloader.chainloadTarget(chainLoadTargetClass, args); | ||
} catch (Exception e) { | ||
throw e; | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/java/me/alex4386/gachon/sw14462/day23/ex13_1a/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package me.alex4386.gachon.sw14462.day23.ex13_1a; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.PrintWriter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
System.out.print("Enter the filename: "); | ||
String filename = scanner.nextLine(); | ||
|
||
// generate 10 random integer | ||
int[] randomIntegers = new int[10]; | ||
for (int i = 0; i < 10; i++) { | ||
randomIntegers[i] = (int) (Math.random() * 1000); | ||
} | ||
|
||
// write to file | ||
File targetFile = new File(filename); | ||
if (targetFile.exists()) { | ||
// unlink the file | ||
targetFile.delete(); | ||
} | ||
|
||
FileOutputStream stream; | ||
try { | ||
targetFile.createNewFile(); | ||
stream = new FileOutputStream(targetFile); | ||
} catch (Exception e) { | ||
System.err.println("Failed to create file: " + e.getMessage()); | ||
return; | ||
} | ||
|
||
PrintWriter writer = new PrintWriter(stream); | ||
for (int i = 0; i < 10; i++) { | ||
writer.println(randomIntegers[i]); | ||
} | ||
|
||
writer.close(); | ||
System.out.println("File created successfully."); | ||
|
||
scanner.close(); | ||
|
||
// reopen the file | ||
targetFile = new File(filename); | ||
if (!targetFile.exists()) { | ||
System.err.println("File does not exists, is the file has been deleted in the meantime?"); | ||
return; | ||
} | ||
|
||
try { | ||
scanner = new Scanner(targetFile); | ||
} catch (Exception e) { | ||
System.err.println("Failed to open file: " + e.getMessage()); | ||
return; | ||
} | ||
|
||
int count = 0; | ||
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, sum = 0; | ||
double average; | ||
|
||
List<Integer> integers = new ArrayList<>(); | ||
while (scanner.hasNextLine()) { | ||
String content = scanner.nextLine(); | ||
if (content.trim().equals("")) continue; | ||
|
||
int value = 0; | ||
try { | ||
value = Integer.parseInt(content); | ||
} catch (NumberFormatException e) { | ||
System.err.println("Invalid number: " + e.getMessage()); | ||
continue; | ||
} | ||
|
||
integers.add(value); | ||
count++; | ||
sum += value; | ||
if (value < min) min = value; | ||
if (value > max) max = value; | ||
} | ||
|
||
average = (double) sum / count; | ||
scanner.close(); | ||
|
||
|
||
System.out.println("min: "+min); | ||
System.out.println("max: "+max); | ||
System.out.println("sum: "+sum); | ||
System.out.println("average: "+average); | ||
|
||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/me/alex4386/gachon/sw14462/day23/ex13_1b/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package me.alex4386.gachon.sw14462.day23.ex13_1b; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.util.Scanner; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
String targetFilename = "names.txt"; | ||
String firstNameCache = ""; | ||
|
||
File file = new File(targetFilename); | ||
if (!file.exists()) { | ||
System.err.println("File not found: " + targetFilename); | ||
System.exit(1); | ||
} | ||
|
||
Scanner scanner; | ||
try { | ||
scanner = new Scanner(file); | ||
} catch (FileNotFoundException e) { | ||
System.err.println("File not found: " + e.getMessage()); | ||
System.exit(1); | ||
return; | ||
} | ||
|
||
while (scanner.hasNextLine()) { | ||
String line = scanner.nextLine(); | ||
String[] names = line.split(" "); | ||
|
||
for (int i = 0; i < names.length; i++) { | ||
if (firstNameCache.equals("")) { | ||
firstNameCache = names[i]; | ||
} else { | ||
System.out.println(firstNameCache + " " + names[i]); | ||
firstNameCache = ""; | ||
} | ||
} | ||
} | ||
|
||
scanner.close(); | ||
} | ||
} |