好的,我需要输入文件(A,B,C,D)中的字符列表作为唯一选项。 程序还应该从文件中读取2到10个字符之间的序列,同时使用(A,B,C,D)作为唯一允许的字符。 然后,程序应检查输入列表中序列的出现次数,并将输出存储到文件中,其中包含序列计数“markers”的整数列表。
Example:
sequence = [ACB] <=== read from file
input = [ABDCBBACBCBACBDABCACBDACCBCBC] <==== read from file
output =[.........1....2......3.......] ====> write to file
如果我能得到一些帮助,到目前为止我所拥有的是:)
package Java;
import static Java.CountEachLetter.countLetters;
import java.io.File;
import java.util.Scanner;
public class EchoWords {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(new File("text.txt"));
//while (input.hasNext()) {
System.out.print("Enter a string: ");
String s = input.nextLine();
int[] count = countLetters(s.toLowerCase());
System.out.println(s);
for (int i = 0; i < count.length; i++) {
if (count[i] != 0)
System.out.println((char)('A' + i) + " appears " +
count[i] + ((count[i] == 1) ? " time" : " times"));
}
}
public static int[] countLetters(String s) {
int[] counts = new int[26];
for (int i = 0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i)))
counts[s.charAt(i) - 'a']++;
}
return counts;
}
}