Results 1 to 5 of 5
- 09-25-2013, 10:26 PM #1
Member
- Join Date
- Sep 2013
- Posts
- 2
- Rep Power
- 0
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(); } }
- 09-25-2013, 10:56 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
Re: Sequences (convert char[] to String) [Replacing String with Character]
- 09-26-2013, 10:47 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 09-26-2013, 10:56 AM #4
Member
- Join Date
- Sep 2013
- Posts
- 2
- Rep Power
- 0
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'
- 09-26-2013, 12:35 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Similar Threads
-
Convert Character to String
By andy9507 in forum New To JavaReplies: 3Last Post: 05-21-2011, 04:29 PM -
replacing last char of string logics
By mukeshgulia in forum New To JavaReplies: 3Last Post: 03-25-2011, 10:30 AM -
Convert Char To String
By fh84 in forum New To JavaReplies: 15Last Post: 10-28-2009, 10:59 PM -
Replacing char in string help
By jimmy-lin in forum New To JavaReplies: 3Last Post: 10-12-2009, 07:01 AM -
Cannot convert from char to String error
By sondratheloser in forum New To JavaReplies: 1Last Post: 12-13-2007, 10:28 PM
Bookmarks