This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Searching an array of strings, eliminating ones that don't have the chars searched for. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching an array of strings, eliminating ones that don Watch "Searching an array of strings, eliminating ones that don New topic
Author

Searching an array of strings, eliminating ones that don't have the chars searched for.

Rob Rowan
Greenhorn

Joined: Nov 14, 2012
Posts: 11
Hello everyone, this is my first post here! Eager to perhaps help some people out as well as get help myself.
I'm confused with this problem. I have a program that generates 8 random chars from two arrays, one being an array of vowels the other being an array of consonants (exactly like countdown)
It then saves the generated chars in a separate array. I was wondering if it was possible to use these chars to search through an array of strings and remove any string that didn't contain some if not all chars.
If it's confusing let me know and I'll try and elaborate.

So far I have this;

I have created the part of the program that generates the chars, that part works perfectly and the chars can be printed or handled in any other way.
This is the part where I am converting a dictionary into an array and printing it. This was just testing it, I don't need to print it.


So if my coding is right. That saves each line as it comes in an array, position equal to the integer count with increases every time the while != null runs.
The thing I am having major issues with is searching the array of words for things that match the chars.
Like countdown.

Thank you if anyone can help, if not, thanks anyway!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Rob Rowan wrote: . . . (exactly like countdown) . . .
And you want us to help with your claim it is cruel and unnatural punishment under the Geneva Convention. Of course!

No, it is a programming thing. Welcome to the Ranch

I would suggest you should get that code out of the main method, but I suspect you know that already and simply had it there until it works. You would appear to have programmed in C before, with so much in the main method, and using _ in variable names. We use different conventions in Java.
I am not quite sure what the problem is. Are you having difficulty reading into an array? Just having a quick look at your code, I can see a few other things.
  • 1: Do you ever use the Arrays class? I don’t think you need that import.
  • 2: You don’t need the two Strings line and Dictionary. To get rid of them, you will probably have to learn some strange‑looking syntax:-
  • I would use a List myself, not an array, but you may not have come across Lists yet. How do you know the array is the right size? If it is too small you will get an out of bounds exception when you get to the 90001st line. If it is too large, you may suffer a null exception when you get to the last array element, which will be null. Suggestion:-
  • 1: Create reader
  • 2: Read every line in the file, doing nothing with it.
  • 3: Close reader.
  • 4: Create new reader.
  • 5: Read each line into the array
  • 6: Close second reader.
  • You are doing nothing with the lines from the first reader, but I omitted to write that you can count them. Then you will know what size the array will be.

    As for working out whether a word matches the letters, that can be awkward. Can you work out how to get arrays of chars out of a String? Do you need to sort those arrays? Can you work out how to tell that one array represent a subset of the other? You cannot do that on screen until after you have written down on paper how you intend to do it. Good luck, and do tell us how you are getting on with it.
    Rob Rowan
    Greenhorn

    Joined: Nov 14, 2012
    Posts: 11
    Thanks a lot for your reply! My problem is I actually have no idea HOW to do this. I'm just learning Java and I was wondering if there was some sort of function that allowed me to search a string for a certain character. Basically what I want to do is find the word in the text file that contains the most amount of chars out of the 8. If anyone could help me with a very BASIC method of how to do it, I could work out how to implement it myself.

    Also about your two questions.
    1) I was messing around with an Array list function and it imported that by itself.
    2) Thanks a lot! That's much more neat.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Rob Rowan wrote:Thanks a lot for your reply! My problem is I actually have no idea HOW to do this. I'm just learning Java and I was wondering if there was some sort of function that allowed me to search a string for a certain character


    I would start by writing down, in very precise, simple steps, in English, how you would do it "manually". Then look at the methods in java.lang.String and see which ones correspond to your steps in English.
    Rob Rowan
    Greenhorn

    Joined: Nov 14, 2012
    Posts: 11
    Okay, thanks very much! I'll let you know how I get on.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    You might consider a technique which puts the letters into alphabetical order, so "Campbell" becomes 'a' 'b' 'c' 'e' 'l' 'l' 'm' 'p'. Note I have also changed 'C' to 'c'. If you also have the array you are searching in alphabetical order, it might be easier to compare the two.
    If you can sort an int[], then you can sort a char[] by exactly the same technique, because chars are not letters; they are numbers. But all capital letters come before all small letters, so you want to get them all into the same case. You can find the exact values (in hexadecimal) here.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Searching an array of strings, eliminating ones that don't have the chars searched for.
     
    Similar Threads
    File
    no such element? eek!
    # of lines in a file?
    2 problems.. getting correct file from filechooser to work..turning array into jlabel
    plz delete