CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Help

  1. #1
    Join Date
    Feb 2012
    Posts
    8

    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

  2. #2
    Join Date
    Feb 2012
    Posts
    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!

  3. #3
    Join Date
    Feb 2012
    Posts
    8

    Re: Help

    Ok thanks will have a go and post what I come up with

  4. #4
    Join Date
    Feb 2012
    Posts
    8

    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.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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 =).
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Jan 2009
    Posts
    596

    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
  •  





Click Here to Expand Forum to Full Width

Featured