所以我有一个问题,我还在上班。我需要使用基本字符更改来加密消息。我编写了所有内容,但我遇到了一个for循环的问题,假设处理字符替换。由于某种原因,它一直试图访问我的数组超出范围,甚至认为我使用数组长度作为限制。我错过了什么,但我不知道在哪里。它应该工作。我有两个文件。我基本上是在接收消息,将其更改为char数组,比较字母以找出它们是什么,然后根据需要替换它们。你能指出我错过了什么吗?
另请注意:我知道我不会使用==但如果我使用.equals,。compareTo或类似的东西,我会得到一个" char无法取消引用"错误。如果我使用==,他们只有时间不这样做。如果你也可以告诉我为什么这是一件事,或者至少指出我能理解为什么会这样,我将不胜感激。
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)
我不明白为什么它试图访问边界外的数组。感谢您的投入和帮助。我试图解决这个问题已有一段时间了。它看起来应该有效。
答案 0 :(得分:0)
总结一下,原始问题的解决方案是for循环应该运行到(x <极限)而不是(x <=极限)。
要回答你的另一个问题,错误&#34; char不能被解除引用&#34;因为&#34; .equals&#34;和&#34; .compareTo&#34;是字符串,当时你正在使用字符。