Results 1 to 5 of 5
  1. #1
    tpolwort is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Sequences (convert char[] to String) [Replacing String with Character]

    I am quite a beginner at programming.. was given an assignment and don't understand how to complete the rest of it. I have tried for hours and hours and can't figure it out. I would greatly appreciate the assistance as to I have no clue what I am doing.

    Write a program in Java that stores the following sequence that is comprised of only 'A','C','G', & 'T' characters: [ATGACATCTCTCCATACG]. The program should store the sequence both as a String and as a char[].

    The program should accept two inputs from the user: -A single character with the only allowable values are either 'A','C','G', or 'T', and -An integer value that is greater than 0 and smaller than the length of the sequence.

    Your program should then produce the following as output: -It prints a total count of the occurrences of the single character provided as input.

    -It identifies the locations where the character entered by the user appears in the sequence at a location that is greater than or equal to the integer value provided as input by the user

    -It generates a new list with the character entered by the user replaced by an asterisk ['*'] according to the previous requirement.

    -Your program should provide two sets of methods to implement this functionality: one set dealing with the sequence as a char[] and the other set of methods dealing with the sentence as an instance of String.

    -Your program should do the necessary checking on the values provided for the character and integer inputs to prevent the values provided by the user from getting your program to malfunction.

    -You should provide as output test cases of your program for a user input corresponding to each of the four letters, naely: 'A','C','G', & 'T', and for two integer values: 0 and 4 - for a total of eight test cases.

    Java Code:
    package Java;
     
    import static Java.CountEachLetter.countLetters;
    import java.util.regex.*;
    import java.util.*;
    import java.util.Scanner;
     
    public class Sequence {
     
    public static void main(String[] args) {
     
    Scanner input = new Scanner(System.in);
     
    // Prompt the user to enter a string
    System.out.print("Enter a string: ");
    String s = input.nextLine();
     
    // Counts each letter to Lower Case
    int[] counts = countLetters(s.toLowerCase());
     
    // Display results
    for (int i = 0; i < counts.length; i++) {
    if (counts[i] != 0)
    System.out.println((char)('A' + i) + " appears " +
    counts[i] + ((counts[i] == 1) ? " time" : " times"));
    }
    // String result = input.replace("*", "asterisk");
     
    //matching
    String seq = input.nextLine();
    Pattern p = Pattern.compile(""); //"C*A*" //"C*A+"
    Matcher m = p.matcher(seq);
    // boolean res = m.matches();
    // System.out.println("matching CCC " + res);
    System.out.println(m.replaceAll("*"));
     
     
    //replace all ACC with _?
    seq = "CGTATCCCACAGCACCAACATTTTTTCCAACAACCCCA"…
    p = Pattern.compile("AC+A");
    m = p.matcher(seq);
    // System.out.println("replacing all AC+C with _");
    //System.out.println(m.replaceAll("*"));
    System.out.println();
     
    }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Sequences (convert char[] to String) [Replacing String with Character]


  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Sequences (convert char[] to String) [Replacing String with Character]

    What problems are you having?

    Start from the beginning...does the counting work?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    tpolwort is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Sequences (convert char[] to String) [Replacing String with Character]

    It counts whatever String you type in but I need to make it so it only allows it to read 'A', 'C', 'G', and 'T'

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Sequences (convert char[] to String) [Replacing String with Character]

    OK, so what do you think you need to do to get it to do that?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Convert Character to String
    By andy9507 in forum New To Java
    Replies: 3
    Last Post: 05-21-2011, 03:29 PM
  2. replacing last char of string logics
    By mukeshgulia in forum New To Java
    Replies: 3
    Last Post: 03-25-2011, 09:30 AM
  3. Convert Char To String
    By fh84 in forum New To Java
    Replies: 15
    Last Post: 10-28-2009, 09:59 PM
  4. Replacing char in string help
    By jimmy-lin in forum New To Java
    Replies: 3
    Last Post: 10-12-2009, 06:01 AM
  5. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 09:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •