1

I was wondering if there is a better implementation for converting the following VB6 snippets in Immediate window to Java:

? Format("002-", "")
-2
? Str("002-")
-2

My Java code scans through the string and if it sees a negative sign, converts the string to a negative integer, else positive. I would like to know if Java has a utility function which functions the same way as this.

A link to the Str function here.

Thanks!

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
nmenego
  • 846
  • 3
  • 17
  • 36

1 Answers1

0

I believe this is what you're looking for:

VB6       Java
---       -----
Str()     Integer.toString ();  # Convert integer to string
***       Integer.ParseInt ();  # Convert string to integer
Format()  String.Format ();

Here are some links:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Integer.parseInt("002-"); does not parse similar to Str(). I think it throws a NumberFormatException. – nmenego Jun 22 '12 at 18:26
  • @nmenego - you're correct. Before you edited your post with the VB.Net link, I incorrectly recalled VB6 "Str()" as string-to-integer. I filled out the table with both variants: string to integer (parseInt()), and integer to string (toString()). – paulsm4 Jun 22 '12 at 18:29
  • Hmm.. I don't think it'll work. I am give a string "23-" and it is supposed to be converted to -23 int. – nmenego Jun 22 '12 at 18:38
  • Q: Why are you giving a string of "23-" (instead of "-23")? And you're saying VB used to accept it? That suprises me ;). Q: Do you know if VB.Net also accepts it? Q: What was the reason? Q: Can you just pass in a string with the sign in the usual place? – paulsm4 Jun 22 '12 at 19:52
  • Suggestion: just use a Java [regex](http://docs.oracle.com/javase/tutorial/essential/regex/) to convert the string (before you try to parse it): – paulsm4 Jun 22 '12 at 19:54
  • Yes, VB can parse both 23- and -23. I have no idea if VB.net does the same. The system I am converting Java from VB receives that string '23-' and the VB code parses it to be -23. – nmenego Jun 23 '12 at 03:36