Results 1 to 6 of 6

Thread: Array Help

  1. #1
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default Array Help

    I'm writing a method to take an integer from the user and swap each pair of digits starting from the least significant.

    E.g., if the method is called with 232563 it would return 325236, or 1804982 returns 1089428. In the second case, the most significant digit, the "1", has no digit to swap with so remains in the same position.

    So I've made it accept and integer input and convert it into an array of chars, but I'm stuck on how to do the swapping operation. I don't want the code - just some ideas on how to approach it. Could anybody help?

    Many thanks,

    Jonathan

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    You could check de length of the array and then swap the elements of the positions array[0] and array[length-1] using a aux variable.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Loops backwards through the array, using a starting index of the last element (length-1).
    In each cycle you will swap the current index entry with the one at index-1 (ie the previous entry).
    Decrement the index by 2 each time.
    Keep doing this while your index > 0.

    You shouldn't need a new array to work with, just use the existing one...unless you feel you need a new one to try and keep things straight in your head.

  4. #4
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default

    Ok great thanks... I'll let you know how I get on!

  5. #5
    jkhamler is offline Member
    Join Date
    Nov 2009
    Posts
    24
    Rep Power
    0

    Default

    Ok I need some help with my syntax... I've got into a bit of a muddle mixing up ints and chars... this is as far as I've got:

    public class Q3
    {
    public static void main (final String [] args)
    {
    KeyboardInput in = new KeyboardInput() ;
    System.out.println("Please enter an integer: ") ;
    int a = in.readInteger() ;
    System.out.print("Digits reversed: "+digitSwap(a)) ; //calls 'digitSwap' method
    }

    public static int digitSwap(int a) // "digit swap" method
    {
    String s = a+"" ; // Converts integer to string
    char[] arr = s.toCharArray(); // Converts string to array of chars

    for (int i = arr.length-1; i > 0; i=i-2); // loops backwards through the array
    {
    int swapArr = arr[i];
    arr[i] = arr[i-1]; // swaps current index entry with the previous entry
    arr[i-1] = swapArr;

    }
    String str = new String(arr); // Converts array of chars back into a string
    int i = Integer.valueOf(str); // Converts string back into an integer
    return i;
    }

    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Use code tags when posting code. That's the first thing, because otherwise we're looking at unformatted code which I find very difficult to read quickly.

    You ought to also tell us what errors you're getting...compiler errors in this case.
    However:

    Java Code:
    for (int i = arr.length-1; i > 0; i=i-2);
    your for loop does nothing due to that pesky semi colon.

    And once you've done that, you have an array of chars, which is fine, but what are you putting into this array here:
    Java Code:
    arr[i-1] = swapArr;
    ?

Similar Threads

  1. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  2. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  3. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •