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 14 of 14

Thread: How To Compare String input to char Array....

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default How To Compare String input to char Array....

    Good evening everybody. I hope you all are well. SoS on my end.

    Anyway, at the moment I'm sort of stuck on a "how will I do that" hump and I'm hoping someone won't mind pushing me over. What I'm trying to do is compare String input to a char array. Let me make it a little more plain, I'm working on a cipher assignment, and my line of thought is this: I will get String input from the user, COMPARE the characters in the string input to an alphabet array, which will then be compared to the cipher array so that the cipher's counterpart can be chosen over the alphabet's. Does that make sense? If so, and this isn't a horrible approach to the problem, can anyone suggest a way that I might compare the random input keyed in by the user to that alphabet array?

    Anyone?........Anyone?........Anyone?



    Thanks all the same.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How To Compare String input to char Array....

    The thread title and description don't correspond, so I'm not sure what the question is. Can you show an example of what you're trying to do, step by step? Input word, comparison to alphabet array, compared to cipher array, resulting output word. It sounds like a simple substitution cipher, but I'm not completely sure.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How To Compare String input to char Array....

    Well, that's pretty much what it is, an Atbash cipher. I can compare the two arrays, one being the standard alphabet, and the other being the cipher using the following code:
    public class ArrayTest
    {
       public static void main(String[] args)
       {
          char [] alphabet;
          alphabet = new char[26];
          alphabet[0] = 'A';
          alphabet[1] = 'B';
          alphabet[2] = 'C';
          alphabet[3] = 'D';
          alphabet[4] = 'E';
          alphabet[5] = 'F';
          alphabet[6] = 'G';
          alphabet[7] = 'H';
          alphabet[8] = 'I';
          alphabet[9] = 'J';
          alphabet[10] = 'K';
          alphabet[11] = 'L';
          alphabet[12] = 'M';
          alphabet[13] = 'N';
          alphabet[14] = 'O';
          alphabet[15] = 'P';
          alphabet[16] = 'Q';
          alphabet[17] = 'R';
          alphabet[18] = 'S';
          alphabet[19] = 'T';
          alphabet[20] = 'U';
          alphabet[21] = 'V';
          alphabet[22] = 'W';
          alphabet[23] = 'X';
          alphabet[24] = 'Y';
          alphabet[25] = 'Z';  
          System.out.println(alphabet);
     
          char [] cipher;
          cipher = new char[26];
          cipher[0] = 'Z';
          cipher[1] = 'Y';
          cipher[2] = 'X';
          cipher[3] = 'W';
          cipher[4] = 'V';
          cipher[5] = 'U';
          cipher[6] = 'T';
          cipher[7] = 'S';
          cipher[8] = 'R';
          cipher[9] = 'Q';
          cipher[10] = 'P';
          cipher[11] = 'O';
          cipher[12] = 'N';
          cipher[13] = 'M';
          cipher[14] = 'L';
          cipher[15] = 'K';
          cipher[16] = 'J';
          cipher[17] = 'I';
          cipher[18] = 'H';
          cipher[19] = 'G';
          cipher[20] = 'F';
          cipher[21] = 'E';
          cipher[22] = 'D';
          cipher[23] = 'C';
          cipher[24] = 'B';
          cipher[25] = 'A';
          System.out.println(cipher);
     
          for (int i = 0; i < alphabet.length; i++) //Here I compare the arrays and convert cipher to alphabet.
          {
             cipher[i] = alphabet[i];
     
          }
            System.out.println(cipher); 
     
       }
    }

    That works fine, but my question is this: how will I be able to compare the actual input that the user keys in, to the alphabet array?

    Something like the following?
    while(input.next()==alphabet[i])

    I'm just not sure.
    Thanks for replying.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How To Compare String input to char Array....

    The String class has a method that will return a char value from the String. It also has a method to create a char array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How To Compare String input to char Array....

    Looking those up now. Thanks Norm.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How To Compare String input to char Array....

    You might be interested to know that

    alphabet[1] = alphabet[0] + 1;

    You didn't need to type all of that out. Similarly could have been done for your cipher[] array, but I would have generated that randomly.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How To Compare String input to char Array....

    GregBrannon, yes, thank you. I definitely needed to know that. I knew there had to be a more efficient way to build those arrays. I knew enough to know that I should be embarrassed by having keyed all that in. Alas...I have much to learn. Thanks again.

  8. #8
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How To Compare String input to char Array....

    Actually, to be honest, although I'm sure this works else you wouldn't have said so, I don't quite understand it...

    alphabet[1] = alphabet[0] + 1;

    Or perhaps I don't understand what this statement can take the place of. I'm assuming that you mean the complete alphabet array that I typed out. If that is the case, unless the alphabet is just something assumed by JAVA, how will it know what the next character should be and so on?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How To Compare String input to char Array....

    That expression is a comment on the letters in the alphabet being next to each other in numeric value.
    For example: 'b' - 'a' = 1 or 'a' + 1 = 'b'

    A java statement would require casting because the compiler converts most numeric expressions to int:
    char nextChar = (char) ('a' + 1);
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How To Compare String input to char Array....

    Ahhh, ok. I see. And I see how that can be useful.

    Let me ask you something else Norm, if you don't mind: I have what I call my general Neandarcode written for this program and would like to have it critiqued. Where would you advise I post it, before I post it in the wrong thread. You see, this program is to use command-line arguments and written in Eclipse, neither of which I am very familiar with and therefore have no idea how I can test it.

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How To Compare String input to char Array....

    I apologize if I was too terse. My point was that you could do something like:
    char[] alphabet = new char[26];
    alphabet[0] = 'A';
     
    // fill in the rest of the alphabet array
    for ( int i = 1 ; i < alphabet.length ; i++ )
    {
        alphabet[i] = (char)( alphabet[i - 1] + 1 );
    }

  12. #12
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How To Compare String input to char Array....

    No, no. No need to apologize. You were quite clear. I just don't have my best "listening ears" on right now.

  13. #13
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How To Compare String input to char Array....

    To the moderator on-duty, or anyone who knows:
    I have what I call my general Neandarcode written for this program and would like to have it critiqued. Where would you advise I post it, before I post it in the wrong thread. You see, this program is to use command-line arguments and written in Eclipse, neither of which I am very familiar with and therefore have no idea how I can test it.

  14. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How To Compare String input to char Array....

    I'm not sure what you want. Do you want to know:

    1. How to add command-line inputs when running in Eclipse?
    2. How to run a program written in Eclipse from a command line?
    3. Someone to smell your code and tell you what smells?

    When you figure out the question, post an appropriate thread in an appropriate location. If none seem right, use "What's wrong with my code?" where most everything ends up anyway.

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. Converting a 2-D char array into a string
    By mist4lyf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 12th, 2013, 08:28 AM
  3. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  4. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 10:04 AM
  5. How do I compare between the array of char and array of words !!?
    By bady2050 in forum Collections and Generics
    Replies: 1
    Last Post: May 5th, 2012, 05:36 PM