Hello all...
I am new to this web site and new to Java and need some help to finish up a homework assignment. The assignment was to create a program that takes an input file, reads the file, codes each line useing the Rot13 cypher, then print the results in an output file. I have completed the program myself except for one minor, and rather annoying issue. When the output file is generated it prints the results all on one line. I know I need something like outFile.println somewhere in the loop but not sure on the exact syntax or the location of the code. Here is the actual codd I have written. Thanks to all....

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

public class Rot13
{


public static void main (String[] args)
{

Scanner keybd = new Scanner(System.in),
inFile;

String fileNameOut,
fileNameIn,
text,
line = "";
PrintWriter outFile;
int k = 0;
int i = 0;

System.out.print("Enter Input File Nmme: ");
fileNameIn = keybd.nextLine();
char c;


try
{
inFile = new Scanner (new File(fileNameIn));
}

catch (FileNotFoundException e)
{
System.out.print("Cannot Open Input File :" + fileNameIn);
return;
}

System.out.print("Enter Output File Name: ");
fileNameOut = keybd.nextLine();

try
{
outFile = new PrintWriter (fileNameOut);
}

catch (FileNotFoundException e)
{
System.out.println ("Error: Cannot open file " + fileNameOut);
return;
}

while (inFile.hasNextLine())
{
line = inFile.nextLine();
for(k = 0; k < line.length(); k++)
{
c = line.charAt(k);

if (Character.isUpperCase(c))
{
c = (char)('A' + (c - 'A' + 13) % 26);
}

else if (Character.isLowerCase(c))
{
c = (char) ('a' + (c - 'a' + 13) % 26);
}

if (i == line.length())
{
outFile.println();
}

outFile.print(c);
i++;
}
}

inFile.close();
outFile.close();


} // main

}


Reply With Quote