-
February 21st, 2012, 05:46 PM
#1
Help
Can someone please help me get started with this problem?
Human and all other genomes are made up of sequences of millions of chemicals, represented by
the four letters c, g, a and t. Write a Java program that accepts strings from this language
and which rejects all other strings. Thus, your program will validate ensure that strings are
indeed valid.
accccgtaggacatc (accepted)
ttcgaaxattctcca (rejected)
Thanks
-
February 21st, 2012, 06:16 PM
#2
Re: Help
Without giving away too much, try looking at the Java API documentation for java.lang.String. This is actually an easy problem to solve using String's charAt() method.
Good luck!
-
February 22nd, 2012, 03:19 PM
#3
Re: Help
Ok thanks will have a go and post what I come up with
-
February 22nd, 2012, 03:50 PM
#4
Re: Help
I think I'm almost done, can't get it to print "Invalid" though. Any ideas? Thanks
import java.util.Scanner;
public class S1 {
public static void main(String args[]) {
String inString;
boolean validity = true;
Scanner input=new Scanner(System.in);
System.out.print("Please enter a string: ");
inString=input.next();
{
int i=0;
while (i < inString.length()) {
if (inString.charAt(i) == 'g' && inString.charAt(i) == 't' && inString.charAt(i) == 'c' && inString.charAt(i) == 'a')
{
validity = true;
break;
}
i++;
}
}
if(validity = true)
System.out.println("Valid");
else
System.out.println("Invalid");
}
}
Last edited by freakinawesome; February 22nd, 2012 at 04:03 PM.
-
February 22nd, 2012, 04:28 PM
#5
Re: Help
Did you read the "Before you post" link AlbertGM supplied in your other thread. If not why not and if you did why have you not posted the code using code tags?
Code:
if(validity = true)
You should never test a boolean value for true or false for a number of reasons.
- It's pointless as the value is already true of false so you can just use it. If you want to test for not true then use the NOT operator '!'.
- It's ugly and makes the code harder to read.
- It adds another place where typos can occur and you can end up assigning a value rather than testing it as you have done (Testing for equality is done with == and not =).
-
February 22nd, 2012, 06:36 PM
#6
Re: Help
Code:
if (inString.charAt(i) == 'g' && inString.charAt(i) == 't' && inString.charAt(i) == 'c' && inString.charAt(i) == 'a')
Have a close look at this line - it's not doing what you think it is.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|