1
public class Solution {

public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int tc = Integer.parseInt(br.readLine());//I get Numberformat Exception here
    for(int i=0;i<tc;i++)                    // Even if my inputs are on separate lines
        {
    String original = br.readLine();
    palindrome(original);
        }
       }

public static void palindrome(String original)
{
     String reverse="";
    int length = original.length();
        for ( int i = length - 1 ; i >= 0 ; i-- )
        reverse = reverse + original.charAt(i);
    if (original.equals(reverse))
    {
     System.out.println(0);
    }
    else
     {
          char[] org = original.toCharArray();
          int len = org.length;
          int mid = len / 2;

          if(len % 2 == 0)
          {
            char[] front = new char[mid];
            char[] back = new char[mid];
            for(int i=0;i<mid;i++)
            {
                front[i] = org[i];
            }
            int j=0;
            for(int i=len-1;i>=mid;i--)
            {
                back[j] = org[i];
                j++;
                while(j > mid)
                {
                    break;
                }
            }
            change(front,back,mid);
          }
          else
          {
            char[] front = new char[mid];
            char[] back = new char[mid];
            for(int i=0;i<mid;i++)
            {
                front[i] = org[i];
            }
            int j=0;
            for(int i=len-1;i>mid;i--)
            {
                back[j] = org[i];
                j++;
                while(j > mid)
                {
                    break;
                }
            }
            change(front,back,mid);
          }
     }
}
public static void change(char[] front,char[] back,int len)
   {
       int count =0;
       for(int i =0;i<len;i++)
       {
           if(front[i] != back[i] )
            {
                count += (back[i] - front[i]);
            }
        }
     System.out.println(count)
   }
  }
  1. What i try to do here is get an input from the number of test cases say 3 in my first line followed by the test-cases themselves.
  2. sample input :

3

abc abcba abcd

  1. Now it has to check if the string is a palindrome if its so it ll print 0
  2. else it breaks the string into two halves front and back and finds the minimum number of changes to make it a palidrome.
  3. here i have also checked if its a odd or even length string if odd i have omitted the middle char.
  4. By changes we can only change 'd' to 'b' not 'b' to 'd'
  5. Once a letter has been changed to 'a', it can no longer be changed.

My code works fine for the above input but it doesnt for some other inputs i dont quiet understand why..

for instance if i give a custom test case as

5
assfsdgrgregedhthtjh
efasfhnethiaoesdfgv
ehadfghsdfhmkfpg
wsertete
agdsjgtukgtulhgfd

I get a Number Format Exception.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
user2860954
  • 177
  • 2
  • 24

3 Answers3

1

Your code works fine here, whithout NumberFormatException: http://ideone.com/QJqjmG

Nicolas Albert
  • 2,586
  • 1
  • 16
  • 16
0

This may not solve your problem, but improves your code...

As first user input you are expecting an integer. You are parsing the String returned by br.readLine() and do not take care of the NumberFormatException parseInt(...) may throw. Just imagine someone hits space or return key as first input.

So I propose to put a try-catch-block around the parseInt(...). Here is an example how this may look like.

Community
  • 1
  • 1
Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
0

Guys thank you for all your suggestion i just found out why my other test cases weren't working

public static void change(char[] front,char[] back,int len)
{
   int count =0;
   for(int i =0;i<len;i++)
   {
       if(front[i] != back[i] )
        {
            count += (back[i] - front[i]);
        }
    }
 System.out.println(count)
}

This part of my code has to be changed to

public static void change(char[] front,char[] back,int len)
   {
       int count =0;
       for(int i =0;i<len;i++)
       {
           if(front[i] != back[i] )
            {
                char great = findGreatest(front[i],back[i]);
                if(great == back[i])
                    {
                count += (back[i] - front[i]);
                }
               else 
                   {
                   count += (front[i] - back[i]);
               }
            }
        }
        System.out.println(count);
   }
   public static char findGreatest(char first,char second)
    {
      int great = first;
      if(first < second)
          {
          great = second;
      }
    return (char)great;
    }
  1. Because i get negative values coz of subtracting ascii's which are greater than them and as i have already mentioned i can only do 'd' to 'a' not the other way round.

Thank you for your time guys!

user2860954
  • 177
  • 2
  • 24