Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: CaesarCipher

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default CaesarCipher

    Im trying to encrypt a .txt file using the CaesarCipher method.
    Below is what i have so far after roughly following an example in Horstmann's book.

    I get errors when trying to compile "reader" and im pretty sure there is an error when trying to initialize encrypt scanner. Any help would be great.

    PHP Code:
    import java.util.Scanner;
    import java.io.*;

    public class 
    CaesarCipher
    {
        public 
    CaesarCipher(int aKey)
        {
            
    Key 4;
        }
        public 
    void encryptScanner(Scanner inPrintWriter out)
        
    throws exception IOException
        
    {
            
    boolean done false;
            while (!
    done)
            {
                
    int next in.read();
                if (
    next == -1done true;
                else
                {
                    
    char c = (charnext;
                    
    char b encrypt(c);
                    
    out.write(c);
                }
            }
        }
        public 
    char encrypt(char c)
        {
            return (
    char) (Key);
        }
        private 
    int Key;

    PHP Code:
    import java.io.FileReader;
    import java.util.Scanner;
    import java.io.*;

    public class 
    reader
    {
        public static 
    void mainString[] args)
        
    throws IOException
        
    {
            
    File file = new File("poem.txt");
            
    Scanner in = new Scanner(file);
            
    PrintWriter out = new PrintWriter("encrypted.txt");
            
            
    CaesarCipher cipher = new CaesarCipher(Key);
            
    cipher.encryptScanner(ScannerPrintWriter);
            
        
            
    in.close();
            
    out.close();
        }


  2. Default Related threads:


  3. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CaesarCipher

    I believe this line to be the culprit in reader.java:
    PrintWriter out = new PrintWriter("encrypted.txt");
    If that's of no help take a look at: PrintWriter (Java 2 Platform SE v1.4.2) will tell you everything you wanted to know about PrintWriters and more. Compare how you've used the printwriter to the notes about how to use it in that way on that page. Maybe I'm wrong on this one, but I'm pretty sure that's at least not a preferred way to do things.

    Also think about the arguments you've passed to the encryptScanner() method, they aren't variables, what are they?

  4. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: CaesarCipher

    CaesarCipher cipher = new CaesarCipher(Key);

    May I ask where the variable 'Key' comes from in your reader class?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #4
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CaesarCipher

    The key from the example im following originally came from a keyboard input but im trying to define it in the first piece of code. As for the Print writer, would i be better using FileWriter?

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: CaesarCipher

    I get errors when trying to compile "reader"
    Please copy and paste here the full text of the error messages.

  7. #6
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CaesarCipher

    reader.java:14: cannot find symbol
    symbol : class CaesarCipher
    location: class reader
    CaesarCipher cipher = new CaesarCipher();
    ^
    reader.java:14: cannot find symbol
    symbol : class CaesarCipher
    location: class reader
    CaesarCipher cipher = new CaesarCipher();
    ^
    reader.java:15: cannot find symbol
    symbol : variable Scanner
    location: class reader
    cipher.encryptScanner(Scanner, FileWriter);
    ^
    reader.java:15: cannot find symbol
    symbol : variable FileWriter
    location: class reader
    cipher.encryptScanner(Scanner, FileWriter);
    ^
    4 errors

    Those are the 4 errors i get, but im still not convinced that the code is right for cipher.encryptScanner(Scanner, FileWriter) and the relating section in CaesarCipher.

    Thanks for the replies so far.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: CaesarCipher

    Does this error message go with the code you posted for the reader class?
    Error message source reference: CaesarCipher cipher = new CaesarCipher();
    reader class source: CaesarCipher cipher = new CaesarCipher(Key);

    The code you posted is different from the code in the error message.
    Can you explain?

  9. #8
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CaesarCipher

    Oh sorry Norm!
    I just took out (Key) to see if it made a difference but hadnt put it back in. It adds another error in as you can see below.

    symbol : class CaesarCipher
    location: class reader
    CaesarCipher cipher = new CaesarCipher(Key);
    ^
    reader.java:14: cannot find symbol
    symbol : class CaesarCipher
    location: class reader
    CaesarCipher cipher = new CaesarCipher(Key);
    ^
    reader.java:14: cannot find symbol
    symbol : variable Key
    location: class reader
    CaesarCipher cipher = new CaesarCipher(Key);
    ^
    reader.java:15: cannot find symbol
    symbol : variable Scanner
    location: class reader
    cipher.encryptScanner(Scanner, FileWriter);
    ^
    reader.java:15: cannot find symbol
    symbol : variable FileWriter
    location: class reader
    cipher.encryptScanner(Scanner, FileWriter);
    ^
    5 errors

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: CaesarCipher

    Did the CaesarCipher class compile OK? Where is its .class file?

    cipher.encryptScanner(Scanner, FileWriter);
    You need to use variable names here, not the names of the classes.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: CaesarCipher

    cannot find symbol
    symbol : variable Key
    Where is the variable Key defined?

  12. #11
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CaesarCipher

    So the variables would be file and out then? or in and out?

    CaesarCipher.java:17: cannot find symbol
    symbol : method read()
    location: class java.util.Scanner
    int next = in.read();
    ^
    1 error

    It did compile but not now for some reason, i had one error with throws exception IOException so changed it to throws IOException. After i fixed that the above error came about.

  13. #12
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CaesarCipher

    Quote Originally Posted by Norm View Post
    Where is the variable Key defined?
    PHP Code:
    public class CaesarCipher
    {
        public 
    CaesarCipher(int aKey)
        {
            
    Key 4
    On the original example this was defined by the user inputting a value of their choice. I decided to just try and define it with no user input needed.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: CaesarCipher

    Key = 4;
    That does not define the varible Key. It gives the variable Key a value of 4.
    You need a data type before a variable name to define a variable:
    <datatype> var_name = ...
    where <datatype> is the data type of the variable. For example: int, double, CaesarCipher (your class is a data type)

    cannot find symbol
    symbol : method read()
    What class is the method read() defined in?
    Is the variable in an instance of that class?

    Read the API doc for the class: Java Platform SE 6

  15. #14
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: CaesarCipher

    To be honest, I reckon it would be best if you left this project alone for now OP.
    Go back and learn some more basic principles and you'll be able to conquer this far easier.
    Just my 2c.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code