Results 1 to 20 of 20
Thread: Change of index in charArray
- 11-07-2010, 09:49 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 19
- Rep Power
- 0
- 11-08-2010, 06:37 AM #2
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.
- 11-08-2010, 07:57 AM #3
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
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
- 11-08-2010, 02:11 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 19
- Rep Power
- 0
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....
- 11-08-2010, 02:22 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 11-08-2010, 02:41 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 19
- Rep Power
- 0
yeah....thats it....
- 11-08-2010, 02:46 PM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Here's some pseudocode for you, it describes the solution to your problem, just translate it into java:
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.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
Ever seen a dog chase its tail? Now that's an infinite loop.
- 11-08-2010, 02:59 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 19
- Rep Power
- 0
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...
- 11-08-2010, 06:08 PM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 11-08-2010, 07:05 PM #10
Member
- Join Date
- Nov 2010
- Posts
- 19
- Rep Power
- 0
ok thanks a lot...i'll try to think about it...
- 11-08-2010, 08:05 PM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Another hint: you need some sort of formula to wrap around the indexes. Think of it this way:
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:newArray[i] = oldArray[expression];
Another exampleJava 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
So the question is, f(index, offset) = ???. Remember, the modulo operator is the missing piece of the puzzle.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
Ever seen a dog chase its tail? Now that's an infinite loop.
- 11-08-2010, 09:15 PM #12
Member
- Join Date
- Nov 2010
- Posts
- 19
- Rep Power
- 0
i get it.....yeah.....
is it correct??code: f(index, offset) = (index+offset)%array.length
- 11-08-2010, 09:23 PM #13
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 11-08-2010, 11:53 PM #14
Member
- Join Date
- Nov 2010
- Posts
- 19
- Rep Power
- 0
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
- 11-09-2010, 12:38 AM #15
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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:
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.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 } }
Also, I hope I'm making any sense right now, as the sleep deprivation is getting to me :DEver seen a dog chase its tail? Now that's an infinite loop.
- 11-09-2010, 08:18 AM #16
Member
- Join Date
- Nov 2010
- Posts
- 19
- Rep Power
- 0
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
- 11-09-2010, 02:44 PM #17
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 11-10-2010, 01:29 AM #18
Member
- Join Date
- Nov 2010
- Posts
- 19
- Rep Power
- 0
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??
- 11-10-2010, 01:40 AM #19
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 11-10-2010, 11:34 AM #20
Member
- Join Date
- Nov 2010
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Pulling an Index made with Lucene to dev batch code for index listings
By txgeekgirl in forum LuceneReplies: 0Last Post: 10-29-2010, 08:15 PM -
Index tables
By andy16 in forum JDBCReplies: 1Last Post: 05-27-2010, 09:48 AM -
Index of an int
By wake in forum New To JavaReplies: 4Last Post: 02-24-2010, 01:54 PM -
I can't run my index.jsp
By gissah in forum New To JavaReplies: 0Last Post: 03-23-2009, 01:42 AM -
Replacing at an index
By bugger in forum New To JavaReplies: 2Last Post: 01-29-2008, 06:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks