Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @(#)Compiler.java
- *
- *
- * @author
- * @version 1.00 2012/5/19
- */
- import java.util.*;
- import java.util.Random;
- import java.util.Scanner;
- import java.*;
- import java.io.*;
- import java.lang.*;
- public class Compiler {
- public static void main (String args[]) throws IOException
- {
- BufferedWriter outStream= new BufferedWriter(new FileWriter("encoded.txt"));
- Scanner input = new Scanner(System.in);
- Scanner confirmation = new Scanner(System.in);
- Random randomGenerator = new Random();
- double rand = (Math.random()*4);
- int randInt =(int)(rand/1);
- randInt = randInt + 1;
- input.useDelimiter (";");
- int cont = 0;
- String in = " ";
- String fileInput = " ";
- while(cont==0)
- {
- System.out.println("Would you like to read from a file or text input in this window? Answer with \"1\" or \"2\" for the respective method");
- int confirm = confirmation.nextInt();
- System.out.println();
- if(confirm == 1)
- {
- cont = 1;
- System.out.println("you have chosen \"file\". Pulling data from the file \"to_encode.txt\"");
- File srcFile = new File("to_encode.txt");
- BufferedReader intwo = new BufferedReader(new FileReader(srcFile));
- fileInput = intwo.readLine();
- intwo.close();
- }
- if(confirm == 2)
- {
- System.out.print("Enter code to be encoded. End the text with a semicolon(;): ");
- in = input.next();
- System.out.println();
- cont = 2;
- }
- if(cont != 1 && cont !=2)
- {
- System.out.println("That is not a valid choice, please try again");
- cont = 0;
- }
- }
- if(cont == 1)
- {
- in = fileInput;
- }
- Random r = new Random();
- int x = in.length();
- final int lgh = in.length();
- String next;
- String left;
- left = in;
- char c;
- String ca;
- String temp;
- String stringCode = " ";
- for(int z = 0; z<lgh;z=z+1) // repeat once for every character in the input string remaining
- {
- for(int y=0;y<randInt;y++) //repeat twice
- {
- c = (char)(r.nextInt(26) + 'a'); //generate a random character (lowercase)
- ca = Character.toString(c);
- stringCode = stringCode + ca; //add a random character to the encoded string
- }
- x = left.length();
- next = left.substring(0,1);
- stringCode = stringCode + next;
- left = left.substring(1,x);
- }
- stringCode = stringCode.substring(randInt+1,stringCode.length());
- String output = "";
- if(randInt==1)
- {
- output = "a"+stringCode;
- }
- if(randInt==2)
- {
- output = "b"+stringCode;
- }
- if(randInt==3)
- {
- output = "c"+stringCode;
- }
- if(randInt==4)
- {
- output = "d"+stringCode;
- }
- if(randInt==5)
- {
- output = "e"+stringCode;
- }
- else
- {
- }
- System.out.println(output);
- System.out.println("is writing to \"encoded.txt\"");
- outStream.newLine();
- outStream.write(output);
- outStream.close();
- System.out.print("Writing has finished. ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement