/ *使用属性列表的简单电话号码数据库* /
我已经在Eclipse中多次运行了该程序,但这向我展示了无法解决的编译问题
package phoneBook;
import java.io.*;
import java.io.File;
import java.util.Properties;
class meths
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
FileInputStream fin=null;
try {
fin=new FileInputStream("PhoneBook.txt");
}catch(FileNotFoundException e)
{
e.printStackTrace();
}
String name,number;
Properties pr=new Properties();
if(fin!=null)
{
try {
pr.load(fin); fin.close();
}catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
void find() throws IOException
{
System.out.println("Enter name to find number");
name=br.readLine();
System.out.println("Number is:"+pr.getProperty(number));
}
void enter()
{
FileOutputStream fout=new FileOutputStream("PhoneBook.txt");
System.out.println("Enter name and number:");
name=br.readLine();number=br.readLine();
pr.put(name, number);
pr.store(fout, "Phone Boook");
}
void operation()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int opt=0;
System.out.println("1 for stop operation\n2 for enter new entry\n3 for searching number");
while(opt!=1)
{
// Reading option
try {
opt=br.read();
} catch (IOException e) {
e.printStackTrace();
}// Read opearation is complete
switch(opt)
{
case 2: enter(); break;
case 3: find(); break;
}
}
}
}
public class PhoneBook
{
public static void main(String args[]) throws Exception
{``
meths ph=new meths();
ph.operation();
}
}
答案 0 :(得分:0)
如Elliott Frisch所述,您不能像在方法块外那样尝试,尝试捕获等方式编写代码。 以下代码在任何方法之外。因此,您需要将这些行放在一个方法中,然后从另一个方法中调用它。
try {
fin=new FileInputStream("PhoneBook.txt");
} catch(FileNotFoundException e) {
e.printStackTrace();
}
和
if(fin!=null) {
try {
pr.load(fin); fin.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:0)