有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Java将用户输入与文件内容进行比较

我想写一个java代码,它可以读取一个包含许多句子的文件-

Hey how you doing?
Hi I am fine.
Hello World, Good Morning!

要求用户输入句子的第一个单词作为输入,输出应该是句子的其余部分。例如,如果输入字是“Hi”,那么输出应该是“我很好”下面是我的代码,我不知道怎么了!真的需要你们的帮助!谢谢

import java.io.*;  
import java.util.*;  
import java.io.FileReader;
import java.io.BufferedReader;

public class FileContents {  
  public static void main(String args[]) {   
    BufferedReader br=new BufferedReader(new FileReader("myfile.txt"));  
    Vector lineArray=new Vector();  
    String lineContents=null;  
    int counter=0,i; 

    try {  
      while ((lineContents=br.readLine())!=null) {  
        lineArray.add(lineContents);  
        counter++;  
      }  
    } catch (FileNotFoundException fne) {  
      fne.printStackTrace();  
    } catch (IOException io) {  
      io.printStackTrace();  
    }  

    Scanner input = new Scanner(System.in);

    int no=3;
    String[] textData=new String[no];
    for (i=0;i<no;i++) {
      textData[i]=br.readLine();
    }
    br.close();

    System.out.println("These are the file contents : ");
    for (i=0;i<lineArray.size();i++) {  
      System.out.println(lineArray.get(i));  
    }  


    System.out.println("\n Enter first word of sentence : ");
    String st = input.nextLine();
    String[] word= st.split(" ");

    System.out.println("\n Rest of the sentence is  :  ");

    for (i=0;i<lineArray.size();i++) {  
      if (word[i].equals(textData[i])) {
        while (word[i]!='\n')
          System.out.println(word[i]);
      }
    }
  }
}

我是初学者,请原谅我的错误

以上代码的输出-无!!没有错误,但也没有输出


共 (2) 个答案

  1. # 1 楼答案

    我无法理解您在上述代码中试图做什么(可能是我的大脑因睡眠不足而无法正常工作),但您可以执行以下操作:

    1. 获取用户输入并将其存储在String input
    2. 像上面那样逐行读取文件,但使用StringTokenizer并标记每一行
    3. 只需将第一个标记与输入进行比较,看看它们是否匹配
    4. 如果是,打印行;退出,否则进入下一行
  2. # 2 楼答案

    您似乎已经下载了输入,使用scanner类只需添加一个HashMap即可

    http://docs.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html

    类似于

    更新

    HashMap responseMap = new HashMap();
    
    public string genResponse(String word) {
       String response = (String) responseMap.get(word);
       if (response != null){
          return response;
       } else {
          return "Word unrecognized!" 
       }
    }
    

    你需要用你的回答填充你的回答图

    更新2

    这就是我将如何重新组织你们的课堂,我省略了代码,因为这是你们的家庭作业

    public class FileContents {
    
        public HashMap responseMap = new HashMap();
        public Vector lineArray = new Vector();
    
        public static void main(String args[]){
            //Call function to populate the map
            populateResponseMap();
            //Read the lines        
            readLines();
        }
    
        public void populateResponseMap(){
            //This is where you would add responses, something like
            responseMap.put("Hi", "I am fine");
        }
    
        public void readLines() {
    
            //This is where you would read the data from the file using you scanner class
    
            //With each line you would call this function 
            genResponse(//first word in line );
        }
    
        public String genResponse(String word) {
            String response = (String) responseMap.get(word);
            if (response != null) {
                return response;
            } else {
                return "Word unrecognized!";
            }
        }
    }
    

    希望这有帮助