从键盘输入一串字符串,编写一个java程序实现统计,输出有几个大写字母,几个小写字母,和几个数字,并把大
这个是处理文件的类FileUtil:
=================================================
package org.xhome.leon.test;
import java.io.*;
public class FileUtil {
FileReader fr;
BufferedReader br;
FileWriter fw;
BufferedWriter bw;
String source = "";
public int upCaseNum = 0;
public int lowerCaseNum = 0;
public int numerNum = 0;
public int otherNum = 0;
public void initReaders(String fileName) throws FileNotFoundException {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
}
public void initWriters(String fileName) throws IOException {
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
}
public void readFile(String fileName) throws FileNotFoundException {
this.initReaders(fileName);
this.source = "";
upCaseNum = 0;
lowerCaseNum = 0;
numerNum = 0;
otherNum = 0;
String sr;
try {
while ((sr = br.readLine()) != null) {
source = source.concat(sr);
}
if (source != null) {
char[] myChars = source.trim().toCharArray();
for (int i = 0; i < myChars.length; i++) {
if (myChars[i] >= 'a' && myChars[i] <= 'z') {
lowerCaseNum++;
} else if (myChars[i] >= 'A' && myChars[i] <= 'Z') {
upCaseNum++;
} else if (myChars[i] >= '0' && myChars[i] <= '9') {
numerNum++;
} else {
otherNum++;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
this.closeReaderIO();
}
}
public void writeFile(String infileName,String outfileName, boolean isToUperCase) throws IOException {
this.initWriters(outfileName);
this.initReaders(infileName);
String st;
try {
if (isToUperCase) {
while ((st = br.readLine()) != null) {
st = st.toUpperCase();
try {
bw.write(st);
bw.newLine();
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
while ((st = br.readLine()) != null) {
st = st.toLowerCase();
try {
bw.write(st);
bw.newLine();
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
this.closeReaderIO();
this.closeWriterIO();
}
}
public void closeReaderIO() {
if (this.fr != null) {
try {
this.fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (this.br != null) {
try {
this.br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void closeWriterIO() {
if (this.fw != null) {
try {
this.fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (this.bw != null) {
try {
this.bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
下面是界面编写的类:
=========================================================
package org.xhome.leon.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.*;
import org.xhome.leon.test.FileUtil;
public class MainView extends JFrame{
JPanel upPanel = new JPanel();
JPanel downPanel = new JPanel();
JLabel rlabel = new JLabel("读入文件绝对路径");
JLabel wlabel = new JLabel("写出文件绝对路径");
JTextField rjtf = new JTextField();
JTextField wjtf = new JTextField();
JButton rbtn = new JButton("读取");
JButton wbtn = new JButton("写入");
JButton upORlow = new JButton("小写方式写入");
JSplitPane jsp;
JTextArea jta = new JTextArea();
boolean isUperCase = false;
FileUtil ft;
public MainView(){
ft = new FileUtil();
this.setBounds(200, 100, 600, 400);
this.init();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(this, "请注意,文件路径分隔符为 / ,而非 \\,比如D盘下的test.txt\n为D:/test.txt");
}
public void init(){
upPanel.setLayout(null);
rlabel.setBounds(5, 5, 150, 30);
wlabel.setBounds(5, 80, 150, 30);
rjtf.setBounds(165, 5, 215, 30);
wjtf.setBounds(165, 80, 215, 30);
rbtn.setBounds(505, 5, 60, 30);
wbtn.setBounds(505, 80, 60, 30);
upORlow.setBounds(5, 120, 120, 30);
rbtn.addActionListener(new MyButtonListener());
wbtn.addActionListener(new MyButtonListener());
upORlow.addActionListener(new MyButtonListener());
upPanel.add(rlabel);
upPanel.add(wlabel);
upPanel.add(rjtf);
upPanel.add(wjtf);
upPanel.add(rbtn);
upPanel.add(wbtn);
upPanel.add(upORlow);
jta.setEditable(false);
downPanel.add(jta);
jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upPanel, downPanel);
jsp.setDividerLocation(170);
jsp.setEnabled(false);
this.add(jsp);
}
class MyButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(rbtn)){
try {
ft.readFile(rjtf.getText().toString().trim());
jta.setText("大写字母:"+ft.upCaseNum+"\n\n");
jta.append("小写字母:"+ft.lowerCaseNum+"\n\n");
jta.append("数字:"+ft.numerNum+"\n\n");
jta.append("其他:"+ft.otherNum+"\n\n");
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(MainView.this, "读取错误,请确认文件路径是否正确");
return;
}
}
else if(e.getSource().equals(wbtn)){
String inFile = rjtf.getText().toString().trim();
if(inFile != null && !inFile.equals("")){
try {
ft.writeFile(inFile, wjtf.getText().toString().trim(), isUperCase);
JOptionPane.showMessageDialog(MainView.this, "写入成功!");
} catch (IOException e1) {
JOptionPane.showMessageDialog(MainView.this, "写入错误,请确认文件路径是否正确");
return;
}
}
}
else if(e.getSource().equals(upORlow)){
if(isUperCase){
isUperCase = false;
upORlow.setText("小写方式写入");
}
else{
isUperCase = true;
upORlow.setText("大写方式写入");
}
}
}
}
}
===============================================
下面是一个装main方法的类:
package org.xhome.leon.main;
import org.xhome.leon.view.MainView;
public class Launcher {
public static void main(String[] args) {
new MainView();
}
}
======================================
下面是我用自己写的那个java文件测试的结果截图:
=======================================
ps:这个是看了楼主的要求后写的。用的是窗口程序实现,如果不用界面,你可以直接用FileUtil这个类进行操作就可以了。 希望对你用帮助 ^ ^
Scanner in=new Scanner(System.in);
String str = in.nextLine();
int DaXie = 0;
int XiaoXie = 0;
int ShuZi = 0;
//大小写字母,数字的个数
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i) > 47 && str.charAt(i) < 58){
ShuZi ++;
}else if(str.charAt(i) > 64 && str.charAt(i) < 91){
DaXie ++;
}else if(str.charAt(i) > 96 && str.charAt(i) < 123){
XiaoXie ++;
}
}
System.out.println("大写的有"+DaXie+"个");
System.out.println("小写的有"+XiaoXie+"个");
System.out.println("数字的有"+ShuZi+"个");
//大写转小写,小写转大写
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if(Character.isUpperCase(ch[i])) str += Character.toLowerCase(ch[i]);
else if(Character.isLowerCase(ch[i])) str += Character.toUpperCase(ch[i]);
else str += ch[i];
}
}
string a ="AbCd";
char[] b =new char[a.length];
for(int i=0;i<a.length;i++)
{
if(a[i]<=z)
{
char[i]=a[i].toUpperCaser();
}
else
{
char[i]=a[i].toLowerCaser();
}
}
for(int i =0;i<a.length;i++)
{
system.out.print(b[i]);
}