Results 1 to 19 of 19
  1. #1
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Unhappy An "if" statement inside a "for" loop?

    It is fixed. The only thing I need help with now is how to select items from int[] arrays in an alternating pattern to create an int[] array or String with values from the two int[] arrays alternating.

    Java Code:
    package encoder1;
    class Encoder {
        public static void main(String[] args) {
    //USE ONLY LETTERS AND SYMBOLS LISTED IN ABCCHARS, NO NUMBERS ALLOWED
            String message="message to encode goes here!";
            message=message.toUpperCase();
    
            int messageLength=message.length();
            System.out.println("Segment length: "+messageLength+" characters.");
    //printing length of message above, declaring characters array below.
            char[] charsInit=new char[messageLength];
            for(int y = 0; y < messageLength ; y++){
                    charsInit[y]=message.charAt(y);
                }
    //y is index location in message and charsInit
            System.out.println(charsInit);
    //END OF SEGMENT VERIFICATION, BEGINNING ENCODING PROCESS...
            int[] abcFactors={10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
                              25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41};
            char[] abcChars={'A','B','C','D','E','F','G','H','I','J','K','L','M',
       'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ','?','.','!',',','$'};
    //input and output values can be adjusted with the two arrays above.
            int[] charFactors;
            charFactors=new int[messageLength];
    //Linking letters to respective numbers
    //inOut is index value in charsInit and charFactors
    //x is index value in abcChars and abcFactors
            for(int inOut=0; inOut < messageLength; inOut++){
            for(int x=0; x < abcChars.length; x++){
                if(charsInit[inOut]==abcChars[x]){
                    charFactors[inOut]=abcFactors[x];
                    System.out.println(charsInit[inOut]+" : "+charFactors[inOut]);
                    }
                }
            }
    //STAGE TWO OF ENCODING PROCESS, RANDOM NUMBERS AND SUB NUMBERS
            int[] randNum=new int[messageLength];
            int[] subNum=new int[messageLength];
            for(int r = 0 ; r < messageLength ; r++){
                randNum[r]=(int)(89 * Math.random() + 10);
            }
            System.out.println("FINISHED STAGE ONE");
    //defining randNum above, defining subNum below
            for(int s = 0 ; s < messageLength ; s++){
                if((randNum[s] + charFactors[s]) > 99){
                    subNum[s]=(randNum[s] - charFactors[s]);
                }else if((randNum[s] - charFactors[s] < 0)){
                    subNum[s]=(randNum[s] + charFactors[s]);
                }else if(randNum[s] <= 50){
                    subNum[s]=(randNum[s] - charFactors[s]);
                }else{
                    subNum[s]=(randNum[s] + charFactors[s]);
                }
            }
    //packaging encoded numbers in String: results
            int[] resultsS1=new int[messageLength];
            for(int f=0;f<messageLength;f++){
                resultsS1[f]=(100*randNum[f])+subNum[f];
            }
            for(int p2=0 ; p2 < messageLength ; p2++){
                System.out.print(resultsS1[p2]);
            }
            System.out.println(" ");
            System.out.println("Length of original message: "+messageLength);
            System.out.println("Length of encoded sequence: "+resultsS1.length * 4);
            System.out.println("Length of decoded message: "+resultsS1.length);
        }
    }
    Last edited by soccermiles; 04-19-2010 at 06:22 PM. Reason: update: complete

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Have a look at this:

    Java Code:
    public void encode(String message) {
    
       for (int i= 0; i < message.length(); i++)
          System.out.print((message.charAt(i)-'A'+1)+" ");
       System.out.println();
    }
    kind regards,

    Jos

  3. #3
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    In the code, I would encode each letter by finding a random number between 0 and 99, then take the number representing the letter and add or subtract that, then write down the two numbers side by side so that their difference is equal to the number representing the letter.
    ex: A=1=6867 or 3435 or 2019 or 1920
    Z=26=5630 or 3056 or 6288
    etc.
    NOTE: SEE ABOVE, PROGRAM HAS BEEN REVISED
    Last edited by soccermiles; 04-17-2010 at 05:48 PM. Reason: talking bout encoding process

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by soccermiles View Post
    I could do that, but then I would just be printing it.
    The printing was just for demonstration purposes; it doesn't take rocket science to fill another int array with those values.

    kind regards,

    Jos

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post
    Oh my god. How many if conditions you've in your code. I don't know how Jos read that code.
    I didn't; always when I see such repetetive code the first thing that comes up is: "the OP forgot something obvious" and I skip the rest of the wall paper ;-)

    kind regards,

    Jos

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    That's a good trick. What I'm normally doing is try to read the code, in that case if OP didn't use code tags, not intended properly, so on... I'm getter lazy to read and just leave.

  8. #8
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    That's a good trick. What I'm normally doing is try to read the code, in that case if OP didn't use code tags, not intended properly, so on... I'm getter lazy to read and just leave.
    sorry about the massive unreadable-ness of my code, I am still very new to java. Not yet used to annotating/applying tags.
    BTW, what is OP? Also, is there a statement that makes a random number? what kinds of numbers can it produce?
    also: I have no idea what ur doing in that piece of code you showed me (cuz imma newb at this).
    Last edited by soccermiles; 04-17-2010 at 05:46 PM. Reason: additional questions

  9. #9
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    if you say

    System.out.println(anArray);

    it will return the memory location of that array, not the values inside of the array.

    To print them you should loop through it.

    Java Code:
    for (int i = 0 ; i < anArray.length ; i++)
    {
        System.out.print(anArray[i]);
    }
    note that it doesn't actually println, just print, so this means the position of the next text will be on the same line.
    if you want a new line after just put
    System.out.println(""); or System.out.print("\n");
    after the loop.


    oops this was dangerously close to josahs post lol.
    Last edited by Cruncher; 04-17-2010 at 05:58 PM.

  10. #10
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Quote Originally Posted by soccermiles View Post
    Also, is there a statement that makes a random number? what kinds of numbers can it produce?
    Yes there is. I think you need to import java.util.Random; or java.util.*;

    Java Code:
    Random generate = new Random();
    int randomNumber = generate.nextInt(5);
    This will give 5 possible outcomes, 0, 1, 2, 3, 4.

    note that 5 is not possible. if you want 1 to 5 you would just put a +1 after it.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by soccermiles View Post
    sorry about the massive unreadable-ness of my code, I am still very new to java. Not yet used to annotating/applying tags.
    BTW, what is OP? Also, is there a statement that makes a random number? what kinds of numbers can it produce?
    also: I have no idea what ur doing in that piece of code you showed me (cuz imma newb at this).
    Never mind.

    OP stand for Original Poster, means the who is started the thread.

    Regarding your question, you want to generate a number, a random number. You can generate random number in two ways, using Math class or util package. Look at the following code segment,

    Java Code:
    public static void main(String[] args) {
            int m = (int)(99 * Math.random());
            System.out.println(m);
        }

  12. #12
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    tyvm, that helps ALOT.
    So, if you want the random function to repeat until 99 times you just say 99*math.random()?
    Last edited by soccermiles; 04-17-2010 at 06:37 PM. Reason: miswording

  13. #13
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by soccermiles View Post
    tyvm, that helps ALOT.
    So, if you want the random function to repeat until 99 times you just say 99*math.random()?
    No, Math.random() returns a random returns a double between 0.0 and 1.0, so to get a random int, you multiply the result of Math.random() by the number of possible int values you want to choose from (then add the offset, if there is one).

    So for a random int between 1 and 1000 (inclusive), you do:
    Java Code:
    import java.lang.Math;
    ...
            int randomInt = Math.random() * 1000 + 1;
    And for a random int between 101 and 200 (inclusive), you do:
    Java Code:
    import java.lang.Math;
    ...
            int randomInt = Math.random() * 100 + 101;
    Note that you could also do them this way:
    Java Code:
    import java.util.Random;
    ...
            Random rnd = new Random();
            int randomInt = rnd.nextInt(1000) + 1;
    and
    Java Code:
    import java.util.Random;
    ...
            Random rnd = new Random();
            int randomInt = rnd.nextInt(100) + 101;
    -Gary-

  14. #14
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    tyvm. the section of code at the top of this thread is now updated.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by soccermiles View Post
    tyvm. the section of code at the top of this thread is now updated.
    How was that now? Is it working as you expecting?

  16. #16
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Talking

    yes, it works just as planned. Here is the decoder (i haven't made a single coherent program out of these yet, not sure how to). I have also updated the original encoder for a couple of things.

    Java Code:
    package encoder1;
    public class Decoder {
        public static void main(String[] args){
    
            int[] message={1,2,3,4,2,2,0,8,6,9,9,7,4,6,1,8,8,1,9,1,4,6,3,0,1,3,2,7,
            9,4,5,8,2,6,5,5,4,2,1,8,3,2,6,8,5,4,6,8,5,8,8,1,9,8,8,6,7,2,9,6,6,2,7,5,
            9,2,7,8,2,0,5,6,9,7,8,1,5,2,7,6,1,0,2,4,3,7,0,9,6,7,3,1,4,5,2,8,8,1,9,5,
            2,0,4,7,3,3,1,9,2,0,5,9};
    //COPY+PASTE CODE TO DECODE IN ARRAY ABOVE, PLACE A COMMA BETWEEN EACH DIGIT
    //THE LIMITS OF THIS PROGRAM ARE UNKNOWN
            int messageLength=message.length / 4;
            System.out.println("Current encoded length: " + message.length);
            System.out.println("Length of original message: " + messageLength);
    
            int[] randNum=new int[messageLength];
            int[] subNum=new int[messageLength];
    
            for(int c=0;c<messageLength;c++){
                randNum[c]=(10*message[4*c])+(message[1+(4*c)]);
                subNum[c]=(10*message[2+(4*c)])+(message[3+(4*c)]);
            }
    
            int[] charFactors=new int[messageLength];
            for(int f=0;f<messageLength;f++){
                if(randNum[f]>subNum[f]){
                    charFactors[f]=randNum[f]-subNum[f];
                }else{
                    charFactors[f]=subNum[f]-randNum[f];
                }
            }
            int[] abcFactors={10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
                              25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41};
            char[] abcChars={'A','B','C','D','E','F','G','H','I','J','K','L','M',
       'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ','?','.','!',',','$'};
    //CHANGE THE TWO ARRAYS ABOVE TO MATCH THE ARRAYS FROM THE ENCODER
            char[] finals=new char[messageLength];
            for(int inOut = 0 ; inOut < messageLength ; inOut++){
                for(int x = 0 ; x < abcFactors.length ; x++){
                    if(charFactors[inOut] == abcFactors[x]){
                        finals[inOut] = abcChars[x];
                    }
                }
            }
            System.out.println("FINISHED DECODING");
            System.out.println(finals);
            System.out.println("Final length: " + finals.length);
        }
    }

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  18. #18
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    how do you mark as solved?

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  2. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  3. Replies: 2
    Last Post: 10-08-2009, 11:28 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •