Help with Print Statement using array's
Hi, I'm a student who is still learning the basics of java.
I'm currenlty working on a program that read's a string, convert's it to lower case, trims the string from whitespaces and then reports how many times each letter of the alphabet appears in that string and reports any non-letters in the string.
I know the easiest way to do this is using a switch case but thats a very poor design and I wouldn't want to do that.
I have managed to create my program but having trouble with my Print Statement because I'm using a for loop to print an array.
Here is my Code:
import B102.*;
class Message
{
static String ReadString()
{
String Read =new String();
System.out.println("Enter in a string:");
Read = Keybd.in.readLine();
Read.toLowerCase();/*convert string to lowercase*/
Read.trim();/*trim string from whitespace*/
return(Read);
}
staticchar[] ConvertString(String Read)
/*This gets the String and stores each character into an array*/
{
int length = Read.length();
char[] input =newchar[length];
for(int i = 0; i < input.length; i++)
{
input[i] = Read.charAt(i);
}
return(input);
}
staticint[] InputToInt(char[] input)
/*This takes the array and store it into another array, converting the
characters from the previous array into an int*/
{
int[] check =newint[input.length];
for(int i = 0; i < check.length; i++)
{
check[i] = (int) input[i];
}
return(check);
}
staticint[] CountInput(char[] input,int[] check)
/*Using the ASCII Table, this checks the int[] array to see if it's a valid
alphabet letter, and if it is, it adds 1 to an int array counter*/
{
int[] count =newint[input.length];
for(int i = 0; i < input.length; i++)
{
if((check[i] >= 97) && (check[i] <= 122))
count[i]++;
}
return(count);
}
staticint CountNInput(char[] input,int[] check)
/*Using the ASCII Table, this checks the int[] array to find non valid
alphabet letters, and adds 1 to an int counter*/
{
int ncount = 0;
for(int i = 0; i < input.length; i++)
{
if((check[i] >= 0) && (check[i] <= 64))
ncount++;
else
if((check[i] >= 91) && (check[i] <= 96))
ncount++;
else
if(check[i] >= 123)
ncount++;
}
return(ncount);
}
staticvoid PrintResult(char[] input,int[] check,int[] count,int ncount)
{
for(int i = 0; i < input.length; i++)
{
if((check[i] >= 65) && (check[i] <= 90))
System.out.println("The letter "+input[i]+" is found "+count[i]+" times");
else
if((check[i] >= 97) && (check[i] <= 122))
System.out.println("The letter "+input[i]+" is found "+count[i]+" times");
}
System.out.println("There are "+ncount+" non-letters found");
return;
}
publicstaticvoid main(String args[])
{
String Read;
char[] input;
int[] check;
int[] count;
int ncount;
Read = ReadString();
input = ConvertString(Read);
check = InputToInt(input);
count = CountInput(input, check);
ncount = CountNInput(input, check);
PrintResult(input, check, count, ncount);
return;
}
}
A Sample Program Test from my source code - which is part of the problem i need solving.
Enter in a String:
abc abc
The letter a is found 1 times
The letter b is found 1 times
The letter c is found 1 times
The letter a is found 1 times
The letter b is found 1 times
The letter c is found 1 times
How can I change this so it would have given the result of:
The letter a is found 2 times
The letter b is found 2 times
The letter c is found 2 times

