This is my program, it will count all the spaces, all the letter a,e,s,t upper or lower cases but I'm having an error when compiling, it give me that one of my variable is not initialized.
Could someone take a look and tell me
import java.util.Scanner;
public class Count
{
public static void main (String[] args)
{
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
char ch; // an individual character in the string
int countA=0,countE=0,countS=0,countT=0;
Scanner scan = new Scanner(System.in);
// Print a program h开发者_JAVA技巧eader
System.out.println ();
System.out.println ("Character Counter");
System.out.println ();
// Read in a string and find its length
System.out.print ("Enter a sentence or phrase: ");
phrase = scan.nextLine();
length = phrase.length();
// Initialize counts
countBlank = 0;
// a for loop to go through the string character by character
for (int i = 0; i < phrase.length(); i++)
{
if(phrase.charAt(i) == ' ') countBlank++;
}
switch(ch) {
case 'a':
case 'A': countA++;
break;
case 'e':
case 'E': countE++;
break;
case 's':
case 'S': countS++;
break;
case 't':
case 'T': countT++;
break;
}
// Print the results
System.out.println ();
System.out.println ("Number of blank spaces: " + countBlank);
System.out.println ("Number of a: " + countA);
System.out.println ("Number of e: " + countE);
System.out.println ("Number of s: " + countS);
System.out.println ("Number of t: " + countT);
System.out.println ();
}
}
You write
switch(ch)
but the variable ch
is never assigned a value.
You have "switch(ch)" but you never assigned it a value. You declared it using "char ch;" but that's not enough.
Maybe you want to do:
ch = phrase.charAt(i);
inside of your loop.
So your combined code can look like this:
import java.util.Scanner;
public class Count
{
public static void main (String[] args)
{
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
char ch; // an individual character in the string
int countA=0,countE=0,countS=0,countT=0;
Scanner scan = new Scanner(System.in);
// Print a program header
System.out.println ();
System.out.println ("Character Counter");
System.out.println ();
// Read in a string and find its length
System.out.print ("Enter a sentence or phrase: ");
phrase = scan.nextLine();
length = phrase.length();
// Initialize counts
countBlank = 0;
// a for loop to go through the string character by character
for (int i = 0; i < phrase.length(); i++)
{
if(phrase.charAt(i) == ' ') countBlank++;
ch = phrase.charAt(i);
switch(ch) {
case 'a':
case 'A': countA++;
break;
case 'e':
case 'E': countE++;
break;
case 's':
case 'S': countS++;
break;
case 't':
case 'T': countT++;
break;
}
}
// Print the results
System.out.println ();
System.out.println ("Number of blank spaces: " + countBlank);
System.out.println ("Number of a: " + countA);
System.out.println ("Number of e: " + countE);
System.out.println ("Number of s: " + countS);
System.out.println ("Number of t: " + countT);
System.out.println ();
}
}
There are two issues: first the current character is never assigned to ch and second the switch statement is not inside for your for loop.
change Ur for statement like this , the switch must be included in the for , and the ch should be initialized to be the i-th character .
for (int i = 0; i < phrase.length(); i++)
{
if(phrase.charAt(i) == ' ')
{
countBlank++;
}
ch = phrase.charAt(i);
switch(ch)
{
case 'a':
case 'A': countA++;
break;
case 'e':
case 'E': countE++;
break;
case 's':
case 'S': countS++;
break;
case 't':
case 'T': countT++;
break;
}
}
精彩评论