将java的关键字保存在一个文件中,编写程序判断一个字符串是否为java关键字
1个回答
展开全部
JavaHotKeyChecker.java:
=======================
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JavaHotKeyChecker {
public static void main(String[] args) throws IOException {
boolean yon = doCheck("class");
if(yon) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
private static boolean doCheck(String input) throws IOException {
boolean isHotKey = false;
File file = new File("D:/javahotkey.txt");
String content = "";
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String temp;
while (null != (temp = br.readLine())) {
content += temp;
}
String[] keys = content.split(",");
List<String> keyList = new ArrayList<String>();
for (String key : keys) {
keyList.add(key.trim());
}
isHotKey = keyList.contains(input);
return isHotKey;
}
}
javahotkey.txt:
=================
public,static,void,class,interface,final,finally,catch,try
=======================
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JavaHotKeyChecker {
public static void main(String[] args) throws IOException {
boolean yon = doCheck("class");
if(yon) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
private static boolean doCheck(String input) throws IOException {
boolean isHotKey = false;
File file = new File("D:/javahotkey.txt");
String content = "";
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String temp;
while (null != (temp = br.readLine())) {
content += temp;
}
String[] keys = content.split(",");
List<String> keyList = new ArrayList<String>();
for (String key : keys) {
keyList.add(key.trim());
}
isHotKey = keyList.contains(input);
return isHotKey;
}
}
javahotkey.txt:
=================
public,static,void,class,interface,final,finally,catch,try
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询