Мне нужно взять несколько строк текста и подсчитать вхождения каждой буквы в каждой строке. Последняя строка должна заканчиваться через определенный период. Меня попросят создать массив int длиной 26, где arrayName[0] = number of a's
, arrayName[1] = number of b's
и т. Д., И этот случай письма должен быть проигнорирован. У меня возникают проблемы с проверкой появления каждой буквы и определением индексированных переменных с правильным количеством вхождений. Мой код до сих пор:java - Ввод числа букв в строках в массив int из 26
import java.util.Scanner;
public class LetterCounter
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String[] textBlock = new String[10];
System.out.println("Enter text.");
int index;
//Allows me to type in something for all "lines" of text until I end one with a period.
for(index = 0; index < textBlock.length; index++)
{
textBlock[index] = input.nextLine();
if(textBlock[index].contains("."))
{
System.out.println("Period found");
break;
}
}
//Outputs what the user printed except for the lines not typed in (they return null).
System.out.println("Text input by user:");
for(index = 0; index < textBlock.length; index++)
{
if(textBlock[index] != null)
{
System.out.println(textBlock[index]);
}
}
//int array
int[] occurrences = new int[26];
//used to turn letter into number
char letter = 'a' - 49;
char letter2 = 'b' - 49;
char letter3 = 'c' - 49;
//checks that numbers are correct (return "0" for a, "1" for b, and "2" for c)
System.out.println("Number value of a: " + letter);
System.out.println("Number value of b: " + letter2);
System.out.println("Number value of c: " + letter3);
}// End of main
}//End of program
Для начала, я думаю, что вы хотите принять декларацию из массив вне вашего цикла. Вероятно, вы хотите заставить каждую букву читать нижний регистр. Затем, проверив букву в «a» на «z», ++ массив в индексе прочитанного письма - «a». –
Аналогичный вопрос здесь: http://stackoverflow.com/questions/33770180/how-do -i-магазин-это-печатные буквы, в-ан-массив/33770240 # 33770240 – markspace