简体   繁体   中英

How do I store a string into a new String[] that has no name in a hashmap?

I am doing a Q and A AI type thing and am trying to save its data into a txt file so that it keeps the questions from the last session, kind of like cleverbot. Is there an easier way of doing this? Or is there a way that I can store string into the new String[]?

import java.lang.Math.*;
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.*;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.BufferedWriter;

public class QandA{

    public static void main(String[] args)throws Exception{

        String UE, OUE, a;
        int i;
        String[] Why = new String[]{"Because.", "Just Because.", "Why yourself."};
        Map<String, String[]> Questions = new HashMap<>();
        Questions.put("Why", Why);
        Scanner k = new Scanner(System.in);
        File f = new File("QandA_Data.txt");

        while (true){
            if (f.exists() && f.length() > 0){
                Scanner input = new Scanner(f);
                for (int c = 0; c < f.length(); c++){
                    a = input.nextLine();
                    Questions.put(a, new String[3]);//<====// This new string
                    for (int v = 0; v < 3; v++){           //
                        a = input.nextLine();              //
                        //I want to store a value here into//
                    }
                }
            }
            System.out.println(" Ask a question! ");
            UE = k.nextLine();
            if(Questions.keySet().stream().filter(UE::equalsIgnoreCase).findFirst().isPresent()) {
                i = (int) Math.floor(Math.random()*3);
                System.out.println(Questions.get(UE)[i]);
            } else {
                System.out.println(" Question Not Found. Enter 3 answers to the question: ");
                OUE = k.nextLine();
                Questions.put(UE, new String[3]);
                Questions.get(UE)[0] = OUE;
                OUE = k.nextLine();
                Questions.get(UE)[1] = OUE;
                OUE = k.nextLine();
                Questions.get(UE)[2] = OUE;
                PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 
                for (int c = 1; c < Questions.size(); c++) {
                    out.println(UE);
                    for (int v = 0; v < Questions.get(UE)[c].length(); v++){
                        out.println(Questions.get(UE)[c]);
                    }
                }
                out.close();
            }  
        }
    }
}

There are many ways you could do this. Here is how you could do it in one line, using the array initialization syntax:

Questions.put(a, new String[]{input.nextLine(),
    input.nextLine(),
    input.nextLine()});

But this of course raises the question, how do we know there will always be three lines available and that they all are to be associated with this same key? Might there ever be more or less? Those sort of considerations could lead to a more robust and flexible approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM