• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay, thanks very much! I'll let you know how I get on.
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Won't you be my neighbor? - Fred Rogers. tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic