[英]How to individually count the occurrences of two chars in a String within a single method?
我的程序正在读取的字符串示例
巴巴啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
我目前有两个 int arrays int[] countA = new int[4]
和int[] countB = new int[4]
其中每个数组中的四个元素对应一个特定维度(Alpha、Bravo、Charlie、Delta)。 我试图将上面的字符串分成 7 个组,其中第一个字符对应于 Alpha,接下来的两个字符对应于 Bravo,接下来的两个对应于 Charlie,最后两个对应于 Delta。 如果字符是 A 或 B,则更新相应维度的数字
(EX: countB[2]++
,当在第 9、10、16 等字符处检测到 B 时)
目前我的程序有两种单独的方法来计算这两个字符的出现次数。 我觉得计算出现次数是一种多余的方法,因为这两种方法中的代码几乎相同。 我正在尝试通过将我的两种计数方法浓缩在一个方法中来进一步优化我的程序。
当前程序代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.Arrays;
//CSC 142 , Check personality, personalityTest.java
// June 12, 2015, Chloe Wake
// Reads through a file of results from the Keirsey Personality test and outputs the answers in a output file.
public class CountDimensions {
public static final int DIMENSIONS = 4;//the number of personality dimensions
public static void main(String[] args) throws FileNotFoundException{
//Scanner console = new Scanner(System.in);
userIntro(); //give an intro to the user
processFile(); //read in the file and store it so we can access it
}
//method that reads in a file and stores each line in an array
public static void processFile() throws FileNotFoundException {
Scanner console = new Scanner(System.in);//create scanner for user input
File fileName = inputFile(console); //store the file the user input typed in
Scanner read1 = new Scanner(fileName);//create scanner for the .txt file specified by user
Scanner inputFile = new Scanner(fileName); //make a new scanner to read the file now that we know it exists
PrintStream output = new PrintStream(outputFile(console));//create printStream object for output
// while loop to get the desired output
while (inputFile.hasNextLine()) { // while the file has text in the line read through it
String name = inputFile.nextLine(); // first line is the name of the person who took the test, go to the next line
String answers = inputFile.nextLine().toUpperCase(); // line after the name is the
int[] findA = countA(answers);
int[] findB = countB(answers);
int[] percentage = percentage(findB);
String[] type = type(percentage);
outputFile(name, percentage, type, findA, findB);
}
}
public static File inputFile(Scanner console) {//ask for file
System.out.print("Type the filename of the file you want to input: ");
File answer = new File(console.nextLine());
while (!answer.exists()) { //if it doesn't exist, throw an error
System.out.println("That file does not exist! Try again.");
answer = new File(console.nextLine()); //store the file the user input typed in
}
return answer;
}
//method that writes output back into a text file
public static File outputFile(Scanner console) {
System.out.print("output file name? ");// PrintStream to write to a user specified output file
File output = new File (console.nextLine());
return output;
}
//method to count how many B's the person answered and stores them in an array
public static int[] countB(String answers) { // takes the information from the user input that was stored in the answers variable
int[] countB = new int[DIMENSIONS]; // method declares the array of 4, searching through 4 at a time
for (int i = 0; i < answers.length(); i++) { // for every time i is less than the length of the answer continue on
char t = answers.charAt(i); // declare a char and make it set to the character at place i in the answers
if (t == 'B') { // if at that spot you find a B continue
if (i % 7 == 0) { // found a B is the spot it is at evenly divisible by 7 aka in the 7th spot?
countB[0]++; // ok then at it to the array in the first spot
}
if (i % 7 == 1 || i % 7 == 2) { //is the B found in
countB[1]++;// ok then at it to the array in the second spot
}
if (i % 7 == 3 || i % 7 ==4) {
countB[2]++;// ok then at it to the array in the third spot
}
if (i % 7 == 5 || i % 7 == 6) {
countB[3]++; // ok then at it to the array in the fourth spot
}
}
}
return countB;
}
// method counts the A's in each dimension and return the total as an array
public static int[] countA(String answers) {
int[] countA = new int[DIMENSIONS];
for (int i = 0; i < answers.length(); i++) {
char t = answers.charAt(i);
if (t == 'A') {
if (i % 7 == 0) {
countA[0]++;
}
if (i % 7 == 1 || i % 7 == 2) {
countA[1]++;
}
if (i % 7 == 3 || i % 7 ==4) {
countA[2]++;
}
if (i % 7 == 5 || i % 7 == 6) {
countA[3]++;
}
}
}
return countA;
}
要使用相似的代码,您可以尝试区分不同的部分并将它们用作 arguments。 在您的情况下, arrays countA
和countB
是相似的,并且唯一的文字'A'
和'B'
发生了变化。 这个原则被称为“封装变化的东西” 。
所以新方法可能如下所示:
public static int[] countA(String answers, char charToFind) {
int[] count = new int[DIMENSIONS];
for (int i = 0; i < answers.length(); i++) {
char t = answers.charAt(i);
if (t == charToFind) {
if (i % 7 == 0) {
count[0]++;
}
if (i % 7 == 1 || i % 7 == 2) {
count[1]++;
}
if (i % 7 == 3 || i % 7 == 4) {
count[2]++;
}
if (i % 7 == 5 || i % 7 == 6) {
count[3]++;
}
}
}
return count;
}
电话将分别是
int[] findA = count(answers, 'A');
int[] findB = count(answers, 'B');
这也允许您使用任何其他字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.