Please tell the error in my logic

package questions;

import java.util.Scanner;

public class vonNeumanLovesBinary {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner sc=new Scanner(System.in);
	//int n=sc.nextInt();
	//for(int i=0;i<n-1;i++) {
		String str=sc.next();
		int k=str.length();
		int j=k-1;
		int ans=0;
		int base=1;
		while(j>=0){
			ans= ans + (str.charAt(j)*base);
			base=base*2;
			j--;
		}
		System.out.println(ans);
	}

}

Hi @vaibhavvishal98,

A character cannot be multiplied to integer … You need to convert ascii value of character to integer and the ascii value of 0 is 48 so i subtracted 48 from the character. Check the comment in the corrected code