Advertisement
azulflame

compiler

May 30th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /**
  2. * @(#)Compiler.java
  3. *
  4. *
  5. * @author
  6. * @version 1.00 2012/5/19
  7. */
  8. import java.util.*;
  9. import java.util.Random;
  10. import java.util.Scanner;
  11. import java.*;
  12. import java.io.*;
  13. import java.lang.*;
  14.  
  15. public class Compiler {
  16. public static void main (String args[]) throws IOException
  17. {
  18.  
  19. BufferedWriter outStream= new BufferedWriter(new FileWriter("encoded.txt"));
  20. Scanner input = new Scanner(System.in);
  21. Scanner confirmation = new Scanner(System.in);
  22. Random randomGenerator = new Random();
  23. double rand = (Math.random()*4);
  24. int randInt =(int)(rand/1);
  25. randInt = randInt + 1;
  26. input.useDelimiter (";");
  27. int cont = 0;
  28. String in = " ";
  29. String fileInput = " ";
  30. while(cont==0)
  31. {
  32. 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");
  33. int confirm = confirmation.nextInt();
  34. System.out.println();
  35. if(confirm == 1)
  36. {
  37. cont = 1;
  38. System.out.println("you have chosen \"file\". Pulling data from the file \"to_encode.txt\"");
  39. File srcFile = new File("to_encode.txt");
  40. BufferedReader intwo = new BufferedReader(new FileReader(srcFile));
  41. fileInput = intwo.readLine();
  42.  
  43. intwo.close();
  44.  
  45. }
  46. if(confirm == 2)
  47. {
  48. System.out.print("Enter code to be encoded. End the text with a semicolon(;): ");
  49. in = input.next();
  50. System.out.println();
  51. cont = 2;
  52. }
  53. if(cont != 1 && cont !=2)
  54. {
  55. System.out.println("That is not a valid choice, please try again");
  56. cont = 0;
  57. }
  58.  
  59. }
  60. if(cont == 1)
  61. {
  62. in = fileInput;
  63. }
  64. Random r = new Random();
  65. int x = in.length();
  66. final int lgh = in.length();
  67. String next;
  68. String left;
  69. left = in;
  70. char c;
  71. String ca;
  72. String temp;
  73. String stringCode = " ";
  74. for(int z = 0; z<lgh;z=z+1) // repeat once for every character in the input string remaining
  75. {
  76. for(int y=0;y<randInt;y++) //repeat twice
  77. {
  78. c = (char)(r.nextInt(26) + 'a'); //generate a random character (lowercase)
  79. ca = Character.toString(c);
  80. stringCode = stringCode + ca; //add a random character to the encoded string
  81. }
  82.  
  83.  
  84. x = left.length();
  85. next = left.substring(0,1);
  86. stringCode = stringCode + next;
  87. left = left.substring(1,x);
  88. }
  89.  
  90. stringCode = stringCode.substring(randInt+1,stringCode.length());
  91.  
  92. String output = "";
  93. if(randInt==1)
  94. {
  95. output = "a"+stringCode;
  96. }
  97. if(randInt==2)
  98. {
  99. output = "b"+stringCode;
  100. }
  101. if(randInt==3)
  102. {
  103. output = "c"+stringCode;
  104. }
  105. if(randInt==4)
  106. {
  107. output = "d"+stringCode;
  108. }
  109. if(randInt==5)
  110. {
  111. output = "e"+stringCode;
  112. }
  113. else
  114. {
  115.  
  116. }
  117.  
  118.  
  119.  
  120. System.out.println(output);
  121. System.out.println("is writing to \"encoded.txt\"");
  122. outStream.newLine();
  123. outStream.write(output);
  124. outStream.close();
  125. System.out.print("Writing has finished. ");
  126. }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement