Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Arrays;
- import java.util.Scanner;
- public class ExtractMessage {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner in = new Scanner(System.in);
- int[] fileData = null;
- do {
- System.out.println("Please enter the file name: ");
- String fileName = in.nextLine();
- fileData = getCodes(fileName);
- } while (fileData == null);
- in.close();
- System.out.println("The secret message was: " + getSecretMessage(fileData));
- }
- private static int[] getCodes(String filename) throws FileNotFoundException {
- File file = new File(filename);
- if (!file.exists()) {
- System.out.println("File does not exist.");
- return null;
- }
- Scanner input = new Scanner(file);
- int[] codes = null;
- while (input.hasNext()) {
- if (input.hasNextInt()) {
- int num = input.nextInt();
- if (codes == null) {
- codes = new int[1];
- codes[0] = num;
- } else {
- codes = Arrays.copyOf(codes, codes.length + 1);
- codes[codes.length - 1] = num;
- }
- } else {
- input.next();
- }
- }
- return codes;
- }
- private static String getSecretMessage(int[] codes) {
- String msg = "";
- for (int i = 0; i < codes.length; i++) {
- msg += (char)(codes[i] - codes.length);
- }
- return msg;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement