首页 > 解决方案 > 如何连接字典文件?

问题描述

我想在用户输入“A”时连接到字典文件我已经制作了菜单,无法通过该方法连接文件,我可以将Read()方法连接到切换流但是Read的表达式的非法开始() 方法来了,包括下面的那个。

我的完整代码是;

public class WordLink
{

   public static void main(String[] args)

   {
      Scanner keyboard = new Scanner(System.in);

      System.out.println("WordLink");
      System.out.println();

      System.out.println("A. set the difficult level");
      System.out.println("B. display the dictionary");
      System.out.println("C. insert a word to the disctionary");
      System.out.println("D. play the game");
      System.out.println("E. exit");
      System.out.println();

      System.out.print("Select a function from the menu: ");
      char choice = keyboard.next().charAt(0);

     void handleInput()
     {
      switch(choice)
      {
         case 'A':
            System.out.println("Option 1");
           Read();
            break;
         case 'B':
            System.out.println("Option 2");
            break;
         case 'C':
            System.out.println("Option 3");
            break;
         default:
            System.out.println("Invailid");
            break;
      }


      public static void String Read() throws IOException
      {
         File text = new File ("dictionary.txt");

         BufferedReader br = new BufferedReader(new FileReader(file));

         System.out.println("Set the difficulty level");
      }

   }

}`

用户输入选项A时的最终输出应该是;(粗体是用户输入)

WordLink

      A set the difficulty level

      B display the dictionary

      C insert a word to the dictionary

      D play the game

      E exit

Select a function from the menu: **A** 

The current difficulty level is 1.

标签: javafileswitch-statement

解决方案


如果文件存在,这应该可以工作

import java.util.*;
import java.io.*;
public class WordLink
{
   public static void main(String[] args)throws Exception
   {

  Scanner keyboard = new Scanner(System.in);
  System.out.println("WordLink");
  System.out.println();

  System.out.println("A. set the difficult level");
  System.out.println("B. display the dictionary");
  System.out.println("C. insert a word to the disctionary");
  System.out.println("D. play the game");
  System.out.println("E. exit");
  System.out.println();

  System.out.print("Select a function from the menu: ");
  char choice = keyboard.next().charAt(0);

  switch(choice)
  {
     case 'A':
        System.out.println("Option 1");
       String content = Read();
       //use it 
        break;
     case 'B':
        System.out.println("Option 2");
        break;
     case 'C':
        System.out.println("Option 3");
        break;
     default:
        System.out.println("Invailid");
        break;
  }
}// main() closed here


  public static String Read() throws IOException//removed void 
  {

    String content = "";
     File text = new File ("dictionary.txt");

     BufferedReader br = new BufferedReader(new FileReader(text));
     // your need to read file here and save into content to use it later

     System.out.println("Set the difficulty level");

     return content;//return a String
  }

   //}// removed this

}// extra character removed here

推荐阅读