Java StreamTokenizer???
???????????? ???????[ 2015/5/19 14:10:00 ] ????????????????
?????????JAVA?????????Scanner?????????????????????????????????????????????POJ1823????????????????????????StreamTokenizer??????????????????????????????Ч??????
??????С?????£?
????1????java.io.StreamTokenizer??????????????????????Token????????
????StreamTokenizer??nextToken???????????????
????2?????????£?StreamTokenizer?????????????Token:????????????c??c++????????????????????
?????????“/”????Token???????????????????"/"??Token???????????????????????????????????Token??
????3????????Ч??????BufferedReader?????£?????StreamTokenizer????
????StreamTokenizer st =new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
????4?????????л?????????????StreamTokenizer??nextToken()??????
????????nextToken()?????????????????????????? String s=st.sval????????????? int n=(int) st.nval?????
????st.nextToken();
????int i=(int) st.nval; //st.navl??????????????double
????st.nextToken();
????int j=(int) st.nval;
????st.nextToken();
????String s=st.sval;
?????????
???????????
?????????
??????java.io.StreamTokenizer??????????????????????Token(???)??StreamTokenizer??nextToken?????????????????
???????????£?StreamTokenizer?????????????Token??????????????C??C++?????????????????????????“/”????Token???????????????????“”??Token????????????????????е????????????????Token??
??????????????????????????????Token??????????????????Token??????Token??漲???????е?????????10?????????Token??????????????????е??????????Token????????StreamTokenizer??ordinaryCha()????????????????????????????????
//??????????????????????·????String fileName = "d:/ceshi.txt";
package szu.edu;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.StreamTokenizer;
/**
* ???StreamTokenizer?????????е??????
* StreamTokenizer ????????????????????“???”????????ζ?????????
* ??????????????????????????????????????????
* ???????????????????????????????????????????????????????
*
* ???????£?StreamTokenizer?????????????Token: ????????????C??C++????????????????????
* ?????"/"????Token???????????????????""??Token????????????????????е????????????????Token??
* ???????????????????????????Token?????′?????????????????Token??????Token??漲??
* ?????е?????????10???????Token??????????????????е??????????Token????????????????
* st.ordinaryChar('''); //?????????????????
* st.ordinaryChar('"'); //????????????????
*/
public class StatisFileChars {
/**
* ????????
* @param fileName ?????
* @return ?????
*/
public static long statis(String fileName) {
FileReader fileReader = null;
try {
fileReader = new FileReader(fileName);
//???????????????????????????
StreamTokenizer st = new StreamTokenizer(new BufferedReader(
fileReader));
//ordinaryChar?????????????????????????????“???”?????
//??????????????????????????????????
st.ordinaryChar(''');
st.ordinaryChar('"');
st.ordinaryChar('/');
String s;
int numberSum = 0;
int wordSum = 0;
int symbolSum = 0;
int total = 0;
//nextToken????????????Token.
//TT_EOF??????????β???????
while (st.nextToken() != StreamTokenizer.TT_EOF) {
//????? nextToken ???????ttype??ν??????????????????
switch (st.ttype) {
//TT_EOL??????????β???????
case StreamTokenizer.TT_EOL:
break;
//TT_NUMBER????????????????????
case StreamTokenizer.TT_NUMBER:
//???????????????????nval??ν?????????????
s = String.valueOf((st.nval));
System.out.println(s);
numberSum += s.length();
break;
//TT_WORD????????????????????
case StreamTokenizer.TT_WORD:
//?????????????????????sval????????????????????????????????
s = st.sval;
wordSum += s.length();
break;
default:
//???????3????????????????????????
s = String.valueOf((char) st.ttype);
symbolSum += s.length();
}
}
System.out.println("sum of number = " + numberSum);
System.out.println("sum of word = " + wordSum);
System.out.println("sum of symbol = " + symbolSum);
total = symbolSum + numberSum + wordSum;
System.out.println("Total = " + total);
return total;
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args) {
String fileName = "d:/ceshi.txt";
StatisFileChars.statis(fileName);
}
}
?????????????????÷??????????????????÷?????????????????????(???磺??????acm????У?????Ч??????)
BufferedReader provides quite fast read operations for almost all problems. But this class may be used to read single characters and lines only. To read tokens and numbers you should use StringTokenizer orStreamTokenizer.
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int a?? b;
while(in.nextToken() != StreamTokenizer.TT_EOF)
{
a = (int)in.nval;
in.nextToken();
b = (int)in.nval;
//out.println(a + b);
System.out.println("a + b = "+(a+b));
}
//out.flush();
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11