0

So my code is: But if I run this.I got java.lang.ArrayIndexOutOfBoundsException Even if i try to encode some Chinese characters or Janpanese characters like "测试一下" Don't ask me to add a try catch or OutOfBounds check

(Because it will change the result to which is unable to verify)

(If encode "TestThissjkdajld9999kdjalyyy".It works without exception.)

Thanks.

    public class Main {  
public static final String l = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

public String encode (String fucyo) {
    char[] av = l.toCharArray();
    char[] testssdvx = fucyo.toCharArray();
    int length = testssdvx.length;
    int now = 1;
    String result = "";
    while(now < length-1) {
        synchronized(this) {
        System.out.println(testssdvx[now]);
        System.out.println(testssdvx[now+1]);
        result += av[(testssdvx[now]>>2)];
        result += av[((testssdvx[now]&3)<<4) + (testssdvx[now+1]>>4 )];
        result += av[((testssdvx[now+1]&15)<<2)+ (testssdvx[now+2]>>6)];
        result += av[(testssdvx[now+2] & 63)];
        now +=3;
        }
    }
    System.out.println("Pre:"+result);
    char[] charArray = result.toCharArray(); 
    result = "";
    for (int i=charArray.length-1; i>=0; i--){ 
       result += charArray[i]; 
    } 

    return result;
}


public static void main(String[] args) {    
    System.out.println(new Main().encode("TestThissjkdajldjalksdjalkdjalksdjskladjciiiiiiii78787875ytyyyyyyyyyyyyy"));
    }
}
ho 3
  • 3
  • 2
  • `now+2` - how big do you think this value may become? – Scary Wombat Jun 18 '18 at 08:07
  • you don't want to change the results? then you're stuck with those exceptions. an ArrayIndexOutOfBoundsException means you are trying to reach an element in an Array with an index higher than the highest valid index, and lower than the minimal valid index. – Stultuske Jun 18 '18 at 08:08
  • *Don't ask me to add a try catch or OutOfBounds check* - OK – Scary Wombat Jun 18 '18 at 08:09
  • Unrelated: please improve your naming skills. Variable names should mean something. They communicate intent to the human reader. Your variable names only communicate confusion to me. This alone makes your code 10 times harder to read than it ought to be! – GhostCat Jun 18 '18 at 08:09
  • And for the record: you dont need a try catch here. Because that exception is nothing but a symptom of **bug(s)** in your code. You fix the bugs, and then you wont need a try/catch, promised. – GhostCat Jun 18 '18 at 08:10
  • `result += av[(testssdvx[now+2] & 63)];` if now has `length-2` value, you are still in while condition but out of range (length) – Cid Jun 18 '18 at 08:10
  • But my friend recode this in Another Programming language.It works without Exception. – ho 3 Jun 18 '18 at 08:16

0 Answers0