Loop wont stop, even with limit set (count++) - loops

I just cannot seem to get this to stop looping after the count (5) is reached. IT just keeps asking away. IS there something I'm not seeing?
Any help is greatly appreciated.
Scanner keyboard = new Scanner(System.in);
int count = 0;
double answer = 0;
int randomInt1;
int randomInt2;
randomInt1 = (int)(1 + Math.random() * 10 - 1 +1);
randomInt2 = (int)(1 + Math.random() * 10 - 1 +1);
//System.out.println("Please answer the following problem: ");
//System.out.println(randomInt1 + "+" + randomInt2 + "=");
//answer = keyboard.nextDouble();
count=1;
while(count < 5)
{
System.out.println("Please answer the following problem: ");
System.out.println(randomInt1 + "+" + randomInt2 + "=");
answer = keyboard.nextDouble();
if(answer != (randomInt1 + randomInt2))
{
System.out.println("Sorry, that is not correct.");
count++;
break;
}
if(answer == (randomInt1 + randomInt2))
{
System.out.println("Nice!");
count++;
break;
}
}
return answer;
}
}
****UPDATED
count=0;
while(count < 5)
{
System.out.println("Please answer the following problem: ");
System.out.println(randomInt1 + "+" + randomInt2 + "=");
answer = keyboard.nextDouble();
if(answer != (randomInt1 + randomInt2))
{
System.out.println("Sorry, that is not correct.");
}
else if(answer == (randomInt1 + randomInt2))
{
System.out.println("Nice!");
}
count++;
break;
}
return answer;

Avoid using break on your code. Use else instead of the second if statement. Also, you should better indent your code. It helps reading.
If you want to count 5 times, you should do:
count = 0;
while(count < 5) {
...(your code here)
}

Why don't you increase count outside the if condition.
count = 0;
while(count < 5) {
if(condition1){
// ...
}
else{
// ...
}
count++;
}
Update:
Below is the complete code, which may be like what you want.
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int count = 0;
double answer = 0;
int randomInt1;
int randomInt2;
randomInt1 = (int) (1 + Math.random() * 10 - 1 + 1);
randomInt2 = (int) (1 + Math.random() * 10 - 1 + 1);
while (count < 5) {
System.out.println("Please answer the following problem: ");
System.out.println(randomInt1 + "+" + randomInt2 + "=");
answer = keyboard.nextDouble();
if (answer != (randomInt1 + randomInt2)) {
System.out.println("Sorry, that is not correct.");
} else if (answer == (randomInt1 + randomInt2)) {
System.out.println("Nice!");
break;
}
count++;
}
}
}

Related

making my CS50 solution more elegant and efficient

I am taking CS50, and I completed the first week problem of implementing the Luhn Check algorithm. Although my solution works, I feel it's very sloppy and not efficient. Can someone please suggest how I can improve my solution. (It's a first week problem; you are not intended to use arrays or functions.)
Here is the code:
#include<stdio.h>
#include<stdlib.h>
#include<cs50.h>
int main()
{
long long x,j=0;
int len,i,len_flag,flag=0,flag_val=0,sp_len=0,check_flag=0; //basic integers related to loop etc
int res,sum,fin_sum=0,h=0,d2_sum=0,d1_sum=0,ff_num=0; // variables related to extracting numbers
int sum_len=0,in_sum=0,in_fsum=0,len_sum=0,m=0; // extraing numbers from more than single sum result
int sum_f=0,sum_final=0;
do{
x = get_long("enter a valid card number \n");
j=x;
len_flag = 0;
len = 0;
while(len_flag!=1)
{
x = x / 10;
if(x==0)
{
len_flag = 1; //finding the lenght of the number
}
len++;
}
if(len==15||len==16||len==13)
{
flag = 1;
sp_len =1;
}
else
{
flag=1;
}
}while(flag!=1);
for(i=0;i<len;i++)
{
res = j % 10;
j = j / 10;
if(i%2!=0)
{
sum = res * 2;
//printf("multi_res : %d \n",sum);
len_flag = 0;
sum_len = 0;
len_sum = sum;
while(len_flag!=1)
{
len_sum = len_sum / 10;
if(len_sum==0)
{
len_flag=1; // checking if the sum is more than single digit
}
sum_len++;
//printf("trig\n");
}
if(sum_len>1)
{ x=0;
while(x<sum_len)
{
in_sum = sum % 10;
sum = sum/10;
in_fsum = in_fsum + in_sum;
//printf("double sum : %d \n",in_fsum);
x++;
}
}
fin_sum = fin_sum + sum;
}
if(i==len-1)
{
for(h=0;h < 1;h++)
{
fin_sum = fin_sum + in_fsum;
}
d1_sum = res;
}
else if(i%2==0)
{
sum_f = sum_f + res; // adding up the number that where not x2
}
if(i==len-2)
{
d2_sum = res; //5555555555554444
}
}
sum_final = sum_f + fin_sum;
//printf("sum_final : %d\n",sum_final);
//printf("sum_f_final : %d\n",sum_f);
//printf("sum_in_final : %d\n",fin_sum);
if(sum_final%10==0)
{
flag_val=1; // checking if the number is valid
}
if(ff_num == 0)
{
ff_num = (d1_sum*10) + d2_sum; //flip
}
do
{
if(sp_len==0)
{
printf("INVALID\n");
check_flag=1;
break;
}
if((flag==1&&flag_val==1&&ff_num==34)||ff_num==37)
{
printf("AMEX\n");
check_flag=1;
break;
}
else if(flag==1&&flag_val==1&&ff_num>50&&ff_num<56)
{
printf("MASTERCARD\n");
check_flag=1;
break;
}
else if(flag==1&&flag_val==1&&d1_sum==4)
{
printf("VISA\n");
check_flag=1;
break;
}
else
{
printf("INVALID\n");
check_flag=1;
break;
}
}while(check_flag!=1);
return 0;
}
I felt like I was fixing a leak while writing the code. I would try to correct one thing, and another thing would go wrong, and this is the final result.

C function doesn't return string, though it should

I'm super new in C, trying to solve CS50's credit problem here.
So I wrote a function, that should check some parameters, and return a string, which I use in the main function to print an answer.
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <math.h>
int lunh(long n);
char* check(long nu);
int main(void)
{
long number = get_long("Number: ");
printf("%s", check(number));
}
int lunh(long n)
{
int length, step_one, num;
long tens;
step_one = 0;
length = floor(log10(labs(n))) + 1;
for (int i = length, powering = 1; i > length / 2; i--)
{
tens = pow(10, powering);
num = ((n / tens) % 10) * 2;
if (floor(log10(abs(num))) + 1 > 1)
{
while (num)
{
step_one += num % 10;
num /= 10;
}
step_one += num;
powering += 2;
}
else
{
step_one += num;
powering += 2;
}
}
for (int i = length, powering = 0; i > length / 2; i--)
{
tens = pow(10, powering);
num = ((n / tens) % 10);
step_one += num;
powering += 2;
}
if (step_one % 10 == 0)
{
return 1;
}
else
{
return 0;
}
}
char* check(long nu)
{
int l, first_two_digits, first_one;
l = floor(log10(labs(nu))) + 1;
first_one = nu / 1000;
first_two_digits = nu / 100;
char* answer = NULL;
if (l == 15)
{
if (first_two_digits == 34 || first_two_digits == 37)
{
if (lunh(nu) == 1)
{
answer = "AMEX";
}
else
{
answer = "INVALID";
}
}
}
else if (l == 13 || first_one == 4)
{
if (lunh(nu) == 1)
{
answer = "VISA";
}
else
{
answer = "INVALID";
}
}
else if (l == 16)
{
if (first_two_digits == 51 || first_two_digits == 52 || first_two_digits == 53 || first_two_digits == 54 || first_two_digits == 55)
{
if (lunh(nu) == 1)
{
answer = "MASTERCARD";
}
else
{
answer = "INVALID";
}
}
}
else
{
answer = "INVALID";
}
printf("%s", answer);
return answer;
}
Input: 4003600000000014
Expected output: "VISA"
Current output: nothing, after inputting the number, the program stops.
At least one problem is here: else if (l == 13 || first_one == 4). From the spec:
Visa uses 13- and 16-digit numbers.
Visa numbers all start with 4
That if test will produce VISA if length is 13 or card starts with 4. The sample input is a 16 digit number.

loop not completing its iteration in C

I am however having trouble with my game loop! Currently when an invalid move is made, the loop iterates correctly. However when a valid move is made, my program displays the changed board, however appears to freeze in the middle of the loop! i have the location where it freezes (Test Spot 2 in the code), but i have no clue why. The desired output of my code is to keep on iterating until there are no moves available. (I haven't created the check for no moves yet as I cannot get my code to run in a normal counted loop!)
The code working when it is Invalid and not when it is valid is shown below.
here
Main Function where the issue occurs
int main(void)
{
int n;
int p;
char oppChar;
char playChar;
int i;
int pTurn;
int deltaRow;
int deltaCol;
int endGame = 999;
printf("Enter the board dimension: ");
fflush(stdout);
scanf("%d", &n);
printf("Computer plays (B/W) ");
fflush(stdout);
scanf(" %c", &oppChar);
if (oppChar == 'B') {
pTurn = 0;
playChar = 'W';
} else {
pTurn = 1;
playChar = 'B';
}
char board[n][26];
int movesAvalB[n][26];
int movesAvalW[n][26];
int AIBoard[n][26];
for (i = 0; i < n; i++) {
for (p = 0; p < n; p++) {
board[i][p] = 'U';
}
}
board[(n / 2) - 1][(n / 2) - 1] = 'W';
board[(n / 2) - 1][(n / 2)] = 'B';
board[(n / 2)][(n / 2) - 1] = 'B';
board[n / 2][n / 2] = 'W';
printBoard(board, n);
int q = 0;
do {
q++;
// testing
printf(" \n");
printf("Turn Number %d \n", pTurn);
// variable decleration
int k = 0;
int y = 0;
int x = 0;
int max = 0;
int temp = 0;
char c[3] = " ";
// arrays are all 0
for (i = 0; i < n; i++) {
for (p = 0; p < n; p++) {
AIBoard[i][p] = 0;
movesAvalB[i][p] = 0;
movesAvalW[i][p] = 0;
}
}
// moves avaliable to W
for (y = 0; y < n; y++) {
for (x = 0; x < n; x++) {
if (moves(n, x, y, board, 'W')) {
movesAvalW[y][x] = 1;
}
}
}
printf("Test Spot 1\n");
// moves avaliable to B
for (y = 0; y < n; y++) {
for (x = 0; x < n; x++) {
if (moves(n, x, y, board, 'B')) {
printf("Test Spot 1.5\n");
movesAvalB[y][x] = 1;
}
}
}
fflush(stdout);
printf("Test Spot 2\n");
// pTurn = pTurn%2;
fflush(stdout);
printf("Enter a move for colour %c (RowCol): \n", playChar);
fflush(stdout);
scanf("%s", &c);
if (positionInBounds(n, c[0], c[1])) {
int y = c[0] - 97;
int x = c[1] - 97;
if (playChar == 'B') {
if (movesAvalB[y][x] == 1) {
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) {
i = 1;
while ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
(board[y + (i * deltaRow)][x + (i * deltaCol)] == 'W')) {
i++;
if ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
(board[y + (i * deltaRow)][x + (i * deltaCol)] == 'B')) {
while (i != 0) {
i--;
board[y + (i * deltaRow)][x + (i * deltaCol)] = 'B';
}
}
}
}
}
}
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
if (board[y + deltaRow][x + deltaCol] == 'W') {
board[y + deltaRow][x + deltaCol] == 'B';
}
}
}
printBoard(board, n);
} else {
printf("Invalid Move.");
}
}
if (playChar == 'W') {
if (movesAvalW[y][x] == 1) {
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) {
i = 1;
while ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
(board[y + (i * deltaRow)][x + (i * deltaCol)] == 'B')) {
i++;
if ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
(board[y + (i * deltaRow)][x + (i * deltaCol)] == 'W')) {
while (i != 0) {
i--;
board[y + (i * deltaRow)][x + (i * deltaCol)] = 'W';
}
}
}
}
}
}
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
if (board[y + deltaRow][x + deltaCol] == 'B') {
board[y + deltaRow][x + deltaCol] == 'W';
}
}
}
printBoard(board, n);
} else {
printf("Invalid Move.");
}
}
} else {
printf("Invalid Move.");
}
pTurn++;
} while (q <= 5);
printf("were out");
}
Secondary Functions which you probably don't need but maybe
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
void printBoard(char board[][26], int n)
{
int i;
char alpha[27] = "abcdefghijklmnopqrstuvwxyz";
printf(" ");
for (i = 0; i < n; i++) {
printf("%c", alpha[i]);
}
int p;
int q;
for (p = 0; p < n; p++) {
printf("\n");
printf("%c", alpha[p]);
for (q = 0; q < n; q++) {
printf("%c", board[p][q]);
}
}
}
bool positionInBounds(int n, char row, char col)
{
int p = row - 97;
int d = col - 97;
if (p > n) {
return false;
}
if (d > n) {
return false;
}
if (0 > p) {
return false;
}
if (0 > d) {
return false;
}
return true;
}
bool positionIntBounds(int n, int row, int col)
{
if (row > n) {
return false;
}
if (col > n) {
return false;
}
if (0 > row) {
return false;
}
if (0 > col) {
return false;
}
return true;
}
bool checkLegalInDirection(char board[][26], int n, char row, char col, char colour, int deltaRow, int deltaCol)
{
int i = 0;
while ((positionIntBounds(n, (row + (i * deltaRow)), (col + (i * deltaCol)))) &&
(board[row + (i * deltaRow)][col + (i * deltaCol)] != colour) &&
(board[row + (i * deltaRow)][col + (i * deltaCol)] != 'U')) {
i++;
if ((positionIntBounds(n, (row + (i * deltaRow)), (col + (i * deltaCol)))) &&
(board[row + (i * deltaRow)][col + (i * deltaCol)] == colour)) {
return true;
}
}
return false;
}
bool moves(int n, int x, int y, char board[][26], char colour)
{
int deltaRow;
int deltaCol;
if (board[y][x] == 'U') {
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) {
if (checkLegalInDirection(board, n, (x + deltaRow), (y + deltaCol), colour, deltaRow, deltaCol)) {
return true;
}
}
}
}
}
return false;
}
(I was told to put fflush(stdout) before my scanf statements but this didnt fix the problem)

Loop incomplete

I just started learning coding for about 2 months because of the course im taking.
My code works (kinda) but after the 1st loop it wont show the 1st line of the main function("You have been given 20 pokeballs, embrak on you quest on becoming a Pokeman Master!!!:), and i dont know why!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int pokeball = 20;
int rand_num;
int PokemonCaught[5] = { 0, 0, 0 ,0, 0 };
int poke_captured = 0;
int rungame = 1;
int stopgame = 1;
int total;
int randomnum();
int encounter_rate();
int pokemon_met();
void BallThrow();
void CaughtPokemon();
void checkball();
void PokeSummary();
char exitno();
int clear();
int main()
{
printf("You have been given 20 pokeballs, embrak on you quest on becoming a Pokeman Master!!!\n");
getchar();
do
{
checkball();
encounter_rate();
pokemon_met();
} while (stopgame == 0);
PokeSummary();
return 0;
}
int randomnum()
{
srand(time(NULL));
}
int encounter_rate()
{
randomnum();
rand_num = rand() % 100;
}
int pokemon_met()
{
if (rand_num <= 30)
{
printf("A wild Margikarp appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else if (rand_num <= 50)
{
printf("A wild Charmander appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else if (rand_num <= 70)
{
printf("A wild Jigglypuff appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else if (rand_num <= 85)
{
printf("A wild Pikachu appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else
{
printf("A wild Dragonite appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
}
void checkball()
{
if (pokeball > 0)
{
stopgame = 0;
}
else
{
stopgame = 1;
}
}
void BallThrow()
{
randomnum();
int BallChance;
int PokeRun;
BallChance = rand() % 2;
pokeball = pokeball - 1;
if (BallChance == 1)
{
printf("Gotcha!\n");
printf("Number of Pokeball left: %d\n", pokeball);
printf("Press ENTER to continue your journey!\n\n");
poke_captured = poke_captured + 1;
CaughtPokemon();
getchar();
if (pokeball == 0)
{
printf("Your pokeball has used up!\n");
printf("Press ENTER to check the summary of your journey\n");
getchar();
PokeSummary();
}
}
else if (BallChance == 0)
{
PokeRun = rand() % 2;
printf("The pokemon broke free!!!\n");
if (PokeRun == 0)
{
if (pokeball == 0)
{
printf("Your pokeball has used up!\n");
printf("Press ENTER to check the summary of your journey\n");
getchar();
PokeSummary();
}
else
{
printf("Number of Pokeball left: %d\n", pokeball);
printf("Press ENTER to throw pokeball!\n\n");
getchar();
BallThrow();
}
}
else if (PokeRun == 1)
{
printf("Oh no! The pokemon ran away!\n");
printf("Number of Pokeball left: %d\n", pokeball);
printf("Press ENTER to continue your journey!\n\n");
getchar();
if (pokeball == 0)
{
printf("Your pokeball has used up!\n");
printf("Press ENTER to check the summary of your journey\n");
getchar();
PokeSummary();
}
}
}
}
void CaughtPokemon()
{
if (rand_num <= 30)
{
PokemonCaught[0] = PokemonCaught[0] + 1;
}
else if (rand_num <= 50)
{
PokemonCaught[1] = PokemonCaught[1] + 1;
}
else if (rand_num <= 70)
{
PokemonCaught[2] = PokemonCaught[2] + 1;
}
else if (rand_num <= 85)
{
PokemonCaught[3] = PokemonCaught[3] + 1;
}
else if (rand_num <= 95)
{
PokemonCaught[4] = PokemonCaught[4] + 1;
}
}
void PokeSummary()
{
int point0, point1, point2, point3, point4, total;
point0 = (PokemonCaught[0]) * 10;
point1 = (PokemonCaught[1]) * 30;
point2 = (PokemonCaught[2]) * 30;
point3 = (PokemonCaught[3]) * 50;
point4 = (PokemonCaught[4]) * 70;
total = point0 + point1 + point2 + point3 + point4;
printf("You have successfully caught %d Pokemon!\n\n", poke_captured);
printf("You have caught:\n");
printf("Margikarp = %d (%dpoints)\n", PokemonCaught[0], point0);
printf("Charmander = %d (%dpoints)\n", PokemonCaught[1], point1);
printf("Jigglypuff = %d (%dpoints)\n", PokemonCaught[2], point2);
printf("Pikachu = %d (%dpoints)\n", PokemonCaught[3], point3);
printf("Dragonite = %d (%dpoints)\n", PokemonCaught[4], point4);
printf("\nTotal points = %d\n", total);
exitno();
}
char exitno()
{
char stay;
printf("Press 'y' to continue, press any other key to quit.");
scanf(" %c", &stay);
if (stay == 'y' || stay == 'Y')
{
clear();
return (main);
}
else
{
printf("Thank you for playing!!");
exit(0);
}
}
int clear()
{
total = total * 0;
poke_captured = poke_captured * 0;
PokemonCaught[0] = PokemonCaught[0] * 0;
PokemonCaught[1] = PokemonCaught[1] * 0;
PokemonCaught[2] = PokemonCaught[2] * 0;
PokemonCaught[3] = PokemonCaught[3] * 0;
PokemonCaught[4] = PokemonCaught[4] * 0;
pokeball = pokeball + 20;
}
I appreciate any help and i know my codes are far from being decent (sigh)
. Thanks
You have made an error when calling main in char exitno() function:
char exitno()
{
char stay;
printf("Press 'y' to continue, press any other key to quit.");
scanf(" %c", &stay);
if (stay == 'y' || stay == 'Y')
{
clear();
return ((char)main()); // main it should be called using parenthesis
}
else
{
printf("Thank you for playing!!");
exit(0);
}
}
That is not inside of your do-while loop.
If you want it to execute every time, then just move it down into the the loop. Otherwise, your program will move past in the first few milliseconds of operation. Alternatively, modify it (likely, put it within a new function) to print out some information that might change during execution of your program--say, the current number of pokeballs remaining.

Adding arrays indexes with integers

I am working on a problem where I have to calculate frequencies of characters in a text. I havent coded in a while and am a little rusty so I thought it would help to get a second pair of eyes on my code.
my code reads in a file and ideally it should hit my if statements and add "1" to my frequency array. However, it always prints "0". Am I not adding correctly?
public class hw4{
public static void main (String []args)throws IOException{
//ask user to enter file name
System.out.printf("Enter a file location and name to encode [press Enter]: ");
Scanner input = new Scanner(System.in);
String filename = input.next();
//Gets file name from Scanner and checks to see if valid
File file = new File(filename);
Scanner text = new Scanner(file);
String[] letters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int[] freq = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
String letter;
while(text.hasNext()){
letter = text.next();
if (letter == "a"){
freq[0] = freq[0] + 1;
}
if (letter == "b"){
freq[1] = freq[1] + 1;
}
if (letter == "c"){
freq[2] = freq[2] + 1;
}
if (letter == "d"){
freq[3] = freq[3] + 1;
}
if (letter == "e"){
freq[4] = freq[4] + 1;
}
if (letter == "f"){
freq[5] = freq[5] + 1;
}
if (letter == "g"){
freq[6] = freq[6] + 1;
}
if (letter == "h"){
freq[7] = freq[7] + 1;
}
if (letter == "i"){
freq[8] = freq[8] + 1;
}
if (letter == "j"){
freq[9] = freq[9] + 1;
}
if (letter == "k"){
freq[10] = freq[10] + 1;
}
if (letter == "l"){
freq[11] = freq[11] + 1;
}
if (letter == "m"){
freq[12] = freq[12] + 1;
}
if (letter == "n"){
freq[13] = freq[13] + 1;
}
if (letter == "o"){
freq[14] = freq[14] + 1;
}
if (letter == "p"){
freq[15] = freq[15] + 1;
}
if (letter == "q"){
freq[16] = freq[16] + 1;
}
if (letter == "r"){
freq[17] = freq[17] + 1;
}
if (letter == "s"){
freq[18] = freq[18] + 1;
}
if (letter == "t"){
freq[19] = freq[19] + 1;
}
if (letter == "u"){
freq[20] = freq[20] + 1;
}
if (letter == "v"){
freq[21] = freq[21] + 1;
}
if (letter == "w"){
freq[22] = freq[22] + 1;
}
if (letter == "x"){
freq[23] = freq[23] + 1;
}
if (letter == "y"){
freq[24] = freq[24] + 1;
}
if (letter == "z"){
freq[25] = freq[25] + 1;
}
}
for(int i=0; i <26; i++){
System.out.printf("%s:%d\n", letters[i], freq[i]);
}
}
}
I realized that there is an equivalency issue.
the check is not
if(letter == "a")
but
if(letter.equals("A")

Resources