java - 如何连接字典文件?
问题描述
我想在用户输入“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.
解决方案
如果文件存在,这应该可以工作
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
推荐阅读
- node.js - 如何使用 Joi 验证我的 request.body 中存在的某些特定字段?
- arrays - 有没有办法从Scala中的给定数组创建n个元素的子数组块?
- java - 找不到符号,符号:方法
- react-native - 世博会开始永远显示“在港口启动 Metro Bundler”
- python-3.x - Python 多处理似乎正在影响程序的结果
- python - 数据帧的平均值
- javascript - How to validate form based on values and submit call out to another page accordingly?
- c++ - 如何在 C++ 中输出分栏符
- bash - 查找内部管道内的 awk 命令
- node.js - 在一个响应中流式传输多个文件 - Node.js