0

The following code works fine when I'm hard coding the input values in the code. But when I get input through keyboard, the value of l[i] and r[i] get assigned to "the desired value"+48. I don't know why this is happening. Please help me in finding a solution.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class RichieRich {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int k = in.nextInt();
    int i,j;
    int number = in.nextInt();
    String numb = Integer.toString(number);
    char[] num = numb.toCharArray();


    int siz = n/2;
    int[] l = new int[siz];
    int[] r = new int[siz];

    for(i=0; i<siz; i++)
    {
        l[i] = num[i];    /*PROBLEM IS HERE*/
    }
    for(i=0, j=n-1; i<siz; i++, j--)
    {
        r[i] = num[j];    /*AND HERE*/
    }
    int count=0;
    for(i=0; i<siz; i++)
    {
        if(l[i] != r[i])
        {
            count++;
        }
    }
    int cose;
    if(k+1 == count)
    {

        cose = 0;
    }
    else if(k>=count)
    {
        cose = 1;
    }
    else
    {
        cose = -1;
    }
    switch(cose)
    {
        case 0:
        {
            int max = 0;
            for(i=0;i<siz;i++)
            {
                if(l[i] > max)
                {
                    max = l[i];
                }
                if(r[i] > max)
                {
                    max = r[i];
                }
            }
            for(i=0; i<siz; i++)
            {
                if(l[i] != r[i])
                {
                    l[i] = max;
                    r[i] = max;
                }
            }
            for(i=0 ; i<siz; i++)
            {
                System.out.println(l[i]);
            }
            for(i=0 ; i<siz; i++)
            {
                System.out.println(r[i]);
            }
        }
        case 1:
        {
            int max = 0;
            for(i=0 ; i<n; i++)
            {
                if(num[i] > max)
                {
                    max = num[i];
                }
                if(l[i] != r[i])
                {
                    l[i] = max;
                    r[i] = max;
                }
            }
            for(i=0 ; i<siz; i++)
            {
                System.out.println(l[i]);
            }
            for(i=0 ; i<siz; i++)
            {
                System.out.println(r[i]);
            }
        }
        case -1:
        {
            System.out.println("-1");
        }
    }
}
}
TDG
  • 5,909
  • 3
  • 30
  • 51
Anand Kumar
  • 169
  • 1
  • 11
  • 1
    Possible duplicate of [Java: parse int value from a char](http://stackoverflow.com/questions/4968323/java-parse-int-value-from-a-char) – Tom Dec 10 '16 at 16:19

1 Answers1

1

You are converting from int to char and the value of the character 0 is 48' and therefore the difference.

TDG
  • 5,909
  • 3
  • 30
  • 51