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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

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

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    Thanks in advance

    /**
     * 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" );
        }
    }
    Last edited by danielp1213; October 24th, 2011 at 08:10 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

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

    Quote Originally Posted by danielp1213 View Post
    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.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

Similar Threads

  1. Converting Code to Multi Thread
    By cmill_xc in forum Threads
    Replies: 1
    Last Post: December 6th, 2010, 12:39 PM
  2. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 10:24 PM
  3. Code Stop working after converting to jar?
    By Ron6566 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2010, 12:17 PM
  4. Converting to String
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 13th, 2010, 06:09 PM
  5. [SOLVED] Please Review My Code (Long Integer Addition)
    By Saradus in forum Java Theory & Questions
    Replies: 10
    Last Post: July 4th, 2009, 12:55 PM