Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Help with code for converting 4 digit string to integer

1. ## 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.

/**
* 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 {

/**
* Construct a Convert4Positions object with the specified
*/
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
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
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 );
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" );
}
}

2. ## Re: Help with code for converting 4 digit string to integer

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.

3. ## 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.

4. ## Re: Help with code for converting 4 digit string to integer

What do you mean by "under"? The location of methods in not important.
public String int2string( int value ) {
// hint: see Character.forDigit
String str = "";
// add code here to finish method
return str;
}
See comment above.

5. ## 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

6. ## 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.