-3

My java program won't convert unicode to int, but will convert ascii to int. The first line works and converts ascii 53 to int 5. However, the second line with the unicode value stops the program.

first = Integer.parseInt(String.valueOf(operatorStack.pop()));  //this pop is ascii 53
second = Integer.parseInt(String.valueOf(operatorStack.pop())); //this pop is unicode \u0004

Here are the errors it is throwing:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:646)
    at java.base/java.lang.Integer.parseInt(Integer.java:778)
Jon
  • 1
  • 3

1 Answers1

1

This doesn't seem to be anything to do with "ASCII" or "Unicode".

An empty string is not a number - that is what the exception message is telling you.

java.lang.NumberFormatException: For input string: ""

So you popped an empty string. You need to figure out why.

user16632363
  • 1,050
  • 3
  • 6
  • It didn’t translate here correctly but it had an empty set symbol. O with strikethrough in between the “ “. I don’t think it’s empty because when I debug it’s showing me the /u0004 on top of the stack. – Jon Oct 26 '21 at 23:25
  • 1
    `\u0004` is the ASCII control character for end of transmission. Of course you cannot parse it as an integer. Which number had you expected? – Ole V.V. Oct 26 '21 at 23:29
  • I was hoping for it to produce an int 4 – Jon Oct 26 '21 at 23:32
  • `\u0034` is the unicode value for the number 4 – TheNytangel Oct 26 '21 at 23:38
  • 1
    When I add integers 1 and 3 together, I get 4 and assign it to variable temp. Then I do, char c = (char)temp. Then it shows me that c is now equal to '\u0004' 4 – Jon Oct 26 '21 at 23:49
  • yes, because you can do something like `char c = 4;`. To do the reverse, you can try `String.charAt` – f1sh Oct 26 '21 at 23:52
  • `int second = '\u0004';` will give you 4. `Integer.parseInt()` will not. – Ole V.V. Oct 27 '21 at 00:29
  • 1
    It seems you are mixing (and hence confusing) which character values in your stack are digit characters and which are disguised integer values. Also while 5 can be represented by the char `'5'` (unicode 53), how would you represent 13? Asking because it is not a single digit (in decimal). I’m afraid you will have to think over. – Ole V.V. Oct 27 '21 at 00:34
  • You should have written this example code using values we can examine and use to reproduce your problem. – Basil Bourque Oct 27 '21 at 04:26