'Is there a Java function that can read a dictionary file and output the definition when asked for a word in standard input? [duplicate]

I have a file definitions.dict which contains words and their respective definitions. A snippet of the file looks like this:

dictionary ? a book or electronic resource that gives a list of the words of a language in alphabetical order and explains what they mean

word ? a single unit of language that means something and can be spoken or written

computer ? an electronic machine that can store, organise and find information, do processes with numbers and other data, and control other machines

I'm trying to write Java code, that takes the word as standard input and outputs the definition of said word. The code I've written so far fails to even compile, giving the error:

Dictionary.java:25: error: array required, but String found
                if(searchword.equals(line[i][0])){
                                            ^
Dictionary.java:26: error: array required, but String found
                    System.out.println(line[i][1]);
                                              ^

Here is the code I have so far:

import java.util.*;
import java.io.*;

public class Dictionary {
    
    public static void main(String[] args) throws FileNotFoundException {

        String searchword = "";
        String[] line = {};
        int i;

        Scanner scanword = new Scanner(System.in);
        if(scanword.hasNextLine()){
            searchword = scanword.nextLine();
        }
        try{
            Scanner scan = new Scanner(new File("sample.dict"));
            while (scan.hasNextLine()){
                line = Arrays.copyOf(line, line.length + 1);
                line[line.length-1] = scan.nextLine();
            }
            
            for(i = 0; i < line.length; i++) {
                line[i].split("\\ ? ");
                if(searchword.equals(line[i][0])){
                    System.out.println(line[i][1]);
            }
                
            }
        }
        catch (FileNotFoundException ex) {
            ;
        }
    }
}

I will admit that this is part of an assignment which I've been struggling with for a while and I am asking here as a last resort. (Note that I am only allowed to work with arrays and no other data structure.)



Solution 1:[1]

Slighlty different approach but what I did was actually create a dictionary object with two parameters:

public String term; 
public String definition; 

Then had a method to load dictionary objects into a Dictionary[] array from my sample.dict.

So then all I had to do in my main method was something like:

    Dictionary[] definitions = loadDefinitions(f);
    while (scan.hasNext()){
        String userInput = scan.next();
        for (int i = 0; i<definitions.length-1;i++){
            if(userInput.contentEquals(definitions[i].term)){
                System.out.println(definitions[i].definition);

Hope that logic kinda makes sense. Good luck on the task! (Have a look at the athletes tutorial example :) )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mia