1

I want to ask the user for inputs using System.out.println(). Like, System.out.println("Enter name"). But, that would produce side-effects. Is there any way to bypass that?

        System.out.println("Enter name: ");
        String customerName = sc.next();
        System.out.println("Enter contact no: ");
        String contactNo = sc.next();
        accounts = (List<Account>) UserService.CreateAccount
                .apply(accounts, new Account(accounts.size() + 1, customerName, contactNo)).get();
        System.out.println("Created account with Number " + accounts.size() + "\n\n");
awesomemypro
  • 531
  • 1
  • 11
  • 32
  • your question is unclear to me – Syed Mehtab Hassan Apr 12 '19 at 12:00
  • Hi, and welcome to stackoverflow.com! Unfortunately, it's unclear what you are asking. Why do you want to avoid side effects (you can't)? Are you asking about how to handle side effects in functional programming? Please edit to clarify. – sleske Apr 12 '19 at 12:00
  • 3
    @SyedMehtabHassan - Displaying output could be considered a side effect. In FP, functions are (largely) meant to be pure, meaning they have no side effects. – T.J. Crowder Apr 12 '19 at 12:00
  • The other question does not give solutions to my question. @sleske – awesomemypro Apr 12 '19 at 12:02
  • Then, how can I ask for inputs? Is there an FP approach for that? – awesomemypro Apr 12 '19 at 12:02
  • 2
    @awesomemypro, consuming external input is *also* a side effect. A pure function cannot do it. You have to allow yourself some deviation from pure FP if you want your program to interact with its environment, and in particular, if you want it to perform I/O. – John Bollinger Apr 12 '19 at 12:06
  • Related: https://stackoverflow.com/questions/29154405/how-to-build-a-java-8-stream-from-system-in-system-console – kutschkem Apr 12 '19 at 12:11

0 Answers0