java - Array being accessed out of bounds? -


so have problem still working on class. need encrypt message using basic character change. have coded having issues loop suppose handle character replacements.for reason keeps trying access array outside of bounds thought using array length limit. missing not sure where. should work. have 2 files. taking message, changing char array, comparing letters find out , replacing them needed. point out have missed?

also note: know not suppose use == if use .equals, .compareto, or "char cannot dereferenced" error. time doesn't if use ==. if let me why thing or @ least point me can understand why happening grateful.

                                    driver      /** program name: driver date:4/14/2016  program description: program going handle window user enters data.  going going call methods of actions class methods: driver(),destination(), message(), */ import javax.swing.*;    // swing classes import java.awt.event.*; // actionlistener interface  import java.util.scanner; //for keyboard   public class driver extends jframe {  //delcare   private string locationletters; //this going hold users letter selection   private string moo; //this going hold users message     private boolean error; //this going check location input errors.     private string loch; //for holder of location selection   private string messh; //to hold message before change array     scanner keyboard = new scanner (system.in);//to make keyboard    actions loc = new actions();   actions mess = new actions();   /**   method constuctor going make window program use.    */  public driver()  {   system.out.println("sup nerd");     destination();   message();    system.out.println(moo + ": top level return");  }//end of driver()   public string destination()  {   //this make loop getting input , checking errors. input , passes input   //to actions file error checking   do{     system.out.println("please enter 2 charater key location want message");    locationletters = keyboard.next();     error = loc.error(locationletters);    if(error == true)     system.out.println("you have entered , incorrect location. please enter vaild location");    else     break;   }while(error == true);      loch = loc.getcountry(locationletters);   return loch;   }//end of destination()   public string message()  {   system.out.println("please enter message have encrypted.");   moo = keyboard.nextline();    moo = moo.touppercase();     messh = mess.encrypt(moo);    system.out.println(moo + ": original input");    system.out.println(messh + ": making sure encrypt works");  return messh;   }//end of message()   public static void main(string[] arg)  {    new driver();    }//end of main  }//end of driver class                                         actions import static java.lang.character.*;  /** program name: action date:4/14/2016  program description: program going handle encryption actions loction message being sent.  methods:location(),  */   public class actions {  //decare    public boolean error(string locha)   {   boolean error = true; //set boolean value   if(locha.equalsignorecase("fr"))    error = false;   if(locha.equalsignorecase("gb"))    error = false;    if(locha.equalsignorecase("ca"))    error = false;   if(locha.equalsignorecase("ja"))    error = false;   if(locha.equalsignorecase("ru"))    error = false;   if(locha.equalsignorecase("ge"))    error = false;   if(locha.equalsignorecase("au"))    error = false;        if(locha.equalsignorecase("mx"))    error = false;    return error;  }//end of error()   public string getcountry(string locha)  {    if(locha.equalsignorecase("fr"))    locha = "france";   if(locha.equalsignorecase("gb"))    locha = "great britain";   if(locha.equalsignorecase("ca"))    locha = "canada";   if(locha.equalsignorecase("ja"))    locha = "japan";   if(locha.equalsignorecase("ru"))    locha = "russia";   if(locha.equalsignorecase("ge"))    locha = "germany";   if(locha.equalsignorecase("au"))    locha = "australia";   if(locha.equalsignorecase("mx"))    locha = "mexico";   return locha;      }//end of getcountry   public string encrypt(string input)  {   int q; //this going length of string array   string encrypted = "meh"; //a holder encrypted message   int limit; //the limit of array      char[] chararray = input.tochararray();    limit = chararray.length;    for(int x = 0;x <= limit; x++)   {    if(chararray[x] == ('a'))     chararray[x] = 'n';    else if(chararray[x] == ('b'))     chararray[x] = 'o';    else if(chararray[x] == ('c'))     chararray[x] = 'p';    else if(chararray[x] == ('d'))     chararray[x] = 'q';    else if(chararray[x] == ('e'))     chararray[x] = 'r';    else if(chararray[x] == ('f'))      chararray[x] = 's';    else if(chararray[x] == ('g'))     chararray[x] = 't';    else if(chararray[x] == ('h'))     chararray[x] = 'u';    else if(chararray[x] == ('i'))     chararray[x] = 'v';    else if(chararray[x] == ('j'))     chararray[x] = 'w';    else if(chararray[x] == ('k'))     chararray[x] = 'x';    else if(chararray[x] == ('l'))     chararray[x] = 'y';    else if(chararray[x] == ('m'))     chararray[x] = 'z';    else if(chararray[x] == ('n'))     chararray[x] = 'a';    else if(chararray[x] == ('o'))     chararray[x] = 'b';    else if(chararray[x] == ('p'))     chararray[x] = 'c';    else if(chararray[x] == ('q'))     chararray[x] = 'd';    else if(chararray[x] == ('r'))     chararray[x] = 'e';    else if(chararray[x] == ('s'))     chararray[x] = 'f';    else if(chararray[x] == ('t'))     chararray[x] = 'g';     else if(chararray[x] == ('u'))     chararray[x] = 'h';    else if(chararray[x] == ('v'))     chararray[x] = 'i';    else if(chararray[x] == ('w'))     chararray[x] = 'j';    else if(chararray[x] == ('x'))     chararray[x] = 'k';    else if(chararray[x] == ('y'))     chararray[x] = 'l';    else if(chararray[x] == ('z'))     chararray[x] = 'm';     else     {     x++;     continue;     }                        }     encrypted = chararray.tostring();     return encrypted;  }//end of encrypt  }//end of action class 

the error loop @

for(int x = 0;x <= limit; x++) 

the error getting

exception in thread "main" java.lang.arrayindexoutofboundsexception: 0     @ actions.encrypt(actions.java:75)     @ driver.message(driver.java:74)     @ driver.<init>(driver.java:39)     @ driver.main(driver.java:85) 

i not understand why trying access array outside bounds. thank input , help. trying figure out time. looks should work.

to recap, solution original problem loop should run (x < limit) not (x <= limit).

and answer other question, error "char cannot dereferenced" occurs because ".equals" , ".compareto" strings , @ time working char.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -