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 5 of 5

Thread: Need help inputting and writing to a external data file.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Need help inputting and writing to a external data file.

    Hi guys here is my assignment for a brief history.

    Project 9
    Random Monoalphabet Cipher
    Random monoalphabet cipher. The Caesar cipher, which shifts all letters by a fixed amount, is ridiculously easy to crack - just try out all 25 possible keys. Here=s a better idea . For the key, don=t use numbers but words. Suppose the key word is FEATHER. First remove the duplicate letters, yielding FEATHR, and append the other letters of the alphabet in reverse order. Now encrypt the letters as follows

    A = F
    B = E
    C = A
    D = T
    E = H
    F = R
    G = Z
    H = Y
    I = X
    et etc

    Write a program that encrypts or decrypts a file using this cipher. For example, decrypt a file using the keyword FEATHER. It is an error not to supply a keyword.
    You will prepare a file encrypted using this keyword cipher. Make the keyword the first word
    in the file. You will give your input file to me and I will pass it to another student so that s/he can decrypt it. You must also be prepared to decrypt the file s/he hands you, using this cipher.
    You are obviously forbidden to use FEATHER as your keyword for the beta-testing.
    Please name the data files for your program TEST1, TEST2, etc.. This is make beta-testing run more smoothly.
    Assume that each file will contain only alphabetic characters and spaces between words.
    You are not to accommodate digits, punctuation or any other special characters in your program code.

    Here is my code.

     
    import java.util.*;
    import java.io.*;
     
    public class Project9
    {
     
         public static String encrypt(String msg, String cw)
        {
         String encryptionMessage = new String();
         msg = msg.toUpperCase();
         cw = cw.toUpperCase();
         int i;
     
            for( i = 0; i < msg.length(); i++)
            {
                char ch = msg.charAt(i);
                int shift = (cw.charAt( i% cw.length() )-'A');
                int oldPositionInAlphabet = ch - 'A';
                int newPositionInAlphabet = (oldPositionInAlphabet + shift)%26;
                encryptionMessage = encryptionMessage + (char)(newPositionInAlphabet+ 'A'); 
            }
     
         return encryptionMessage;
        }
     
     
        public static void main (String[] args) throws IOException
        {
            Scanner scan = new Scanner(System.in);
            String unencryptedFile; 
     
            System.out.print( "Please input your Unencrypted file: " );
            unencryptedFile = scan.next();
     
            File inputFile = new File(unencryptedFile);
            Scanner scanOne = new Scanner(inputFile);
     
            if(!inputFile.exists())
            {
                System.out.println("This file does not exist!");
                System.exit(0);
            }
     
            String codeWord;
            System.out.println( "Please enter your code word. " );
            codeWord = scan.nextLine();
     
            File outFile = new File(unencryptedFile);
            PrintWriter pw = new PrintWriter(outFile);
     
            while(outFile.exists())
            {
                String line = scanOne.nextLine();
                String encryptedLine = encrypt(line,codeWord);
                pw.println(encryptedLine);
                pw.close();
            }
     
            scanOne.close();
     
     
     
        }
     
    }

    For some reason I keep getting an error message pertaining to my data file. I don't know what else to do. Here is input and error message below.

    Please input your Unencrypted file: TEST.dat

    java.io.FileNotFoundException: TEST.dat (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
    at java.util.Scanner.<init>(Scanner.java:656)
    at Project9.main(Project9.java:42)

    Does anyone have any suggestions?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,647
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Need help inputting and writing to a external data file.

    (The system cannot find the file specified)
    Move the file to the location where the program is looking for the file
    or change where the program is looking to be where the file is located.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    110
    My Mood
    Where
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help inputting and writing to a external data file.

    That's what I was thinking so should I hard code the file directory. Like the C:\\Users\\gshavers\\..... And then allow the program to search for the file in this directory.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,647
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Need help inputting and writing to a external data file.

    To see where the program is looking for the file, add a println() statement that prints out the absolute path for the inputFile File object.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Need help inputting and writing to a external data file.

    Since it is not entering the if-statement of if(!inputFile.exists()), the representation of the file or the file itself exists. It's now a matter of figuring out what the compiler thinks the file path is, as it may differ from what you're expecting. To test this, add the following in your code:

    String path = inputFile.getAbsolutePath();

    Also, why do you have 2 instances of File as inputFile and outFile? You shouldn't have 2 unless they each refer to a different file or directory.

Similar Threads

  1. Writing data to binary file.
    By MateuszS in forum Java Theory & Questions
    Replies: 1
    Last Post: November 16th, 2012, 09:18 AM
  2. Replies: 1
    Last Post: February 27th, 2012, 08:16 AM
  3. Writing formatted data to file
    By kafka82 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 20th, 2011, 03:07 PM
  4. displaying dyanamic data on jsp while core engine is writing it on text file
    By abhyudayaupadhyay in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: May 24th, 2011, 10:03 AM
  5. Replies: 1
    Last Post: April 7th, 2011, 01:32 PM