12 Replies - 2129 Views - Last Post: 19 March 2010 - 05:45 PM Rate Topic: -----

#1 firebug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-February 10

Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 12 March 2010 - 07:04 PM

Hi All,

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


Is This A Good Question/Topic? 0
  • +

Replies To: Please Help Fix Exception in thread "main" java.lang.ArrayInde

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 12 March 2010 - 07:05 PM

What command line arguments are you passing your program?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10650
  • View blog
  • Posts: 39,559
  • Joined: 27-December 08

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 12 March 2010 - 07:52 PM

Remember that since arrays are indexed from 0 through length-1, if you pass in one argument, then the element is args[0]. However, without seeing your command line arguments, I can't really help you much more.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 12 March 2010 - 10:31 PM

public static void main(String[] args) {

String keyword = args[1];

and what is in args[0] ?

This post has been edited by pbl: 12 March 2010 - 10:32 PM
Reason for edit:: Sorry Macosxnerd

Was This Post Helpful? 1
  • +
  • -

#5 firebug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-February 10

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 13 March 2010 - 08:41 AM

My command line is
java Encryptor -d -k FEATHER encrypt.txt output.txt
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 13 March 2010 - 10:31 AM

View Postfirebug, on 13 March 2010 - 09:41 AM, said:

My command line is
java Encryptor -d -k FEATHER encrypt.txt output.txt

so
args[0] == "-d"
args[1] == "-k"
args[2] == "encrypt.txt"
args[3] == "output.txt"

so your problem is not with the args[]

at random_monoalphabet_19_1.Encryptor.main(Encryptor.java:24)

Which line is line 24 ?
Was This Post Helpful? 1
  • +
  • -

#7 firebug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-February 10

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 14 March 2010 - 06:07 AM

Line 24 is ...
String keyword = args[1];


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 14 March 2010 - 06:09 PM

View Postfirebug, on 14 March 2010 - 07:07 AM, said:

Line 24 is ...
String keyword = args[1];


So I doubt that you actually do: java Encryptor -d -k FEATHER encrypt.txt output.txt
Was This Post Helpful? 0
  • +
  • -

#9 firebug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-February 10

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 14 March 2010 - 07:07 PM

Sorry. Command line is java Encryptor -k FEATHER encrypt.txt output.txt (no -d).
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 14 March 2010 - 07:52 PM

View Postfirebug, on 14 March 2010 - 08:07 PM, said:

Sorry. Command line is java Encryptor -k FEATHER encrypt.txt output.txt (no -d).

java Encryptor -d -k FEATHER Encryptor.java output.txt

gives me Java.io exception file FEATHER not found so we are far away from your

String keyword = args[1];
Was This Post Helpful? 0
  • +
  • -

#11 firebug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-February 10

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 19 March 2010 - 02:27 PM

I reworked the program a little and got rid of the error message. The problem I am having now is it is not displaying anything. I think there is a problem with output.txt and I am not sure how to fix it. Here is what I have...

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;


public class MonoalphabetCipher {

      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;
  }

}



import java.io.FileReader;
import java.io.FileWriter;

public class Encryptor {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {



  if (args.length !=4) {
      System.out.println
       ("Usage: java Encryptor keyword -d -k FEATHER encrypt.txt output.txt");
      return;
  }
     String keyword = args[1];
     char [] cypher;
     cypher = MonoalphabetCipher.makeCypher(MonoalphabetCipher.
             removeDups(keyword));


  try
  {

     FileReader in = new FileReader(args[2]);
     FileWriter out = new FileWriter(args[3]);


   for (int i = 0; i < args.length;i++)
   {

    if (args[0].startsWith("-"))
     {
        String option = args[0].substring(1);
          if(option.equals("d")){
          MonoalphabetCipher.decrypt(in,out,cypher);
          }
        else if (option.equals("k"))
        {
          MonoalphabetCipher.encrypt(in,out,cypher);

        }

      }

     }
   }
    catch(Exception e)
    {
      System.out.println("Exception: " + e);
    }

  }


}



Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 19 March 2010 - 04:24 PM

   for (int i = 0; i < args.length;i++) 
   { 
 
    if (args[0].startsWith("-")) 
     { 


Why making a loop to test 4 times args[0] ?
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Please Help Fix Exception in thread "main" java.lang.ArrayInde

Posted 19 March 2010 - 05:45 PM

In order to handle command line arguments, you may want to make methods with variable amounts of arguments to easily handle this.

http://www.deitel.co...umentLists.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1