How to take in user input and write it to a text file in java using commands?

Viewed 9

this was a program I made to take in commands, and using those commands, take in user input and write it to a file however when I enter the commands, the program just ends, how comes?

import java.io.*;
import java.util.Scanner;
public class Anka {
    public static void main(String[] anka) {
        String participant_details =null;
        String product_details =null;
        String command =null;

        //creating scanner object to take in user input
        Scanner input = new Scanner(System.in);
        System.out.println("ANKA BUSINESS SUPPORT SYSTEM");
        System.out.print("Enter a command: ");
        System.out.println("Register name password product date_of_birth");
        System.out.println("Post_product product_name \"description\"");
        System.out.println("Performance");
        System.out.println();
        command = input.nextLine();

        //creating file using filewriter package
        try {
            FileWriter writer = new FileWriter("anka.txt");

            //writing data into file
            if (command == "Register") {
                System.out.print("Register");
                participant_details = input.next();
                writer.write(participant_details);

                writer.close();
            }
            else if (command == "Post_product") {
                System.out.print("Post_product");
                product_details = input.next();
                writer.write(product_details);

                writer.close();
            }
        }
        catch (IOException e) {
            System.out.println("error");
        }
        if (command == "Performance") {

            try {
                FileReader reader = new FileReader("anka.txt");
                int data = reader.read();
                while (data != -1) {
                    System.out.print((char) data);
                    data = reader.read();
                }
                reader.close();
            } catch (FileNotFoundException e) {
                System.out.println("File not found");
            } catch (IOException e) {
                System.out.println("No content");
            }
        }
        else if (command == "") {
            System.out.println("Enter a command.");
        }
    }
}

Whenever I try to run the program by entering a command, it just ends the whole program, so I was wondering where the issue was exactly.

0 Answers
Related