Results 1 to 20 of 20
  1. #1
    kovalensue is offline Member
    Join Date
    Nov 2010
    Posts
    19
    Rep Power
    0

    Default Change of index in charArray

    Hi people, i've got a problem....a have charArray includes a,b,c,d,...and so on and i want to return chars with different index...so index 0 is A...but i want index 0 is B....how can i do this??

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You mean you want to remove the first element from the array? Or you just want to be able to refer to them by a certain number, so that element 'A' is index -1? I don't see why you'd want to do that as you can just add/subtract from an index variable to get your new index.

  3. #3
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    i completely not see why you'd want to do this.
    But you could do it as following i think.

    Java Code:
    charArray[0]=b;
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  4. #4
    kovalensue is offline Member
    Join Date
    Nov 2010
    Posts
    19
    Rep Power
    0

    Default

    I am not sure if i can explain it better (its really hard in english:D).....i have a homework from school to create Caesar cipher...and recommended procedure from teacher is....create alphabet array and then make a coding method which creates second array (cipher) with chars but chars will be moved by variable which is set in constructor....

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    If I understand correctly, you want to shift an array, for example, you start with
    a b c d
    shift it by one place and get
    b c d a
    Is this how it's supposed to be?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    kovalensue is offline Member
    Join Date
    Nov 2010
    Posts
    19
    Rep Power
    0

    Default

    yeah....thats it....

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Here's some pseudocode for you, it describes the solution to your problem, just translate it into java:
    Java Code:
    get the original array and offset
    create a new array of the same size as original
    for every index i from 0 to length of original array -1
      set the elemet at index i of new array to element of original array at index i+offset
    end loop
    Of course, doing just this doesn't quite work yet, as you'll get an ArrayIndexOutOfBoundsException as i+offset grows beyond the length of the array. That problem you should solve yourself, a quick hint, the modulo operator will be very usefull here.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  8. #8
    kovalensue is offline Member
    Join Date
    Nov 2010
    Posts
    19
    Rep Power
    0

    Default

    thank for help but i dont uderstand it well...i am realy beginner...i started programming two month ago....can you write a code...its better to understand it for me...

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Ok, but I won't write the missing part of the code, do at least a little bit yourself.
    Java Code:
    public static void shift(char[] array, int offset) {
      char[] ret = new char[array.length];
      for(int i = 0; i < array.length; i++)
        ret[i] = array[i+offset]; //something more needs to be done on this line
      return ret;
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    kovalensue is offline Member
    Join Date
    Nov 2010
    Posts
    19
    Rep Power
    0

    Default

    ok thanks a lot...i'll try to think about it...

  11. #11
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Another hint: you need some sort of formula to wrap around the indexes. Think of it this way:
    Java Code:
    newArray[i] = oldArray[expression];
    Think of the expression as a function of index and offset, eg f(index,offset) = newIndex. The plug in some values and try to figure out what the function is:
    Java Code:
    array length = 5, offset = 1
    f(0, 1) = 1
    f(1, 1) = 2
    f(2, 1) = 3
    f(3, 1) = 4
    //looking at these values would indicate the following
    //f(index, offset) = index+offset, but
    //when you reach 4 you need to wrap around, so you don't get
    //an array index out of bounds exception, so you get
    f(4, 1) = 0
    Another example
    Java Code:
    array length = 5, offset = 2
    f(0, 2) = 2
    f(1, 2) = 3
    f(2, 2) = 4
    f(3, 2) = 0
    f(4, 2) = 1
    So the question is, f(index, offset) = ???. Remember, the modulo operator is the missing piece of the puzzle.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  12. #12
    kovalensue is offline Member
    Join Date
    Nov 2010
    Posts
    19
    Rep Power
    0

    Default

    i get it.....yeah.....

    code: f(index, offset) = (index+offset)%array.length
    is it correct??

  13. #13
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Now doesn't that feel just great :D
    Good job man, keep plugging away, if you get stuck again, you know where to find us.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  14. #14
    kovalensue is offline Member
    Join Date
    Nov 2010
    Posts
    19
    Rep Power
    0

    Default

    mayday i stuck again:(....now i have methode to create new char array...and now i don know how to create method to translet the input text

    Java Code:
    public class Caesar
    {
        private int offset;
        private String output;
        private static final char [] CIPHER = { };
        private static final  char [] ALPHABET = {
            'a','A',
            'b','B',
            'c','C',
            'd','D',
            'e','E',
            'f','F',
            'g','G',
            'h','H',
            'i','I',
            'j','J',
            'k','K',
            'l','L',
            'm','M',
            'n','N',
            'o','O',
            'p','P',
            'q','Q',
            'r','R',
            's','S',
            't','T',
            'u','U',
            'v','V',
            'w','W',
            'x','X',
            'y','Y',
            'z','Z',
        };
        
    /**
      *  Constructor...
      */   
        public Caesar(int offset)
        {
            this.offset = offset;
        }
     /**
      *  Method for encrypting input text...
      */
        public String encrypt (String openText) {
            String input = openText;
            char [] inputChar = input.toCharArray();
            String output = "";
            for (int i = 0; i < vstupChar.length; i++){
              
            // i really dont know what write here i tried everything:(          
            ??output = output + CIPHER[i]??
            }
            return output;
        }
        
    /**
      *  Method which creates ciphered alpahbet
      */
        private void shift (char [] ALPHABET) {
            char [] CIPHER = new char[ALPHABET.length];
            for (int i = 0; i < ALPHABET.length; i++) {
                CIPHER [i] = ALPHABET [(i + offset)%ALPHABET.length];      
            }
            System.out.println (CIPHER);
            }
    
       
    }

    i was thinkin about it for hours....maybe i am just stupid....:D

  15. #15
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Heh, you should have said what your assignment is from the beginning. You don't really need to create the alphabet array and coded array. But, the wraparound logic you figured out will still be usefull, I'll explain in a bit.
    Characters are basically just integers, so you can apply the offset directly to them. You had the right idea with converting the input String to a char array, now simply construct another char array for the encoding. A small snippet explaining this is here:
    Java Code:
    public class CharsAreInts {
      public static void main(String[] args) {
        char a = 'a';
        System.out.println(a); //prints out a
        System.out.println(a+3); //prints out d
        //if you cast a char to int, you find out the numeric values of letters
        System.out.println((int)'a'); //prints out 97
        System.out.println("a: "+(int)'a'+", z: "+(int)'z'+", A:"+(int)'A'+", Z:"+(int)'Z'); //prints out the ranges of numbers representing letters
      }
    }
    Now, the wraparound logic will be similar, if you encouter a 'z' character, you'll need to print out 'c', but you should differentiate a case where the letter is upper case or lower case, to get the correct number.
    Also, I hope I'm making any sense right now, as the sleep deprivation is getting to me :D
    Ever seen a dog chase its tail? Now that's an infinite loop.

  16. #16
    kovalensue is offline Member
    Join Date
    Nov 2010
    Posts
    19
    Rep Power
    0

    Default

    oh thanks...i think so i dont have to create arrays...i need to find values of chars and then use only alphabet chars....i just wanted to do it how profesor recommended..maybe i didnt uderstand him

  17. #17
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Just because someone is a professor, doesn't mean they're any good at what they do. It's very common (at least at my uni) for a professor to teach a course that is quite removed from his area of expertise, so always take a teachers advice with a grain of salt.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  18. #18
    kovalensue is offline Member
    Join Date
    Nov 2010
    Posts
    19
    Rep Power
    0

    Default

    one more question....i have a problem if i want to move the charr array back....

    f(index, offset) = (index - offset)%array.length

    but it doesnt work...any hints??

  19. #19
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Because this can generate negative numbers. For the switch back to original alphabet, you'd be better off using (index + (array.length-offset))%array.length.
    Also, I hope i didn't just put my foot in my mouth, as sleep deprivation is getting to be a common phenomena.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  20. #20
    kovalensue is offline Member
    Join Date
    Nov 2010
    Posts
    19
    Rep Power
    0

    Default

    thanks....i think i have a cipher...and it works....YEEEHAAA

Similar Threads

  1. Replies: 0
    Last Post: 10-29-2010, 09:15 PM
  2. Index tables
    By andy16 in forum JDBC
    Replies: 1
    Last Post: 05-27-2010, 10:48 AM
  3. Index of an int
    By wake in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 02:54 PM
  4. I can't run my index.jsp
    By gissah in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 02:42 AM
  5. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 07:33 AM

Tags for this Thread

Posting Permissions

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