Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

I have no idea what I'm doing

Name: Anonymous 2011-04-03 20:43

Here's my story:
I'm writing a basic program that "encrypts" a .txt file by converting characters to ASCII values, and the program compiles with no problems.
However, the program hangs when printing to a new file. The file is created, but cannot be opened and has no text within.

Can anyone help? I'm not good with I/O operations, and I haven't coded in Java for a long time.
Here's my code:

//Encryption.java
//This program reads a text file and creates an encrypted version of the text file

import java.util.*;
import java.io.*;

public class Encryption {
    //declare variables
    public static String line;
    public static int lineEncrypted;
    public static int i;
    public static int lineInt;
    public static char lineChar;

    public static void main(String[] args) throws IOException {
        System.out.println("Input file: Secret.txt");

        //input file
        BufferedReader in = new BufferedReader(new FileReader("Secret.txt"));
        line = in.readLine();

        //while there is more data to read
        while(line != null) {
            encrypt();
            line = in.readLine();
        }
    }

    public static void encrypt() throws IOException {
        //convert each character in the line to its ASCII value
        for(int i = 0; i < line.length(); i++) {
            char lineChar = line.charAt(i);
            int lineInt = (int) lineChar;
            print();
        }
    }

    public static void print() throws IOException {
        System.out.println("Output file: EncryptedSecret.txt");

        //output file
        PrintWriter out = new PrintWriter(new FileWriter("EncryptedSecret.txt"));

        //while there is more data to write
        while(line != null) {
            out.println(lineInt + " ");
        }

        //close file
        out.close();
    }
}

Name: Anonymous 2011-04-03 20:44

Also, sorry for the format of the post, I've never posted in /prog/ before. I do read it occasionally though.

Name: Anonymous 2011-04-03 21:14

Your problem is you're using Java.

Name: Anonymous 2011-04-03 21:44

The file is created, but cannot be opened and has no text within
Actually, this will produce a file and write text to it, but it will only ever write the first.  Character.  Endlessly.

Run your program but "ctrl+c" out of it as fast as you can and open up the manageably smaller text file (it will still take time for Notepad to stop weeping).  Just lines and lines of 0, usually.
while(line != null) {
   out.println(lineInt + " ");
}

line will never be null at this point.

Name: Anonymous 2011-04-03 22:33

>>1
use [code] tags

Don't change these.
Name: Email:
Entire Thread Thread List