类型字符的Error方法未定义

时间:2014-01-12 23:02:56

标签: java character

我有一个程序可以找到.txt文件中出现四个字母的次数,我无法弄清楚它为什么会给我这个错误:

Error: The method isWhiteSpace(char) is undefined for the type java.lang.Character

此错误的含义是什么,导致此问题的原因是什么,以及如何解决?

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileNotFoundException;

public class Count
{
  public static void main (String[] args) throws  FileNotFoundException {

  String phrase;
  String everything = "";  // 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;
  int countE;
  int countS;
  int countT;

  java.io.File file = new java.io.File("counting.txt");
  Scanner inFile = new Scanner (file);

 Scanner scan = new Scanner(System.in);

 phrase = inFile.nextLine();
 length = phrase.length();

      // Initialize counts

  while (true)
  { 
  if (phrase.equalsIgnoreCase("quit"))

      break;

  else
  {

  countBlank = 0;
  countA = 0;
  countE = 0;
  countS = 0;
  countT = 0;

  for ( int i = 0; i < 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;
      }

 }
        System.out.println ();
        System.out.println ("Number of blank spaces: " + countBlank);
        System.out.println ();

    System.out.println ("Number of A's: " + countA);
    System.out.println ();
    System.out.println ("Number of E's: " + countE);
    System.out.println ();
    System.out.println ("Number of S's: " + countS);
    System.out.println ();
    System.out.println ("Number of T's: " + countT);
    break;

  }     
 }


 } 
}

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

方法isWhiteSpace(char)接受类型为char而非Character的参数。

Character是一个对象,char是本机类型。

如果char1Character类型的对象,请使用以下代码:

//Assume char1 is an object of type Character
boolean flagWS;
flagWS = Character.isWhiteSpace(char1.charValue())