[英]Array being accessed out of bounds?
所以我有一个问题,我还在为课堂而努力。 我需要使用基本字符更改来加密消息。 我已经对所有内容进行了编码,但是我遇到了一个假设处理字符替换的 for 循环的问题。出于某种原因,它一直试图在边界之外访问我的数组,甚至认为我使用数组长度作为限制。 我错过了一些东西,但我不确定在哪里。 它应该工作。 我有两个文件。 我基本上是在接收一条消息,将其更改为字符数组,比较字母以找出它们是什么,然后根据需要替换它们。 你能指出我遗漏了什么吗?
另请注意:我知道我不应该使用 == 但如果我使用 .equals、.compareTo 或类似的东西,我会收到“char cannot be dereferenced”错误。 他们只有在我使用 == 时才不这样做。 如果您也可以告诉我为什么会这样,或者至少指出我可以理解为什么会发生这种情况,我将不胜感激。
driver
/**
Program Name: Driver
Date:4/14/2016
Program Description: This program is going to handle the window where the user enters data.
It is also going to be what is going to call the methods of the Actions class
Methods: Driver(),destination(), message(),
*/
import javax.swing.*; // For the Swing classes
import java.awt.event.*; // For the ActionListener Interface
import java.util.Scanner; //for the keyboard
public class Driver extends JFrame
{
//delcare
private String locationLetters; //this is going hold the users letter selection
private String moo; //this is going to hold the users message
private boolean error; //this is going to check the location input for errors.
private String locH; //for the holder of location selection
private String messH; //to hold the message before change it to an array
Scanner keyboard = new Scanner (System.in);//to make the keyboard
Actions loc = new Actions();
Actions mess = new Actions();
/**
This method is the constuctor that is going to make the window for the program to use.
*/
public Driver()
{
System.out.println("sup nerd");
destination();
message();
System.out.println(moo + ": The Top level return");
}//end of Driver()
public String destination()
{
//this to make a loop for getting input and checking it for errors. It get input and passes the input
//to the actions file for error checking
do{
System.out.println("Please enter the two charater key for the location you want to message");
locationLetters = keyboard.next();
error = loc.error(locationLetters);
if(error == true)
System.out.println("You have entered and incorrect location. Please enter a vaild location");
else
break;
}while(error == true);
locH = loc.getCountry(locationLetters);
return locH;
}//end of destination()
public String message()
{
System.out.println("Please enter the message you would like to have encrypted.");
moo = keyboard.nextLine();
moo = moo.toUpperCase();
messH = mess.encrypt(moo);
System.out.println(moo + ": The 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: This program is going to handle all the encryption actions as well
loction where the message is being sent.
Methods:Location(),
*/
public class Actions
{
//decare
public boolean error(String locHA)
{
boolean error = true; //set the 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 is going to be the length of the string array
String encrypted = "meh"; //a holder for the encrypted message
int limit; //the limit of the 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
错误与我的 for 循环有关
for(int x = 0;x <= limit; x++)
我得到的错误是
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Actions.encrypt(Actions.java:75)
at Driver.message(Driver.java:74)
at Driver.<init>(Driver.java:39)
at Driver.main(Driver.java:85)
我不明白为什么它试图访问边界外的数组。 感谢您的投入和帮助。 我想解决这个问题有一段时间了。 看起来它应该工作。
回顾一下,原始问题的解决方案是 for 循环应该运行到 (x < limit) 而不是 (x <= limit)。
并回答您的另一个问题,出现错误“char cannot be dereferenced”,因为“.equals”和“.compareTo”用于字符串,而那时您正在使用字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.