Runtime Error!!?


  • 1
    W

    Runtime Error Message: Line 8: java.lang.ArrayIndexOutOfBoundsException: 0
    Last executed input: "", 1

    What does this mean? I even run my code in Eclipse successfully.
    Is there anybody who could help me? The below is my code:

    public class Solution {

    public String convert(String s, int nRows) {
        if(s==null&&s.length()==0) return null;
        char[] newS = s.toCharArray();
    	String result ="";
    	for(int row = 0; row < nRows; row++){
    		int tag = 0;
    		int i = row;
    		result = result + newS[i];
    		if(row==0){
    			while(i+2*(nRows-row)-2<newS.length){
    				result = result + "" + newS[i+2*(nRows-row)-2]; 
    				i+=2*(nRows-row)-2; 
    			}
    		}else if(row==nRows-1){
    			while(i+2*nRows-2<newS.length){
    				result = result + "" + newS[i+2*(nRows-1)];
    				i+=2*nRows-2;
    			}
    		}else{
    			while((i+2*(nRows-row)-2<newS.length&&tag==0)||(i+row<newS.length&&tag==1)){
    				if(tag==0){
    					result = result + "" + newS[i+2*(nRows-row)-2]; 
    					i+=2*(nRows-row)-2;  
    					tag = 1;
    				}else{
    					result = result + "" + newS[i+2*row];
    					i+=2*row;
    					tag = 0;
    				}
    			}
    		}
    	}
    	return result;
    }
    

    }


  • 1
    S

    For this kind of error, it would be better to provide your code.

    Pay attention when you update your question with your code, try to add some content to explain your code.

    However, for this specific test case

    input: "", 1

    It looks like your code do not cover the edge case, which means how code runs if input is an empty string, check some index of string or array carefully. Sometimes, IDE will get rid of this things. But LeetCode is more restrictive to that kind of things.

    Anyway, without code, it is only a guess, I completely unsure of it.


  • 0
    W

    Hi, Shangrila,
    Thanks for your advice. I did forget to cover the edge case so I added the code like "if(input==null&&input.length()==0)". But I still had the same problem. The below is my code. Could u plz give me a hand? So confused..

    public class Solution {

    public String convert(String s, int nRows) {
    
        if(s==null&&s.length()==0) return null;
    
        char[] newS = s.toCharArray();
    
    	String result ="";
    	for(int row = 0; row < nRows; row++){
    		int tag = 0;
    		int i = row;
    		result = result + newS[i];
    		if(row==0){
    			while(i+2*(nRows-row)-2<newS.length){
    				result = result + "" + newS[i+2*(nRows-row)-2]; 
    				i+=2*(nRows-row)-2; 
    			}
    		}else if(row==nRows-1){
    			while(i+2*nRows-2<newS.length){
    				result = result + "" + newS[i+2*(nRows-1)];
    				i+=2*nRows-2;
    			}
    		}else{
    			while((i+2*(nRows-row)-2<newS.length&&tag==0)||(i+row<newS.length&&tag==1)){
    				if(tag==0){
    					result = result + "" + newS[i+2*(nRows-row)-2]; 
    					i+=2*(nRows-row)-2;  
    					tag = 1;
    				}else{
    					result = result + "" + newS[i+2*row];
    					i+=2*row;
    					tag = 0;
    				}
    			}
    		}
    	}
    	return result;
    }
    

    }

    Thanks:)


  • 0
    S

    Hi @WangWeijue, thanks for your updating. But could you please update your original question, instead of leave it in the comment? After that I will try to check it out.


  • 0
    W

    Hi, @Shangrila,
    I did update my question like what you said. Thanks a lot for your help.
    Jingdai


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.