Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import acm.program.ConsoleProgram;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Task4 extends ConsoleProgram {
- public void run(){
- int key=readInt();
- String allFile = fileReader();
- char[] text = allFile.toCharArray();
- String res="";
- char[] buffer = new char[allFile.length()];
- char[] upper =
- {'A','B','C','D','E','F','G','H','I','J','K','L','M','N', 'O','P','Q',
- 'R','S','T','U','V','W','X','Y','Z'};
- char[] lower= {'a','b','c','d','e','f','g','h',
- 'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
- for(int i=0;i<allFile.length();i++){
- for(int j=0;j<26;j++){
- if(text[i]==upper[j]){
- if (j!=25-key){
- res+=upper[j+key];
- }else{
- res+=upper[j-26+key];
- }
- }
- if(text[i]==lower[j]){
- if (j!=25){
- res+=lower[j+key];
- }else{
- res+=lower[j-26+key];
- }
- }
- }
- }
- println(res);
- try {
- fileWriter("out.txt", res);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public String fileReader() {
- FileReader fr = null;
- try {
- fr = new FileReader("C:\\Users\\БарабухаМаріяМаксимі\\Desktop\\home11\\in.txt");
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Scanner scan = new Scanner(fr);
- String str = "";
- boolean f = true;
- while (scan.hasNextLine()) {
- if (f) {
- f = false;
- str = scan.nextLine();
- } else {
- str = str + "\n" + scan.nextLine();
- }
- }
- return str;
- }
- public void fileWriter(String fileName, String str) throws IOException {
- FileWriter nFile = new FileWriter(fileName);
- nFile.write(str+"\n");
- nFile.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement