(1)计算这篇短文的字符数(含空白)共有多少?
(2)若不含空白,这篇短文共有多少个字符?
(3)统计这篇短文用了多少个单词“to”。
(4)将这篇短文内所有大写字母改成小写,并将更改后的短文写到文本文件”joke.txt” 里。
There was an American couple who had no children, so they wanted to adopt a child. Finally, an orphanage contacted them, saying, "We have a baby for adoption.It's a Russian orphan." The couple was delighted and went to bring the baby home.
On the way home, they stopped by a university to enroll in a Russian course. "Why do you want to learn Russian? The English that we speak is a very good language," the university secretary asked."Well, we just adopted a Russian baby. When he begins to speak Russian in a few years, we are afraid that we might not be able to understand him," the couple replied.
(2)若不含空白,这篇短文共有多少个字符?
(3)统计这篇短文用了多少个单词“to”。
(4)将这篇短文内所有大写字母改成小写,并将更改后的短文写到文本文件”joke.txt” 里。
There was an American couple who had no children, so they wanted to adopt a child. Finally, an orphanage contacted them, saying, "We have a baby for adoption.It's a Russian orphan." The couple was delighted and went to bring the baby home.
On the way home, they stopped by a university to enroll in a Russian course. "Why do you want to learn Russian? The English that we speak is a very good language," the university secretary asked."Well, we just adopted a Russian baby. When he begins to speak Russian in a few years, we are afraid that we might not be able to understand him," the couple replied.
解决方案 »
- JTextField这个组件调用哪个方法可以隐藏边框??
- 关于Java io 中编码解码的问题
- BufferedImage怎样可以成为JButton 的背景 ?
- 我写了个多人聊天程序如何连到外网
- 求JDBC的连接实例。新人
- 高手指点!利用java中的正则表达式解析html网页的问题?
- 与各位研究一下关于jbuilder 9.0中query控件设置模糊查询!!
- 还是问一下这个问题吧,说不定有好办法
- 初学Java遇到难题,求救
- SOS!SOS!我用VJ++做的Applet,在VJ++运行他所在的工程下能正常用IE浏览,但单独运行包含Applet超文本文件时,Applet却显示不出来,请各路高手不吝赐教。小弟在此先表感谢
- java 和C++
- Hibernate是否支持union啊
字符串长度:str.length()
String[] arr = str.split(" ")
for循环遍历arr
可以统计非空白字符数及有多少个to最后toUpperCase()转,输出
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class T {
public static void main(String[] args) throws IOException {
//读入文件
FileInputStream fis = new FileInputStream(new File("data.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringBuffer sb = new StringBuffer();
String str = br.readLine();
while(str != null) {
sb.append(str);
str = br.readLine();
}
br.close();
//统计文件的字符数、非空白字符数和单词to的个数
str = sb.toString();
int count = str.length();//总共的字符数
String[] words = str.split(" ");
int count1 = 0;//非空白字符数
int count2 = 0;//to的个数
for(String word : words) {
count1 += word.length();
if(word.equals("to")) {
count2 ++;
}
}
System.out.println("文件中总共有" + count + "个字符!");
System.out.println("文件中总共有" + count1 + "个非空白字符!");
System.out.println("文件中总共有" + count2 + "个单词to!");
//文件中的所有大写字母改成小写字母
str = str.toLowerCase();
//转换后的内容写入文件
FileOutputStream fos = new FileOutputStream("joke.txt");
fos.write(str.getBytes());
fos.close();
}
}
1.首先算出短文的字符,接住用.trim()得到新的字符串 ~前减后。得到空白
2.1中有了
3.简单的算法
4.。。
不知道行不行呢