Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

test for valid characters  RSS feed

 
Dianne Gerrelli
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to setup a test for valid characters but what ever I try seems to have problems.

I have a method called valid () which takes validLetters from another method.

public void validate (){

String content = getContent(); //test sequence
System.out.println(content+ "content passed to validate");

if (content.startsWith("A")) //very basic string test
System.out.println("This may be a fasta file");
else
System.out.println("Not a fasta file");

char[] theseletters = this.validLetters(); //copy array
for (int i = 0; i < theseletters.length; i++) //walk through array
System.out.println(theseletters);
}


The valid letters are stored in an array of characters.

public char[] validLetters (){
char[] letters = {'A', 'C', 'D', 'E', 'F', 'G', 'H','I', 'K',
'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y'};
return letters;}

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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this might be all you need to do

 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Does this help?

Layne
[ January 23, 2006: Message edited by: Layne Lund ]
 
Dianne Gerrelli
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!