File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java - Works, but doesnt work... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java - Works, but doesnt work..." Watch "Java - Works, but doesnt work..." New topic
Author

Java - Works, but doesnt work...

James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
I am making a program where you will be asked if you would wish to scramble a program, if you click yes, it brings you to another question on which file you wish to upload, it uploads the file, but it wont scramble it, and I have all the coding in there from two programs I made, one to ask the questions, and one to scramble the document, and when I tried putting them in there together it wouldnt work, I was wondering what the problem might be on this, and the only way to show you is with the code, so here is a bit of my code.

___________________________________________________________________________

import java.io.*;

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

FileInputStream File1; //create a file to input
DataInputStream In; //create an Input stream
String temp = ""; //create a string to temerarily store data
FileOutputStream Fileout; // create a file to output
PrintStream p; //create a print stream
//Time to define the variables
int lengthOf;
int temp2 = 0;
char[] temp3;
String Encode = "";
String Decode = "";
String Input = "";
String Inputb = "";
String temp4 = "";
String temp5 = "";
String yes = "";
String a = "a";
String b = "b";
String filename = "";
String fileInput = " ";

DataInput keyboard = new DataInputStream (System.in);
//Figuring out if you would like to encode something, or decode something.
{
System.out.println ("Would you like to scramble your document? (y for yes, n for no):");
temp4 = keyboard.readLine ();
Input = temp4;
}
if (Input.startsWith ("Y") || Input.startsWith ("y"))
{
//Storing the user input to "filename" so the program can retrieve the file you wish to see
System.out.println ("Which file would one wish to scramble? (*bobdole.txt is for you sefeldas*) ");
filename = keyboard.readLine ();

File1 = new FileInputStream (filename);
In = new DataInputStream (File1);
{
/*This is the functionality of the program, where it takes every letter, lower case and upper case, is being converted
fromitsnatrualstatetoastateinwhichisrandom.Thisway, once it is scrambled, noone who catches onto it can read it
unlesstheyhavethedecoder*/
Fileout = new FileOutputStream ("scrambled.txt"); //Where the data to which is scrambled is stored
p = new PrintStream (Fileout);
while (In.available () > 0)
{

temp = In.readLine ();
lengthOf = temp.length ();
temp3 = temp.toCharArray ();

for (temp2 = 0 ; temp2 < lengthOf ; temp2++)
{
if (temp3 [temp2] == ('a')) //If the letter is <-- it will be changed to...
{
p.println ("J"); //...this in the output scrambled.txt
}
/*From here on out, the past two comments follow the exact same pattern with different letters, and or characters*/

else if (temp3 [temp2] == ('b'))
{
p.println ("a");
}
else if (temp3 [temp2] == ('c'))
{
p.println ("m");
}
...
...
...
else if (temp3 [temp2] == ('7'))
{
p.println ("0");
}
else if (temp3 [temp2] == ('8'))
{
p.println ("P");
}
else if (temp3 [temp2] == ('9'))
{
p.println ("r");
}
else if (temp3 [temp2] == ('0'))
{
p.println ("R");
}
else if (temp3 [temp2] == (' '))
{
p.println ("!");
}
else if (temp3 [temp2] == ('!'))
{
p.println (".");
}
else if (temp3 [temp2] == ('.'))
{
p.println ("_");
}
else if (temp3 [temp2] == (','))
{
p.println ("*");
}
fileInput = In.readLine ();

System.out.println (fileInput);
}

}
}
}


{


if (Input.startsWith ("N") || Input.startsWith ("n") || Input.startsWith ("Y") || Input.startsWith ("y"))
{
System.out.println ("Would you like to unscramble your document? (y for yes, n for no):");
temp5 = keyboard.readLine ();
Inputb = temp5;
}
if (Inputb.startsWith ("N") || Inputb.startsWith ("n"))
{
System.out.println ("You typed: " + Inputb);
}
else
//Storing the user input to "filename" so the program can retrieve the file you wish to see
System.out.println ("Which file would one wish to unscramble? (*bobdole.txt is for you sefeldas*) ");
filename = keyboard.readLine ();

File1 = new FileInputStream (filename);
In = new DataInputStream (File1);


while (In.available () > 0)
{
fileInput = In.readLine ();
System.out.println (fileInput);
}

System.out.println ("You typed: " + Input);
}
}
}

Inbetween where the ... lines are is the same coding for all letters and numbers, I would appreciate the help


When in doubt, get the hammer!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Does it write anything to the output file ? I notice you don't close the output stream at any point - if the output file is empty or incomplete this could be the reason.
[ June 05, 2008: Message edited by: Joanne Neal ]

Joanne
James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
this is how the program runs

Would you like to scramble your document? (y for yes, n for no):
y
Which file would one wish to scramble? (bobdole.txt is for you sefeldas)
bobdole.txt
Micheal Moore
Micheal Jackson
Mr Sefeldas
Bill
Bob Dole
Mousseau
Mary Jane
Mohomad Damohom
Danny Dean Douglas Doucette
Joe
Jim
Jan
null
null
null
null
null
null
null
null
null
Would you like to unscramble your document? (y for yes, n for no):
n
You typed: n

thats how it is outputted there, but as into scrambled.txt the output is scrambled but its only 10-20 letters going down in a row, not the whole document.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
So try closing the output stream.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java - Works, but doesnt work...