I need to setup a test to see if the test sequence 'content' contains valid letters and no other characters. I thought I could do this by using one of the methods in the Java library but nothing seems to be exactly what I need. The public int compareTo(String anotherString) will only seem to work if there is an exact match and the letters are in the same order. So is good for finding words but not for what I need.
I then thought I could use pattern and matcher and set up my valid characters as the pattern but I need to say something like any of these characters but no others. This I could do if I was putting the letters into an expression by using "A"|"C"|"D", but I do not know the correct syntax for this or how to extract the characters from the array.
The other way is to set up a loop to check each character in the test sequence with those in the array but this seems very long winded.
I would imagine there is a much more simple way of doing this but up to know I have not found it. Has anyone got any tips?
Best wishes Dianne Gerrelli
Joined: Jun 09, 2003
this might be all you need to do
Joined: Dec 06, 2001
With a little bit of work, you could create a regular expression to do this. At this point, that might be more work than it is worth. Let's create a new method called isValid() with the following header:
You might even want to make this a private method in your class, but at the moment, let's step back from the Java syntax and write out the steps to solve this problem in English. Let me help you get started:
Notice how I am using concepts (and even keywords) from Java, but I am not adhering to the strict syntax. This allows a little bit of freedom so you don't have to worry about those kinds of details. Instead, you can concentrate on the solution to the problem instead.
Also, using English gives me the freedom to say things like "if c is not valid". At this level, I don't worry about the exact details of doing this. That's the next step. So can you describe in words how you would check if a char is valid? Hopefully you can use the pseudocode above as an example.
I can see the logic of dealing with one character at a time. I am afraid I was studying the wood so much that I did not see the trees. I tried to change things this morning but did not have enough time before I had to go to work. However I think I have two alternatives either deal with the programme as it is or try to alter the variables that I am using. The problem I have been set does have some constrants in how to store and access data.
I have been told to store the valid characters in an array. When I read the text again (always a good tip) it says store the valid characters initially in an array. So I could
1. Find a method to get characters out of an array one at a time and store in a local variable
2. Use an iterator method to search through an array and get one character at a time and again copy into a local variable
3. copy the contents of the array into a local string variable and then traverse the string one character at a time.
I think 2 may be the most preferable method as I believe iterators are designed to search and get elements from collections.
Later in the programme I have a slightly different problem as I am asked to get the test sequence from a file. I have used BufferedReader for this and then copied one line at a time into a string. So for this I could either look for a method which gets a character rather than a line or alternatively just test the string by getting one character at a time. This would mean writting the same type of method as 3 above which may save me some time.
At present I am just at the stage of beginning to understand how Java works so I tend to try things and see if I can successfully code it and understand how the whole process works. This may not necessarily be the best way of working as sometimes I end up going down having to rewrite things as I can get so far and no further. I have not been given any guidance as to best practice so would be grateful for any tips on manipulating text/characters and whether anyway is better than another.
Best wishes Dianne Gerrelli
Joined: Dec 06, 2001
I think you have the right idea to check one character at a time from your character array. However, you are making this a little more complicated than you need to. Java provides simple syntax to access individual elements in an array without using iterators or additional local variables. Let me illustrate with an example:
The idea here is that we use the syntax "array[x]" to access the element at index x. The index value starts at 0. My examples above all use constants for the index, but you are allowed to use an int variable if you want.
Let me know if this helps. Hopefully you can make some progress from here. If you need more details about arrays, I suggest that you reread that section from your text book.