|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
a console ROT13 program gone bad!!! please help
For starters, here's the entire source code:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.lang.String; public class ROT13 { /** Creates a new instance of ROT13 */ public ROT13() { } /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { int x; char[] Alpha = {'a','b','c','d','e','f','g','h','i','j','k','l',' m','n','o','p','q','r','s','t','u','v','w','x','y' ,'z',' ','A','B','C','D','E','F','G','H','I','J','K','L', 'M','N','O','P','Q','R','S','T','U','V','W','X','Y ','Z'}; char[] Rev = {'z','y','x','w','v','u','t','s','r','q','p','o',' n','m','l','k','j','i','h','g','f','e','d','c','b' ,'a',' ','Z','Y','X','W','V','U','T','S','R','Q','P','O', 'N','M','L','K','J','I','H','G','F','E','D','C','B ','A'}; char[] temp; BufferedReader KeyboardInput; KeyboardInput = new BufferedReader(new InputStreamReader(System.in)); String NewLine= new String(); while(((NewLine = KeyboardInput.readLine())!=null)){ x=NewLine.length(); temp=new char[x]; NewLine.getChars(0,x,temp,0); for(int i=0; i <=53;i++){ for(int check=0; check <=53;check++){ if(temp[i]==Alpha[check]){ temp[i]= Rev[check]; } } } System.out.println(temp); } } } __________________________________________________ __________________________________________________ ____________ Yeah, I realize it's a really small project but it's still not working. The problem is that I get a java.lang.ArrayIndexOutOfBoundsException on line 51. I think it's because I can't compare strings with operators but I'm not familiar enough with the language to know what class or method to call as and argument for the the if() expression. I'm assuming there is something in either java.lang.String or java.util.Arrays but I haven't found anything yet. If someone with more experience in this language could speed this along I would be very grateful. Jimmy Scythe |
|
#2
|
|||
|
|||
|
"if(temp[i]==Alpha[check]){"
should be "if(temp[i].equals(Alpha[check])){" .equals() compares two strings. .equalsIgnoreCase() compares two strings without considering the case. I might be wrong with the capitalization of these, but you can always do a quick google search to find what they are supposed to be. |
|
#3
|
|||
|
|||
|
thanx for the answer but....
I tried that not long after I posted. the problem now is that temp[i] can't be dereferenced. temp[] and Alpha[] are character arrays BTW. I'm thinking that I'm going to have to convert both values to ints and store them in temporary variables. I don't like that solution since it's ugly as all hell. Of course if I was that worried about program size I would have just used half the alphabet in Alpha[] and Rev[]. I'm also thinking that since I'm going to have to convert anyway, I might as well just pitch Alpha[] and Rev[] and test to see if the value is < or > the integer value of 'm'. There has got to be an easier way to convert types and/or sort and re-arrange character arrays and strings. Thanx for the answer though, it was solid advice.
Jimmy Scythe |
|
#4
|
|||
|
|||
|
I also just placed the temp[i] in a plain char variable and did likewise to Alpha[check]. Apparently, you can't dereference chars at all.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > a console ROT13 program gone bad!!! please help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|