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

Thread: String Arrays []

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default String Arrays []

    Hi guys,

    As part of a Research project in cryptography i am doing a very basic model of a dictionary attack. This is not an actual dictionary attack just to help explain things in my essay.

    So here is what Im trying to do:

    I have made a very small dictionary using String Array that contains just around 50 words or so.
    I have Created Scanner object to read in user input.

    Here is what I have so far:

    String[] words = new String[] { "animal", "anthony", "bear", "bright", 
                "cat", "cities", "control", "copper", "country", "degree", "driving",
                "electric", "earth", "eggs", "expert", "family", "female", "flying",
                "friend", "fuit", "guessing", "german", "gold", "green", "happy", 
                "harbour", "heart", "history", "horse", "insect", "interest", "internet",
                "island", "jelly", "journey", "judge", "kettle", "kiss", "language",
                "laugh", "letter", "loud", "machine", "male", "manager", "market",
                "married", "measure", "monkey", "noise", "nose", "number", "officer",
                "opinion", "other", "orange", "paint", "parcel", "potato", "quick",
                "quiet", "reading", "record", "snow", "school", "science", "ship",
                "teacher", "thunder", "trousers", "value", "voice", "waiting", "walking",
                "woman", "yellow", "zebra"};
     
            // Create a new Scanner Object called scan
            Scanner scan = new Scanner(System.in);
     
            // Print out a request and display to the User
            // & Read In User Input using the Scanner Object
            System.out.println("Enter a common word: ");
            scan.nextLine();

    Right... now what I am trying to do is..... when the user enters a random common word, the program searches the String Array and prints out either ''matching word'' or ''non matching word''.

    So how do i go about getting the program to search the words in the string array.

    I think an if statement is used no ?

    i.e. if ('word entered by user' = 'one of the values in the string array')

    then .....


    Something like that ? But im not really sure.

    Thanks for your help.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Arrays []

    To determine if an item is in an array you will need to look at each element in the array, usually done in a loop.
    You should look at using one of the collection classes to hold the words. Many have a contains() method that will say if an item is in the container. The HashSet class might work

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String Arrays []

    String input = new Scanner(System.in).next(); 
    boolean contains = false;
    for(String w : words){
    if(input.equalsIgnoreCase(w)){
    contains = true;
    break;
    }
    }
    if(contains){
    System.out.println(String.format("Array contains %s", input));
    }else{
    System.out.println(String.format("Array does not contain %s", input));
    }
    If you need to know what index that word is at, just use a normal for loop.

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: String Arrays []

    If what you're interested in is an exact match, you can sort your array with Arrays.sort and search it with Arrays.binarySearch. Check the API docs for java.util.Arrays.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String Arrays []

    Quote Originally Posted by Norm View Post
    To determine if an item is in an array you will need to look at each element in the array, usually done in a loop.
    You should look at using one of the collection classes to hold the words. Many have a contains() method that will say if an item is in the container. The HashSet class might work
    Hi.

    This sounds like the best method.

    So i need to do a if (the string array contains 'the word the user entered')

    then ....... System.out.print("found matching word");

    else ... ("no match found");

    But how do i actually implement this?

    I am really stuck. Please help.

    regards.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: String Arrays []

    Which technique do you want to use? post#3 shows one way.

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String Arrays []

    Oh Right apologies. I quickly skim read the replies. Didnt realise Post 3 posted code.

    I will now try this.....

  8. #8
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String Arrays []

    Yep that works fine. Just what I wanted.

    thanks alot.

Similar Threads

  1. Replies: 7
    Last Post: December 11th, 2011, 11:58 PM
  2. How to change a String value into a number and then back into a String.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 18th, 2011, 01:43 PM
  3. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  4. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  5. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM