-3

When I am trying to convert a string from integer using parseint() it is not considering any leading zeros (eg: "00100100" will be converted to an integer as "100100"). The application that I am trying to build requires the leading zeros to be there. Is there any method through which I can achieve that or anything that I can do with parseint() that will keep the leading zeros.

Note: I need the output in integer format and have the input in string format.

Edit: I need to perform the boolean operation so i need those zeros. Is there any way to perform boolean operations on String or any way that I can convert it to integer and can perform the operation. Because everytime the String starts with zero the boolean operations are giving wrong results

S.Mehta
  • 15
  • 1
  • 7
  • 6
    An `int` is a number. Numbers are numbers; they have no notion of leading zeroes. You want a string. – SLaks Jun 20 '17 at 16:58
  • 8 x 0 & 1 are you sure you not dealing with bits? – Alex K. Jun 20 '17 at 16:59
  • Integers don't have leading zeros, so you're operating under a misconception. If the leading zeros are required to be there in the receiving application, then it's not expecting integers, but strings. There is no such number as 01. – Ken White Jun 20 '17 at 16:59
  • 1
    Pretty sure you want to deal with bits – Felix Jun 20 '17 at 17:03
  • Maybe https://stackoverflow.com/questions/9333681/java-bitset-example –  Jun 20 '17 at 17:05
  • @AlexK. I am dealing with bits but the bits are stored in a string and I need to perform boolean operations. Is there any way I can achieve that. – S.Mehta Jun 20 '17 at 17:05
  • @S.Mehta: BitSet https://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html – D. Kovács Jun 20 '17 at 17:49

2 Answers2

0

Better off storing each number in an integer array if you really need the leading 0s.

int num[] = {0,0,0,1,0,1,0};

Dan
  • 31
  • 2
0

I created and ArrayLIst and then when I was passing individual bits to it, "0" were getting accepted

ArrayList<String> var = new ArrayList<>();
var.add("0010101000");
var.add("0010101010");

ArrayList<Integer> var1 = new ArrayList<>();

String var2 = var.get(0).substring(0,1);
int var3 = Integer.parseInt(var2);
System.out.println(var3);

This will display 0 as integer

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
S.Mehta
  • 15
  • 1
  • 7