JAVA编程实现郑码查询(1)——将码表放在文本文件.txt和.sqlite数据库中

方法一:将码表放在一个文本文件中,如zmb.txt,实现该功能。

程序代码如下:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

class Task 
{
    public static void main(String[] args) throws IOException 
    {
        Scanner sc = new Scanner(System.in);//scanner控制窗函数
        while(true) 
        {
            System.out.println("请输入:");
            String str = sc.nextLine();
            FileReader fr = new FileReader("zmb.txt");//创建一个新的 FileReader,给予File读
            BufferedReader br = new BufferedReader(fr);//从一个字符输入流中读取文本
            String s = null;//定义一个字符串
            boolean b = false;//判断字典中是否含有输入的词
            while((s = br.readLine()) != null) //readLine()读一行文本
            {
                String english = s.substring(s.indexOf("<")+1, s.indexOf(">"));//返回指数在这个字符串指定的子字符串中第一个出现的下标
                String china = s.substring(s.lastIndexOf("<")+1,s.lastIndexOf(">"));//返回指数在这个字符串的指定子字符串中最后出现的下标
                if(str.equals(english)) //该词存在
                {
                    System.out.println(china);
                    b = true;
                }
            }
            if(str.equals("ByeBye")) //判断是否输入的为"ByeBye"
            {
                System.out.println("程序退出!");
                System.exit(0);//结束程序
            }
            {
                if(!b) //没有该词
                System.out.println("查不到该词!");
            }
        }
    }
}

zmb.txt文件如下:
这里写图片描述
运行结果如下:

这里写图片描述

方法二:将码表放在SQLite文件中,如zmdb.sqlite,实现该功能。

这里写图片描述
这里写图片描述
程序代码如下:

import java.sql.*;                      
import java.io.IOException;         
import java.util.Scanner;

class SQLiteZMB {
    static Connection con;   
    static Statement sql;    
    static ResultSet res;     
    public Connection getConnection(){   
        try {                             //加载数据库驱动
            Class.forName("org.sqlite.JDBC");
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        try {                 //连接数据库
            con=DriverManager.getConnection("jdbc:sqlite:zmdb.sqlite");
            System.out.println("Succeeded connecting to the Database!");
            System.out.print('\n');
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return con;           //返回con
    }
    public static void main(String[] args)throws IOException {
        Scanner scan = new Scanner(System.in);//scanner控制窗函数
        SQLiteZMB c = new SQLiteZMB();  
        con =c.getConnection();              //与数据库建立连接
        while(true) {
            try {
                    sql = con.createStatement();     //操作数据库
                    res = sql.executeQuery("select * from zmb");//要执行的语句
                    String str = scan.nextLine();  
                    boolean b = false;       //判断字典中是否含有输入的词         
                    while(res.next()) {     
                        String en = res.getString("english");      //获取english字段值
                        String ch = res.getString("chinese");      //获取chinese字段值
                        String english = en.substring(en.indexOf("<")+1,en.indexOf(">")); //返回指数在这个字符串指定的子字符串中第一个出现的下标
                        String chinese = ch.substring(ch.lastIndexOf("<")+1,ch.lastIndexOf(">"));  //返回指数在这个字符串的指定子字符串中最后出现的下标
                        if(str.equals(english)) {      //对应该词存在
                            System.out.println(chinese);                        
                            b = true;              
                        }
                    }
                    if(str.equals("ByeBye")) {   //判断是否输入的为ByeBye 
                        System.exit(0);           //退出程序
                    }
                    if(!b) {                  //查找不到对应的词    
                        System.out.println("查不到该词");    
                    }
             }catch(SQLException e) {
                    e.printStackTrace();
             }
        }
     }
}

运行结果如下:
这里写图片描述

比较以上两种方法,过程不同,但实现的功能相同,发现将码表放在文本文文件中较简单易实现,而数据库则是功能更加完整。

猜你喜欢

转载自blog.csdn.net/weixin_42014622/article/details/80288054
今日推荐