• Post Reply Bookmark Topic Watch Topic
  • New Topic

comparing characters  RSS feed

 
Dianne Gerrelli
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone please explain why the following code prints the characters to be tested and the index but fails to actually perform the test that I need. I want to have something that compares characters to look for valid characters. I have a test characters, which is a String and valid characters which is also a String. I then want to check each of the test characters to see if they match with the valid characters and if not valid report the error by printing the character that is not valid and the index it is in the String.

I have tried very hard to get this to work with the valid characters stored in an array but have not been able to get it to work as the complier will not allow me to setup a test between variables that do not match ie character in a string and character in an array.



Best wishes
Dianne Gerrelli
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I looked at your validation method and I couldn't find any flaw its logic. I tested it out on my own:


and got exactly the output I was expecting:

Therefore if it is not working for you my only suggestion is that you check to make sure that the content variable and the theseLetters variable contain exactly what you think they contain. I would do this by inserting System.out.println(...) statements just before the body of the loop, just as a sanity check.

Garrett
 
Dianne Gerrelli
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Garrett,

Thanks for the reply. I have done as you suggested and inserted the characters for the Strings and retested. It works now and only prints out when the string contains a valid letter. I think because I have the print statement in a loop it also prints this message 4 times, one for each character compared from theseletters string so I will need to fix this by throwing and catching exceptions instead of the print statements. You may therefore get a post concerning exceptions as I do not really understand how these work. Do you know of any good explanations?

I will check the returns from my methods getContent() and validLetters() as it looks as though the problem may be with them not returning the correct data.

Best wishes
Dianne Gerrelli



---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.5.0\bin\java.exe" Sequence
agtaacxatgc
acgt
Invalid character[x] at position 7
agtaacxatgc
acgt
Invalid character[x] at position 7
agtaacxatgc
acgt
Invalid character[x] at position 7
agtaacxatgc
acgt
Invalid character[x] at position 7

> Terminated with exit code 0.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you need to get into exception handling for this situation. When I tested the method just as you have written it, the invalid character message only printed out once for me. I'm having a hard time understanding why its printing out 4 times for you. Also what I was trying to suggest was this:

The System.out.println(...) messages are inserted strictly for debugging purposes. Once you are sure the method does what you want, remove them or comment them out. One more thought would be instead of printing error messages, the validate method could return a boolean indicating whether the test passed or failed. Or if you want to know which positions contained invalid characters maybe you could return an int[] containing the indices of all invalid characters. If the test passed you could return a zero-length int[] (instead of just returning null). Obviously ultimately these design decisions are up to you. Just giving my 2cents.

Garrett
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Or perhaps these design decisions are up to a college professor?

Layne
 
Dianne Gerrelli
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks, I am afraid that the design decisions are indeed up to the course organisers.

Best wishes
Dianne
 
Phil Campell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I hate to bring back a dead thread, but I'm new to the Java (and programming, in general) field and this is the closest thread I could find relating to my issue.

I'm making a program that increases the count of an index in an array according to letters. Here's what I have so far (I'll post the code and then explain it below):

So, String 'important' is basically a string of characters that are "A, C, G, T" not case sensitive. Currently, int index = "acgt".indexOf(important.charAt(i)); works great, if string important is a string of previously mentioned characters in lowercase. Anywhere I add the string method .equalsIgnoreCase() to the code: int index = "acgt".indexOf(important.charAt(i)); I get a "cannot find symbol" pointing to the .equalsignoreCase... I've tried to make a (before the for loop) a new string - String nucleotide = input.nextLine(); String important = equalsIgnoreCase(nucleotide); which I thought would take that whole string (of characters a, g,c,t) and make it not matter if it CAPITOL letters or not.

Right now, if String important is lower case, it will make a new array with index 0 equal to all lowercase a's in the line, index 1 equal to all lowercase c's, etc. String important, however, can be all capitols, all lowercase or mixed. Any advice?

Please help!! And thanks in advance!
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to treat the "important" String as if it were all lower-case? Then just change it to all lower-case right after you read it in. Since your code doesn't propose to compare that whole string to anything else, then there isn't any point in using the equalsIgnoreCase() method in any case.
 
Phil Campell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not that I want to read it all lowercase, I need to be able to read it regardless of whether it's lowercase, capital or mixed. If it were just reading the string as one or the other, all I would have to do is change int index = "ACGT".indexOf(nucleotides.charAt(i)); from ACGT to int index = "acgt".indexOf(nucleotides.charAt(i));. And i can't make 'index'.equalIgnorCase, because it's a string object and wont be compatible with type int.

so, I need a way to make int index = "ACGT".indexOf(nucleotides.charAt(i)); equalIgnoreCase() somehow.


 
Phil Campell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the double post, but I forgot to include something to make it a little more clear:

It's not that I want to read it all lowercase, I need to be able to read it regardless of whether it's lowercase, capital or mixed. If it were just reading the string as one or the other, all I would have to do is change int index = "ACGT".indexOf(nucleotides.charAt(i)); from ACGT to int index = "acgt".indexOf(nucleotides.charAt(i));. And i can't make 'index'.equalIgnorCase, because it's a string object and wont be compatible with type int.

so, I need a way to make int index = "ACGT".indexOf(nucleotides.charAt(i)); equalIgnoreCase() somehow.
The input file looks something like this:

cure for cancer protein
ATGCCACTATGGTAG

So if int index = "ACGT".indexOf(nucleotides.charAt(i)); I will get a new array of [4.0, 3.0, 4.0, 4.0]. But lets say the input file is:

cure for cancer protein
atgccactatggtag

the array will be [0.0, 0.0, 0.0, 0.0] because they are all lowercase and int index is capital. If i change "ACGT" to "acgt", then this file will work, but the previous (with all the capitals) will be a [0.0, 0.0, 0.0, 0.0].

The file could look like either of those or

cure for cancer protein
aTgCCaCtaTgGtag.
 
Phil Campell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to apologize again for bringing back a dead thread. I also want to thank you for your help, but I ended up figuring it out. Instead of trying to convert the entire string to "equalsIgnoreCase()" I simple kept String important (because I need to keep it un-touched), made a new string of important.toLowerCase(). That way, it doesn't matter what the original is, because I'm reading the copy of it that is all of one type.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey Geller wrote:It's not that I want to read it all lowercase, I need to be able to read it regardless of whether it's lowercase, capital or mixed.


Yes, I know. But I didn't tell you to "read it all lowercase", I told you to change it to all lowercase after you read it. I understood perfectly well what you were asking.
 
Phil Campell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I misunderstood your post then. You asked "You want to treat the "important" String as if it were all lower-case?" And by the question mark I thought you were asking me if thats what I wanted to do. But I didn't want to treat it as lowercase. In my head, I was still trying to get it to equalsIgnoreCase(). When you told me to convert it to all lowercase, I honestly don't know what I was thinking but didn't know what you were talking about (like I said - new to the whole programming scene). I then went through the book I have and it listed the methods for string objects and two were "toUpperCase()/toLowerCase()" and then it hit me to convert it. So you did have the right idea but I was stubborn and didn't understand/didn't listen. Sorry and thank you!
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I know. When you have a good idea and you're running for the goal line with it (or insert your own sports metaphor here), it's pretty difficult to listen to the guy on the sidelines who is yelling at you that you're going the wrong way. Just human nature, really.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of converting the entire String to uppercase/lowercase, you can convert the characters themselves as well, using Character.toLowerCase and Character.toUpperCase. These methods are static so no need to create a Character instance either.
 
Phil Campell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Figured it out
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!