Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SIMPLE JAVA CODING Q: can someone help me add to the bottom code to do the follo

ID: 3801557 • Letter: S

Question

SIMPLE JAVA CODING Q:

can someone help me add to the bottom code to do the following:

i need to read in a .txt file named input.txt. It is a file with words in it and i want to read the words in so that the array named commands that I make in the bottom code has a word as each element (so instead of a, b, c, each element is a word from the file.

public class Worker {
    public static void main(String[] args){
        //read in the file into string array of words
        String[] commands = {"a","b","c"};
        for(int i = 0; i< commands.length; i++){ // Here was mistake //
            if (commands[i] == "a") {
                   System.out.print(commands[i]);
            }
            else if (commands[i] == "b") {
                   System.out.print(commands[i]);
                }
            else if (commands[i] == "c") {
                   System.out.print(commands[i]);
            }
            else{
                System.out.print("none of these was found!");
            }
        }
    }
}

Explanation / Answer

String[] getCommands() {
        ArrayList<String> list = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))){
            String line;
            while((line = br.readLine())!=null){
                list.addAll(Arrays.asList(line.split(" \.\?!")));
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Numbers.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Numbers.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list.toArray(new String[list.size()]);
    }

then commands = getCommands();