Computer Science Canada

Rot13 encryption problem

Author:  Fifa4ever [ Fri Apr 20, 2012 5:35 pm ]
Post subject:  Rot13 encryption problem

MY problem is that I can not seem to be getting any information from "test.txt"
the other thing i need is i need to then encrypt "test.txt" and save it in a file specified by the user.
im pretty stuck, because my teacher is not much help at all.

menu class


    // import and package//
    package rot;
    import java.io.*;
    public class Menu {


    public static void main ( String[] args ) throws Exception{

    // Accessing other classes
    //Ciper r = new Ciper();
    Writetofile g = new Writetofile ();
    Dtos e = new Dtos();
    // user input variables
    String input;
    int select1;
    BufferedReader stdin = new BufferedReader (
    new InputStreamReader (System.in));


    //main menu
    System.out.println("Hello and welcome to my ROT13 cipher java program" );
    System.out.println("1. Encrypt");
    System.out.println("2. Decrypt");
    System.out.println("3. Exit");
    System.out.println("Please input the corresponding number" );
    input = stdin.readLine ();
    select1 = (int) Integer.parseInt (input);

    if (select1 == 1) {
    System.out.println("Accessing File ");





    //g.openFile();
    //g.readFile();
    //r.encrypt();
    //g.closeFile();


    System.out.println("Loading Please wait !");
    System.out.println("");
    System.out.println("File Message.txt is now Encrypted.!!");


    }
    else if (select1 == 2) {

    System.out.println("Decrypting Message.txt! ");




    } else if (select1 == 3) {
    System.out.println("Thank you for using my program!!");
    }






    }
    }



class with all my methods in it


    package rot;

    import java.util.Formatter;
    import java.util.Scanner;
    import java.io.*;
    public class Writetofile {

    //Ciper u = new Ciper ();
    int y;
    private Formatter k;
    public Scanner x;
    String plaintext;
    String fileName = new String ();

    String str;
    FileWriter fw;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


    public void openFile(String o){
    try {
    System.out.println("Messages.txt be encrypted or decrypted:");
    x = new Scanner (new File (o));
    }
    catch (Exception e) {
    System.out.println("Could not find file.Bye!");
    }
    }



    public void closeFile () {
    k.close();
    }



    public String readFile(String o){
    while (x.hasNextLine()){
    System.out.println(x.nextLine());

    }
    return o;

    }



    public void encrypt(){
    y = plaintext.length();
    char[] chars = new char[y];
    for (int i = 0; i < y; i++)
    {
    chars[i] = plaintext.charAt(i);
    }

    for (int i = 0; i < chars.length; i++)
    {
    if ((chars[i] >= 'a' && chars[i] <= 'm'))
    {
    chars[i] += 13;
    }
    else if ((chars[i] >= 'n' && chars[i] <= 'z'))
    {
    chars[i] -= 13;
    }
    else if ((chars[i] >= 'A' && chars[i] <= 'M'))
    {
    chars[i] += 13;
    }
    else if ((chars[i] >= 'N' && chars[i] <= 'Z'))
    {
    chars[i] -= 13;
    }
    System.out.print(chars[i]);
    }
    return fileName;

    }





    }












Author:  Zren [ Sat Apr 21, 2012 3:40 am ]
Post subject:  RE:Rot13 encryption problem

Assuming you didn't forget to uncomment the function calls...

Java:

//g.openFile();
public void openFile(String o){


What's wrong with the first line after looking at the second?


As for writing to a file: http://lmgtfy.com/?q=bufferedwriter+example You can also use the PrintWriter class if you want similar methods to those found in System.out.*.

Actually, I'm amazed the teacher isn't helping you with a standard in/out task. Did you skimp reading course reading?

Author:  Fifa4ever [ Sat Apr 21, 2012 2:52 pm ]
Post subject:  Re: Rot13 encryption problem

okay so i believe that i will now be able to write to a new file. I just need help in actually reading the file. maybe someone could check over my readtofile method . If anyone has the time to check it over, would be appreciated!

Author:  Zren [ Sun Apr 22, 2012 11:41 pm ]
Post subject:  RE:Rot13 encryption problem

This reads a file fine:

Java:

public class Test {
        public static void main(String[] args) {
                File inputFile = new File("in.txt");

                try {
                        Scanner in = new Scanner(inputFile);
                        while (in.hasNextLine()) {
                                System.out.println(in.nextLine());
                        }

                        in.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
}


I don't see why it wouldn't work unless it's the way you've structured your code. Does your menu work?

Author:  Fifa4ever [ Mon Apr 23, 2012 8:19 am ]
Post subject:  Re: Rot13 encryption problem

alright got some free time on my hands, ill get back if i need anymore help


: