1 / 1
Apr 2012

Hi All,

I'm trying to solve the PALIN problem in java and i get the following exception on ideone,

Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Integer.java:499)
at Main.main(Main.java:14)

My code is as below,

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
public class Main {
	public static void main(String[] args) throws IOException {
		PrintWriter out = new PrintWriter(System.out);
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());
		while(n>0){
			String	ip = in.readLine();
			char [] number = ip.toCharArray();
			int length = number.length;
			for(int i=0;i<length;i++){
				if(i<=length-i-1){
					number[length-i-1]= number[i];
				}
			}
			if(compareArray(number,ip.toCharArray()) ==1){
				out.println(number);
			}
			else if(number.length%2 ==0){
				out.println(work(number,number.length/2-1,number.length/2));
			}
			else{
				out.println(work(number,number.length/2,number.length/2));
			}
			out.flush();
			n--;
		}
	}
	public static int compareArray(char [] converted,char [] original){
		if(converted.length < original.length){
			return -1;
		}
		else if(converted.length > original.length){
			return 1;
		}
		else{
			for(int i=0;i<converted.length;i++){
				if(converted[i]<original[i])
					return -1;
				if(converted[i]>original[i])
					return 1;
			}
		}
		return 0;
	}
	public static char [] work(char [] ip,int pos1,int pos2){
		if(pos1<0){
			ip[ip.length-1]= '1';
			String temp = ""+'1';
			for(int i=0;i<ip.length;i++){
				temp = temp+ip[i];
			}
			return temp.toCharArray();
		}
		else if(ip[pos1] < '9'){
			ip[pos1]=ip[pos2]= (char) (ip[pos1]+1);
			return ip;
		}
		else{
			ip[pos1]=ip[pos2]= '0';
			ip=work(ip,pos1-1,pos2+1);
			return ip;
		}
	}
}

I get the error when i parse the number of inputs for the problem.
Can the input include empty lines or a null. How do i fix this error?

Thanks

Suggested Topics

Want to read more? Browse other topics in JAVA based languages or view latest topics.