Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:42:10+00:00 2026-06-13T17:42:10+00:00

I’m trying to write a simple calculator with addition, subtraction, etc. My problem is

  • 0

I’m trying to write a simple calculator with addition, subtraction, etc.

My problem is with getting the user input. How do I turn the string of numerical values into a vector? And also what is a better way to write the program?

(ns scalc.core)

(defn add
  [numbers]
  (println (apply + numbers)))

(defn numchoose
  []
  (println "What numbers?: ")
  (let [numbers (read-line)] numbers))

(defn opchoose
  []
  (println "What operation would you like to do?: ")
  (let [operation (read-line)]

    (if (= operation "add")
      (do
        (println "You chose to add.")
        (let [numvect (numchoose)]
              (add [numvect]))))))


(defn -main
  [& args]

  (opchoose)
  (numchoose))

And this is the error:

 ~/clj/scalc 1/7 % lein trampoline run -m scalc.core
What operation would you like to do?: 
add
You chose to add.
What numbers?: 
5 7
Exception in thread "main" java.lang.ClassCastException: Cannot cast java.lang.String to java.lang.Number
        at java.lang.Class.cast(Class.java:3005)
        at clojure.core$cast.invoke(core.clj:318)
        at clojure.core$_PLUS_.invoke(core.clj:927)
        at clojure.lang.AFn.applyToHelper(AFn.java:161)
        at clojure.lang.RestFn.applyTo(RestFn.java:132)
        at clojure.core$apply.invoke(core.clj:601)
        at scalc.core$add.invoke(core.clj:5)
        at scalc.core$opchoose.invoke(core.clj:21)
        at scalc.core$_main.doInvoke(core.clj:27)
        at clojure.lang.RestFn.invoke(RestFn.java:397)
        at clojure.lang.Var.invoke(Var.java:411)
        at user$eval15.invoke(NO_SOURCE_FILE:1)
        at clojure.lang.Compiler.eval(Compiler.java:6511)
        at clojure.lang.Compiler.eval(Compiler.java:6501)
        at clojure.lang.Compiler.eval(Compiler.java:6477)
        at clojure.core$eval.invoke(core.clj:2797)
        at clojure.main$eval_opt.invoke(main.clj:297)
        at clojure.main$initialize.invoke(main.clj:316)
        at clojure.main$null_opt.invoke(main.clj:349)
        at clojure.main$main.doInvoke(main.clj:427)
        at clojure.lang.RestFn.invoke(RestFn.java:421)
        at clojure.lang.Var.invoke(Var.java:419)
        at clojure.lang.AFn.applyToHelper(AFn.java:163)
        at clojure.lang.Var.applyTo(Var.java:532)
        at clojure.main.main(main.java:37)

EDIT: the solved program now looks like this:

(ns scalc.core)

(defn add [numbers]
  (reduce + numbers))

(defn numchoose []
  (let [nums (re-seq #"\d+" (read-line))]
    (map #(Integer/parseInt %) nums)))

(defn delegate []
  (println "What operation would you like to do?: ")
  (let [operation (read-line)]

    (when (= operation "add")
      (println "You chose to add.")
      (println "What numbers? ")
      (add (numchoose)))))

(defn -main
  [& args]

  (delegate))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:42:12+00:00Added an answer on June 13, 2026 at 5:42 pm

    For getting the numbers, you can use re-seq:

    (re-seq #"\d+" "123 456 789") => ("123" "456" 789")
    

    You still only have strings rather than numbers though. You can use read-string to get the numbers (read-string is convenient, but not safe in all cases. Here we make sure there are really only numbers in these strings so it’s fine).

    (read-string "5") => 5
    

    Instead of (apply + numbers) you could use reduce: (reduce + numbers), also your add function really shouldn’t print anything (you should try to separate functional functions from side-effecty functions whenever possible).

    This (let [numbers (read-line)] numbers) is equal to (read-line). Don’t overcomplicate things!

    Instead of

    (if (= operation "add")
          (do ... ))
    

    you can write

    (when (= operation "add")
         ...)
    

    when is just a macro that’s useful when you don’t need the else case in your ifs (it wraps everything after the condition in a do, and evaluates to nil when the condition evaluates to false).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.