每日一練------字元統計(數字、大小寫字母、空格等等)
阿新 • • 發佈:2019-02-09
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/*
* 題目:輸入一行字元,分別統計出其中英文字母、空格、數字和其它字元的個數。統計字元型別
* 寫了過載的兩個方法,一個接受字串作為引數,一個接受檔案作為引數
*/
public class Basic7 {
private int number = 0;
private int bigLetter = 0;
private int smallLetter = 0;
private int space = 0;
private int others = 0;
public static void main(String[] args) {
Basic7 my1 = new Basic7();
Basic7 my2 = new Basic7();
String myString = "akhfahueueb24837 yiufa/.;.,gjakhghwuihg\\ajkahgjab9028748565";
String fileName = "C:\\Users\\風中徜徉\\Desktop\\liujunfeng.txt";
File myFile = new File(fileName);
my1.statistic(myString);
my2.statistic(myFile);
}
public void statistic(String astring)
{
byte mybytes[] = astring.getBytes();
for(byte b :mybytes)
{
if(b>='0' &&b<='9')
{
number++;
}
if(b>='a' &&b<='z')
{
smallLetter++;
}if(b>='A' &&b<='Z')
{
bigLetter++;
}
if(b==' ')
{
space++;
}
else{others++;}
}
System.out.println("數字"+number+" 小寫字母 "+smallLetter+" 大寫字母"+bigLetter+" 空格"+space+" 其他"+others);
}
public void statistic(File afile)
{
BufferedReader br =null ;
try {
br = new BufferedReader(new FileReader(afile));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(afile.getAbsolutePath()+"檔案不存在");
//e.printStackTrace();
}
String temp;
try {
while((temp=br.readLine())!=null)
{
byte mybytes[] = temp.getBytes();
for(byte b :mybytes)
{
if(b>=0 &&b<=9)
{
number++;
}
if(b>='a' &&b<='z')
{
smallLetter++;
}if(b>='A' &&b<='Z')
{
bigLetter++;
}
if(b==' ')
{
space++;
}
else{others++;}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("數字"+number+" 小寫字母 "+smallLetter+" 大寫字母"+bigLetter+" 空格"+space+" 其他"+others);
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/*
* 題目:輸入一行字元,分別統計出其中英文字母、空格、數字和其它字元的個數。統計字元型別
* 寫了過載的兩個方法,一個接受字串作為引數,一個接受檔案作為引數
*/
public class Basic7 {
private int number = 0;
private int bigLetter = 0;
private int smallLetter = 0;
private int space = 0;
private int others = 0;
public static void main(String[] args) {
Basic7 my1 = new Basic7();
Basic7 my2 = new Basic7();
String myString = "akhfahueueb24837 yiufa/.;.,gjakhghwuihg\\ajkahgjab9028748565";
String fileName = "C:\\Users\\風中徜徉\\Desktop\\liujunfeng.txt";
File myFile = new File(fileName);
my1.statistic(myString);
my2.statistic(myFile);
}
public void statistic(String astring)
{
byte mybytes[] = astring.getBytes();
for(byte b :mybytes)
{
if(b>='0' &&b<='9')
{
number++;
}
if(b>='a' &&b<='z')
{
smallLetter++;
}if(b>='A' &&b<='Z')
{
bigLetter++;
}
if(b==' ')
{
space++;
}
else{others++;}
}
System.out.println("數字"+number+" 小寫字母 "+smallLetter+" 大寫字母"+bigLetter+" 空格"+space+" 其他"+others);
}
public void statistic(File afile)
{
BufferedReader br =null ;
try {
br = new BufferedReader(new FileReader(afile));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(afile.getAbsolutePath()+"檔案不存在");
//e.printStackTrace();
}
String temp;
try {
while((temp=br.readLine())!=null)
{
byte mybytes[] = temp.getBytes();
for(byte b :mybytes)
{
if(b>=0 &&b<=9)
{
number++;
}
if(b>='a' &&b<='z')
{
smallLetter++;
}if(b>='A' &&b<='Z')
{
bigLetter++;
}
if(b==' ')
{
space++;
}
else{others++;}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("數字"+number+" 小寫字母 "+smallLetter+" 大寫字母"+bigLetter+" 空格"+space+" 其他"+others);
}
}
執行結果如下圖