用Java编写程序,从用户处读取多个字符串并与文本文件进行比较
我正在尝试编写一个程序,该程序将接受用户输入(一条由字符组成的长消息),存储消息并搜索文本文件,以查看这些单词是否出现在文本文件中。我遇到的问题是,我只能读取消息的第一个字符串,并将其与文本文件进行比较。例如,如果我输入“学习”;如果文本文件中有一个单词,我将得到一个结果,显示在该文件中找到的是。但是,如果我键入“learning is”,它仍然只会将learning作为文件中的一个单词返回,即使“is”也是文本文件中的一个单词。我的程序似乎无法读取空白空间。所以我想我的问题是,我该如何扩充我的程序来做到这一点,并读取文件中的每个单词?我的程序是否也可以读取用户原始消息中的每个单词(带空格或不带空格),并将其与文本文件进行比较 多谢各位用Java编写程序,从用户处读取多个字符串并与文本文件进行比较,java,java.util.scanner,Java,Java.util.scanner,我正在尝试编写一个程序,该程序将接受用户输入(一条由字符组成的长消息),存储消息并搜索文本文件,以查看这些单词是否出现在文本文件中。我遇到的问题是,我只能读取消息的第一个字符串,并将其与文本文件进行比较。例如,如果我输入“学习”;如果文本文件中有一个单词,我将得到一个结果,显示在该文件中找到的是。但是,如果我键入“learning is”,它仍然只会将learning作为文件中的一个单词返回,即使“is”也是文本文件中的一个单词。我的程序似乎无法读取空白空间。所以我想我的问题是,我该如何扩充我的
import java.io.*;
import java.util.Scanner;
public class Affine_English2
{
public static void main(String[] args) throws IOException
{
String message = "";
String name = "";
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.println("Please enter in a message: ");
message = scan.next();
Scanner file = new Scanner(new File("example.txt"));
while(file.hasNextLine())
{
String line = file.nextLine();
for(int i = 0; i < message.length(); i++)
{
if(line.indexOf(message) != -1)
{
System.out.println(message + " is an English word ");
break;
}
}
}
}
}
import java.io.*;
导入java.util.Scanner;
公共类仿射英语2
{
公共静态void main(字符串[]args)引发IOException
{
字符串消息=”;
字符串名称=”;
扫描仪输入=新扫描仪(System.in);
扫描仪扫描=新扫描仪(System.in);
System.out.println(“请输入消息:”);
message=scan.next();
扫描仪文件=新扫描仪(新文件(“example.txt”);
while(file.hasNextLine())
{
String line=file.nextLine();
对于(int i=0;i
您可以试试这个。使用此解决方案,您可以在example.txt文件中找到用户输入的每个单词:
public static void main(String[] args) throws IOException
{
String message = "";
String name = "";
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.println("Please enter in a message: ");
message = scan.nextLine();
Scanner file = new Scanner(new File("example.txt"));
while (file.hasNextLine())
{
String line = file.nextLine();
for (String word : message.split(" "))
{
if (line.contains(word))
{
System.out.println(word + " is an English word ");
}
}
}
}
你可以试试这个。使用此解决方案,您可以在example.txt文件中找到用户输入的每个单词:
public static void main(String[] args) throws IOException
{
String message = "";
String name = "";
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.println("Please enter in a message: ");
message = scan.nextLine();
Scanner file = new Scanner(new File("example.txt"));
while (file.hasNextLine())
{
String line = file.nextLine();
for (String word : message.split(" "))
{
if (line.contains(word))
{
System.out.println(word + " is an English word ");
}
}
}
}
正如马克在评论中指出的,改变
scan.next();
致:
应该行得通,我试着为我工作。正如马克在评论中指出的,改变
scan.next();
致:
如果你能使用Java8和Streams API的话,我试过并为我工作。如果你能使用Java8和Streams API的话
public static void main(String[] args) throws Exception{ // You need to handle this exception
String message = "";
Scanner input = new Scanner(System.in);
System.out.println("Please enter in a message: ");
message = input.nextLine();
List<String> messageParts = Arrays.stream(message.split(" ")).collect(Collectors.toList());
BufferedReader reader = new BufferedReader(new FileReader("example.txt"));
reader.lines()
.filter( line -> !messageParts.contains(line))
.forEach(System.out::println);
}
publicstaticvoidmain(String[]args)引发异常{//您需要处理此异常
字符串消息=”;
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入消息:”);
message=input.nextLine();
List messageParts=Arrays.stream(message.split(“”).collect(Collectors.toList());
BufferedReader=newBufferedReader(newFileReader(“example.txt”);
reader.lines()
.filter(行->!messageParts.contains(行))
.forEach(System.out::println);
}
如果您可以使用Java 8和Streams API
public static void main(String[] args) throws Exception{ // You need to handle this exception
String message = "";
Scanner input = new Scanner(System.in);
System.out.println("Please enter in a message: ");
message = input.nextLine();
List<String> messageParts = Arrays.stream(message.split(" ")).collect(Collectors.toList());
BufferedReader reader = new BufferedReader(new FileReader("example.txt"));
reader.lines()
.filter( line -> !messageParts.contains(line))
.forEach(System.out::println);
}
publicstaticvoidmain(String[]args)引发异常{//您需要处理此异常
字符串消息=”;
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入消息:”);
message=input.nextLine();
List messageParts=Arrays.stream(message.split(“”).collect(Collectors.toList());
BufferedReader=newBufferedReader(newFileReader(“example.txt”);
reader.lines()
.filter(行->!messageParts.contains(行))
.forEach(System.out::println);
}
我建议您首先处理该文件并构建一组合法英语单词:
public static void main(String[] args) throws IOException {
Set<String> legalEnglishWords = new HashSet<String>();
Scanner file = new Scanner(new File("example.txt"));
while (file.hasNextLine()) {
String line = file.nextLine();
for (String word : line.split(" ")) {
legalEnglishWords.add(word);
}
}
file.close();
最后,将用户的输入拆分为令牌,并检查每个令牌是否为合法单词:
for (String userToken : message.split(" ")) {
if (legalEnglishWords.contains(userToken)) {
System.out.println(userToken + " is an English word ");
}
}
}
}
我建议您首先处理该文件并构建一组法律英语单词:
public static void main(String[] args) throws IOException {
Set<String> legalEnglishWords = new HashSet<String>();
Scanner file = new Scanner(new File("example.txt"));
while (file.hasNextLine()) {
String line = file.nextLine();
for (String word : line.split(" ")) {
legalEnglishWords.add(word);
}
}
file.close();
最后,将用户的输入拆分为令牌,并检查每个令牌是否为合法单词:
for (String userToken : message.split(" ")) {
if (legalEnglishWords.contains(userToken)) {
System.out.println(userToken + " is an English word ");
}
}
}
}
您有很多解决方案,但在查找匹配项时,我建议您查看模式和匹配器并使用正则表达式 我还没有完全理解您的问题,但您可以添加类似的内容(我没有测试代码,但这个想法应该可以正常工作):
您有很多解决方案,但在查找匹配项时,我建议您查看模式和匹配器并使用正则表达式 我还没有完全理解您的问题,但您可以添加类似的内容(我没有测试代码,但这个想法应该可以正常工作):
Try message=scan.nextLine();而不是message=scan.next();如果要搜索一次输入的不同单词,则需要一个分隔符,可以拆分每个字符,但它应该是唯一的,并且不应该出现在某个单词中的某个位置,例如字符
t
,例如message=scan.nextLine();而不是message=scan.next();如果要搜索一次输入的不同单词,则需要一个分隔符,可以拆分每个字符,但它应该是唯一的,并且不应该出现在某个单词的某个位置,例如字符t