如何跳出main方法里的循环啊?我尝试用fis.read()== -1来跳出循环,可是好像不管用!!!
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class WordAnalyse {
public static void main( String args[] ) throws Exception{
FileInputStream fis = new FileInputStream( "D:\\javacode\\Test\\src\\Word.txt" );
char c =' ';
String str;
while( true ){
str = "";
if( ' ' == c ){
c = getChar( fis );
}
switch( c ){
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
while( letter(c)||digit(c) ){
str = str + String.valueOf( c );
c = ( char )fis.read();
}
pangduan( str );
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
while( digit(c) ){
str = str + c;
c = ( char )fis.read();
}
System.out.println("( "+str+",常数 )" );
break;
case '+':
System.out.println( "( +,加号 )" );
c = ( char )fis.read();
break;
case '-':
System.out.println( "( -,减号 )" );
c = ( char )fis.read();
break;
case '*':
System.out.println( "( *,乘号 )" );
c = ( char )fis.read();
break;
case '<':
c = ( char )fis.read();
if ( '=' == c ){
System.out.println( "( <=,小于等于 )" );
c = ( char )fis.read();
}else{System.out.println( "( <,小于号 )" );}
break;
case '=':
c = ( char )fis.read();
if( '=' == c ){
System.out.println( "( ==,等于 )" );
c = ( char )fis.read();
}else{System.out.println( "( =,赋值 )" );}
break;
case '\r':
System.out.println( "( \\r,换行符 )" );
c = ( char )fis.read();
break;
case '\n':
System.out.println( "( \\n,换行符 )" );
c = ( char )fis.read();
break;
case '(':
System.out.println( "( (,左括号 )" );
c = ( char )fis.read();
break;
case ')':
System.out.println( "( ),右括号 )" );
c = ( char )fis.read();
break;
case '[':
System.out.println( "( [,中括号 )" );
c = ( char )fis.read();
break;
case ']':
System.out.println( "( ],中括号 )" );
c = ( char )fis.read();
break;
case '.':
System.out.println( "( .,句号 )" );
c = ( char )fis.read();
break;
case '{':
System.out.println( "( {,花括号 )" );
c = ( char )fis.read();
break;
case '}':
System.out.println( "( },花括号 )" );
c = ( char )fis.read();
break;
case '"':
System.out.println( "( \",双引号 )" );
c = ( char )fis.read();
break;
case ';':
System.out.println( "( ;,分号 )" );
c = ( char )fis.read();
break;
default:
c = ( char )fis.read();
System.out.println( "错误符号" );
}
if( '`' == c ){
break;
}
}
}
public static void pangduan( String str ){
if( str.equals("while")){
System.out.println( "( while,保留字 )" );
}else if( str.equals( "if" ) ){
System.out.println( "( if,保留字 )" );
}else if( str.equals( "else" ) ){
System.out.println( "( else,保留字 )" );
}else if( str.equals( "switch" ) ){
System.out.println( "( switch,保留字 )" );
}else if( str.equals( "case" ) ){
System.out.println( "( case,保留字 )" );
}else if( str.equals( "public" ) ){
System.out.println( "( public,保留字 )" );
}else if( str.equals( "static" ) ){
System.out.println( "( static,保留字 )" );
}else if( str.equals( "void" ) ){
System.out.println( "( void,保留字 )" );
}else if( str.equals( "main" ) ){
System.out.println( "( main,保留字 )" );
}else if( str.equals( "System" ) ){
System.out.println( "( System,保留字 )" );
}else if( str.equals( "string" ) ){
System.out.println( "( string,保留字 )" );
} else if( str.equals( "out" ) ){
System.out.println( "( out,保留字 )" );
}else if( str.equals( "println" ) ){
System.out.println( "( println,保留字 )" );
}
else{
System.out.println( "( "+str+",变量 )" );
}
}
public static char getChar( FileInputStream fis ) throws Exception{
int c = 0;
while( true ){
c = fis.read();
if( 32 != c ){
break;
}
}
return ( char )c;
}
public static boolean letter( char c ){
if( 'a'<=c && c<='z' ){
return true;
}else return false;
}
public static boolean digit( char c ){
if( '0'<=c && c<='9' ){
return true;
}else return false;
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class WordAnalyse {
public static void main( String args[] ) throws Exception{
FileInputStream fis = new FileInputStream( "D:\\javacode\\Test\\src\\Word.txt" );
char c =' ';
String str;
while( true ){
str = "";
if( ' ' == c ){
c = getChar( fis );
}
switch( c ){
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
while( letter(c)||digit(c) ){
str = str + String.valueOf( c );
c = ( char )fis.read();
}
pangduan( str );
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
while( digit(c) ){
str = str + c;
c = ( char )fis.read();
}
System.out.println("( "+str+",常数 )" );
break;
case '+':
System.out.println( "( +,加号 )" );
c = ( char )fis.read();
break;
case '-':
System.out.println( "( -,减号 )" );
c = ( char )fis.read();
break;
case '*':
System.out.println( "( *,乘号 )" );
c = ( char )fis.read();
break;
case '<':
c = ( char )fis.read();
if ( '=' == c ){
System.out.println( "( <=,小于等于 )" );
c = ( char )fis.read();
}else{System.out.println( "( <,小于号 )" );}
break;
case '=':
c = ( char )fis.read();
if( '=' == c ){
System.out.println( "( ==,等于 )" );
c = ( char )fis.read();
}else{System.out.println( "( =,赋值 )" );}
break;
case '\r':
System.out.println( "( \\r,换行符 )" );
c = ( char )fis.read();
break;
case '\n':
System.out.println( "( \\n,换行符 )" );
c = ( char )fis.read();
break;
case '(':
System.out.println( "( (,左括号 )" );
c = ( char )fis.read();
break;
case ')':
System.out.println( "( ),右括号 )" );
c = ( char )fis.read();
break;
case '[':
System.out.println( "( [,中括号 )" );
c = ( char )fis.read();
break;
case ']':
System.out.println( "( ],中括号 )" );
c = ( char )fis.read();
break;
case '.':
System.out.println( "( .,句号 )" );
c = ( char )fis.read();
break;
case '{':
System.out.println( "( {,花括号 )" );
c = ( char )fis.read();
break;
case '}':
System.out.println( "( },花括号 )" );
c = ( char )fis.read();
break;
case '"':
System.out.println( "( \",双引号 )" );
c = ( char )fis.read();
break;
case ';':
System.out.println( "( ;,分号 )" );
c = ( char )fis.read();
break;
default:
c = ( char )fis.read();
System.out.println( "错误符号" );
}
if( '`' == c ){
break;
}
}
}
public static void pangduan( String str ){
if( str.equals("while")){
System.out.println( "( while,保留字 )" );
}else if( str.equals( "if" ) ){
System.out.println( "( if,保留字 )" );
}else if( str.equals( "else" ) ){
System.out.println( "( else,保留字 )" );
}else if( str.equals( "switch" ) ){
System.out.println( "( switch,保留字 )" );
}else if( str.equals( "case" ) ){
System.out.println( "( case,保留字 )" );
}else if( str.equals( "public" ) ){
System.out.println( "( public,保留字 )" );
}else if( str.equals( "static" ) ){
System.out.println( "( static,保留字 )" );
}else if( str.equals( "void" ) ){
System.out.println( "( void,保留字 )" );
}else if( str.equals( "main" ) ){
System.out.println( "( main,保留字 )" );
}else if( str.equals( "System" ) ){
System.out.println( "( System,保留字 )" );
}else if( str.equals( "string" ) ){
System.out.println( "( string,保留字 )" );
} else if( str.equals( "out" ) ){
System.out.println( "( out,保留字 )" );
}else if( str.equals( "println" ) ){
System.out.println( "( println,保留字 )" );
}
else{
System.out.println( "( "+str+",变量 )" );
}
}
public static char getChar( FileInputStream fis ) throws Exception{
int c = 0;
while( true ){
c = fis.read();
if( 32 != c ){
break;
}
}
return ( char )c;
}
public static boolean letter( char c ){
if( 'a'<=c && c<='z' ){
return true;
}else return false;
}
public static boolean digit( char c ){
if( '0'<=c && c<='9' ){
return true;
}else return false;
}
}
* 从文件读数据
*/
public void testRead() throws IOException {
String filename = "D://aaa.txt";
File file = new File(filename);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s = null ;
while ( (s=br.readLine()) != null) {
System.out.println(s);
}
br.close();
fr.close();
}
我这个算法是一定要单字节读取的,所以无法采用你这种判断(s=br.readLine()) != null) 来跳出循环
。
}?
str = "";
if( ' ' == c ){
c = getChar( fis );}
if(c == -1){
break;
}
}
。
}?
貌似不对吧!