SHARE
TWEET

Untitled

a guest Apr 20th, 2019 106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class ExtractMessage {
  7.    
  8.     public static void main(String[] args) throws FileNotFoundException {
  9.         Scanner in = new Scanner(System.in);
  10.         int[] fileData = null;
  11.         do {
  12.             System.out.println("Please enter the file name: ");
  13.             String fileName = in.nextLine();
  14.             fileData = getCodes(fileName);
  15.         } while (fileData == null);
  16.         in.close();
  17.         System.out.println("The secret message was: " + getSecretMessage(fileData));
  18.     }
  19.    
  20.     private static int[] getCodes(String filename) throws FileNotFoundException {
  21.         File file = new File(filename);
  22.         if (!file.exists()) {
  23.             System.out.println("File does not exist.");
  24.             return null;
  25.         }
  26.         Scanner input = new Scanner(file);
  27.         int[] codes = null;
  28.         while (input.hasNext()) {
  29.             if (input.hasNextInt()) {
  30.                 int num = input.nextInt();
  31.                 if (codes == null) {
  32.                     codes = new int[1];
  33.                     codes[0] = num;
  34.                 } else {
  35.                     codes = Arrays.copyOf(codes, codes.length + 1);
  36.                     codes[codes.length - 1] = num;
  37.                 }
  38.             } else {
  39.                 input.next();
  40.             }
  41.         }
  42.         return codes;
  43.     }
  44.    
  45.     private static String getSecretMessage(int[] codes) {
  46.         String msg = "";
  47.         for (int i = 0; i < codes.length; i++) {
  48.             msg += (char)(codes[i] - codes.length);
  49.         }
  50.         return msg;
  51.     }
  52.    
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top