Results 1 to 6 of 6
  1. #1
    JayP is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default Problems with empty arrays (Putting

    Hi

    I've been trying to find a way to put the first character of a string to the end of the string. What I'm doing is the following:

    • Create an char array with the single letters in each slot.
    • Taking the first character and putting it into the last array number + 1.
    • making the first array[0] = null; (and this is where the problem lies)


    When I make the array[0] = null and System.out.print(array); it gives me an error because its empty. I also tried doing array[0] = " "; and still gives me the same error.

    I don't have the script with me its at school but thats how I'm doing it sorry I can't get the error message.

    Is there a way to destrow the first array[0]? so I don't need it, or possibly make a loop that shifts everything over -1? But that seems long a redundant.

    Any help is greatly appreciated!

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Arrays do not support shifting.
    Try using a loop, setting index i to i+1. Stop when i+1 is the last element. Then you can set the last element to the save char from the beginning.

  3. #3
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    "Taking the first character and putting it into the last array number + 1."

    This will cause an IndexOutofBounds exception because as you are saying you are inserting it into the last array index + 1.

    Unless there is a reason you are not using the substring String function I would take a look at that.

    String (Java 2 Platform SE v1.4.2), int)

    I think the pseudo code for what you are trying to accomplish with the method you are attempting above would be something like this


    Java Code:
    String to char array
    Store index 0 (first char) to temp variable
    
    Loop char array from index 1 to last index
       retrieve current index of char array and insert into (current index - 1)
    End Loop
    
    insert temp variable (first char) to last index of char array
    
    convert char array to String -- Return or Print String

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Use a container that allows the removing and adding of elements. See Vector or ArrayList
    Arrays require moving ALL of the elements.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I'd just use substring. It would make it much easier.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I just read this:
    making the first array[0] = null; (and this is where the problem lies)
    null is used for reference types (objects), you cannot use it for/with primitives.

Similar Threads

  1. Arrays and use of null or empty inputs
    By Desmond in forum New To Java
    Replies: 6
    Last Post: 07-27-2010, 04:54 PM
  2. two problems with 2D arrays
    By doha786 in forum New To Java
    Replies: 0
    Last Post: 04-05-2010, 06:06 AM
  3. empty arrays.
    By circuspeanuts in forum New To Java
    Replies: 11
    Last Post: 04-06-2009, 07:08 PM
  4. Putting controls in arrays?
    By Cymro in forum New To Java
    Replies: 4
    Last Post: 02-01-2008, 06:54 PM
  5. Problems with arrays
    By Marcus in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:10 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
  •