I am getting the following error when I run this program...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1.
I see where the error is occurring (String keyword = args[1]) and I have searched the web to try and solve this on my own, but I just can't see how to fix it. If someone can give me some advice on how to fix this, I would greatly appreciate it.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
public class Encryptor {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String keyword = args[1];
char [] cypher;
cypher = makeCypher(removeDups(keyword));
FileWriter f1 = null;
FileReader f = null;
if (args.length < 3 || args.length > 5) usage();
try
{
f = new FileReader(args[2]);
f1 = new FileWriter(args[3]);
for (int i = 0; i < args.length;i++)
{
if (args[i].startsWith("-"))
{
String option = args[i].substring(1);
if(option.equals("d"))
decrypt(f,f1,cypher);
else if (option.equals("k"))
{
encrypt(f,f1,cypher);
}
}
}
}
catch(Exception e)
{
System.out.println("Exception: " + e);
}
}
public static void usage()
{ System.out.println
("Usage: java Monoalphabet keyword [-d] [-kn] infile outfile");
System.exit(1);
}
public static void decrypt(FileReader reader, FileWriter writer,char[] cypher )
{ char [] C = new char[26];
for ( int i = 0; i< C.length; i++)
{ C[i] = (char)( i + 97);
}
int positionOfSpace = ' ';
String outgoing = "";
String temp = "";
BufferedReader infile = new BufferedReader(reader);
try {
while(( temp = infile.readLine()) != null)
{
for (int k = 0; k < temp.length(); k++)
{
char whatever = temp.charAt(k);
char ch = C[(int)whatever - positionOfSpace];
String chs = new Character (ch).toString();
outgoing = outgoing + chs;
}
}
writer.write(outgoing);
reader.close();
writer.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e);
}
}
public static void encrypt(FileReader reader,FileWriter writer, char[] cypher )
{ int positionOfA = 'a';
String outgoing = "";
String temp = "";
BufferedReader infile = new BufferedReader(reader);
try {
while(( temp = infile.readLine()) != null)
{
for (int k = 0; k < temp.length(); k++)
{
char whatever = temp.charAt(k);
char ch = cypher[(int)whatever - positionOfA];
String chs = new Character (ch).toString();
outgoing = outgoing + chs;
}
writer.write(outgoing);
}
reader.close();
writer.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e);
}
}
public static String removeDups(String keyword)
{ String keep = "";
keyword = keyword + "zyxwvutsrqponmlkjihgfedcba";
for(int i = keyword.length()-1; i > -1 ; i--)
if(i == keyword.indexOf(keyword.charAt(i)))
{
keep = keyword.charAt(i)+ keep;
}
return keep;
}
public static char[] makeCypher(String keep)
{
char Array [];
Array = keep.toCharArray();
return Array;
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at random_monoalphabet_19_1.Encryptor.main(Encryptor.java:24)
This post has been edited by firebug: 12 March 2010 - 07:05 PM

New Topic/Question
Reply




MultiQuote









|