# Help with code for converting 4 digit string to integer

• October 24th, 2011, 08:08 PM
danielp1213
Help with code for converting 4 digit string to integer
Hey i was just wondering if someone cold help me with this code to convert a 4 digit string into an integer ad vice versa. I have the instance variables and stuff but i think some of my code is in the wrong place.

sorry if the code doesn't come out right

I am pretty sure the int2string and string2int are in the wrong place but correct me if I am wrong.

Code :

```/** * The Convert4Positions class will convert a string of length four * into a positive integer, and a positive integer into string of * length four. The radix of the string is specified with the * a Convert4Positions object is created. * * @author Daniel Stratton */ public class Convert4Positions { private int radix;   /** * Construct a Convert4Positions object with the specified * radix. * @param radix the radix for the conversions. */ public Convert4Positions( int radix ) { Convert4Positions decimal = new Convert4Positions( 10 ); assert decimal.int2string( 8976 ).equals( "8976" ); assert decimal.string2int( "6384" ) == 6384; Convert4Positions octal = new Convert4Positions( 8 ); assert octal.int2string( 01777 ).equals( "1777" ); assert octal.string2int( "02534" ) == 2534; Convert4Positions binary = new Convert4Positions( 2 ); assert binary.int2string( 1101 ).equals( "1101" ); assert binary.string2int( "1001" ) == 1001; }   /** * int2string converts an integer to 4 position string. * * @param value, the value to convert * @return a string with four digits that represents the integer */ public String int2string( int value ) { // hint: see Character.forDigit String str = "";   return str; }   /** * string2int converts a string of length four into an integer. * The class uses the defined radix for the converion. * * @param str, the string to convert * @return the integer value of the converted string. */ public int string2int( String str ) { // hint: use charAt(0) for first char, charAt(1) for next, ... // hint: see Character.digit int v = 0; return v; }     // code for testing public static void printInt2StringDecimal(Convert4Positions conv, int v ) { String sv = conv.int2string( v ); int iv = conv.string2int( sv ); // %04d converts an integer into a decimal number with 4 postions // with zero padding System.out.printf("int2string: %04d is %s%n", v, sv ); System.out.printf("string2int: %s is %04d%n", sv, iv ); }   public static void printInt2StringOctal(Convert4Positions conv, int v ) { String sv = conv.int2string( v ); int iv = conv.string2int( sv ); // %04o converts an integer into a octal number with 4 postions // with zero padding System.out.printf("int2string: %04o is %s%n", v, sv ); System.out.printf("string2int: %s is %04o%n", sv, iv ); }   public static void printInt2StringBinary(Convert4Positions conv, String sv ) { int iv = conv.string2int( sv ); String ssv = conv.int2string( iv ); // there will be no leading zeros System.out.printf("int2string: %s is %s%n", sv, Integer.toBinaryString(iv) ); // I am using the orginal sv System.out.printf("int2string: %s is %s%n", sv, ssv ); }   /** * Provide simple console output based testing. */ public static void main( String[] args ) { System.out.println("Decimal"); Convert4Positions decimal = new Convert4Positions( 10 ); printInt2StringDecimal( decimal, 1234 ); printInt2StringDecimal( decimal, 9876 ); printInt2StringDecimal( decimal, 47 );   System.out.println(); System.out.println("Octal"); Convert4Positions octal = new Convert4Positions( 8 ); // In Java, octal numbers start with 0 printInt2StringOctal( octal, 01234 ); printInt2StringOctal( octal, 07654 ); printInt2StringOctal( octal, 034 );   System.out.println(); System.out.println("Binary"); Convert4Positions binary = new Convert4Positions( 2 ); printInt2StringBinary( binary, "0101" ); printInt2StringBinary( binary, "1001" ); } }```
• October 24th, 2011, 08:22 PM
Junky
Re: Help with code for converting 4 digit string to integer
Quote:

Originally Posted by danielp1213
I am pretty sure the int2string and string2int are in the wrong place but correct me if I am wrong.

Being in the worng place is not the issue. The problem is those methods do not do what they are supposed to. All int2String does is return an empty string. All string2int does is return 0. I assume your assignment is to actually complete the code in those methods.
• October 24th, 2011, 08:48 PM
danielp1213
Re: Help with code for converting 4 digit string to integer
yeah that is what I mean. Do they go under the other methods int2 string and string2int ??

Could I use
String str = "012";
char ch0 = str.char AT (0);

Or something like that? There is a start under each method.
• October 24th, 2011, 09:05 PM
Junky
Re: Help with code for converting 4 digit string to integer
What do you mean by "under"? The location of methods in not important.
Code java:

```public String int2string( int value ) { // hint: see Character.forDigit String str = ""; // add code here to finish method return str; }```
See comment above.
• October 24th, 2011, 09:34 PM
danielp1213
Re: Help with code for converting 4 digit string to integer
yeah thats what I mean where you have ass code here. Do i use assert decimal.int2string( 8976 ).equals( "8976" ); OR do I use String str = "012"; char ch0 = str.char AT (0); ETC
• October 24th, 2011, 09:38 PM
Junky
Re: Help with code for converting 4 digit string to integer
You add code that will convert the int to a String, just like the method name says it should do.