Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Loop to note position of lowest number

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop to note position of lowest number

    Hi guys,

    I have a password key which is "password12Qcb6z:!sh:&BYztwYTPM" which i have repeating a number of times in a 1d array.

    What i am trying to create an array where numbers are assigned starting with 1, and they are assigned first by alphabetical order, and second, where the same letter appears twice, by position in the word.


    for example:

    C O N V E N I E N C E
    1 10 7 11 3 8 6 4 9 2 5


    my key is alot longer than "CONVENIENCE" its roughly 328 characters long ("password12Qcb6z:!sh:&BYztwYTPM" repeated ). so i have a 1d int array array which i want to map out 1-328 following the rule above starting wit the lowest character in its ascii values 32 = SPACE to ~ = 127. so i then have a array mapping out 1-328 ...


    I have this code so far

    int v=0, count=0, r=32; // 33 is the first usable ascii character in test file 
    int[] colprintorder = new int[numberofcols];
     
     
    while(r<=127) // loop unit last usable ascii char
    {
        for(int i=0;i<passwordinbyte.length;i++) // go through each cell of  the array
        {
     
           if (r == passwordinbyte[i]) // if the value of r is the same as what is in passwordinbyte  map its position storing in colprintorder
           {
           colprintorder[count] = i ;
           count++; // next cell in the array
           }
        }  
     r++; // look for the next characters ascii value  32-127
    }
     
    for(int a=0; a<colprintorder.length; a++) // print out colprint order
    {
       System.out.println( colprintorder[a] + " " );
    }
     
    System.out.println("lengthhrr" +passwordinbyte.length);


    Here is my output using the code snippet above:


    http://i57.photobucket.com/albums/g2...e2k/lolcat.jpg


    it appears theres somthing wrong with my code but i dont know what can you guys help me ?


    thank you
    Last edited by fortune2k; November 22nd, 2010 at 03:42 PM.


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop to note position of lowest number

    I have added a better testing print out below

     Coloumn print order: 
       17   19   10     8       9     12    14    15    11    1     7    16    5    0      6     2      3     18    4   13
     Password in bytes: 
      112   97  115  115  119  111  114  100   49   50   47   70   50  123   52   57  104   33  118   43
     Password in characters: 
        p    a       s       s     w      o      r      d       1    2      /     F     2     {       4     9      h     !     v     +

    as you can see the lowest ascii number charcter is '+' which has the value of 43 however it has been put at position of 13. (http://www.cs.utk.edu/~pham/ascii_table.jpg)

    the coloumn array need to map the password in bytes in order. Does anyone know what im doign wrong

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Loop to note position of lowest number

    Ok, I'm a little confused (maybe this is above me or something).

    So, you are wanting to get the ASCII values of all of the characters, then determine, based on their ASCII values, which come first, second, third, ect.

    If so, I am unsure why you have all that code. You can convert a String to a char[] using the String.toCharArray() method, then sort the char[] with the Arrays.sort(char[]) method, then do whatever with the sorted array.

    When I input CONVENIENCE in the code below:
    import java.util.Arrays;
     
    public class ASCIITesting
    {
        public static void main(String[] args)
        {
        	String input = "CONVENIENCE";
        	char[] charArr = input.toCharArray();
        	Arrays.sort(charArr);
        	for(int i=0;i<charArr.length;i++)
        		System.out.print(Character.toString(charArr[i]));
        }
    }
    It outputs:
    CCEEEINNNOV
    which is the word sorted based on each character's ASCII value.
    Similarly, if I input password12Qcb6z:!sh:&BYztwYTPM it outputs:
    !&126::BMPQTYYabcdhoprssstwwzz
    Am I totally off?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop to note position of lowest number

    yes you are off but after 2 hours if staring at the screen i figured it out .

    Than kyou for your help

Similar Threads

  1. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  2. Replies: 1
    Last Post: October 16th, 2010, 03:32 PM
  3. [SOLVED] last character position
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 05:31 AM
  4. unsafe operations note??
    By bookface in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 22nd, 2010, 09:58 AM
  5. Can a for loop work with random number?
    By humdinger in forum Loops & Control Statements
    Replies: 7
    Last Post: January 10th, 2010, 10:06 PM