嗨!
我的 Clojure 项目中有2个文件。
第一个(db.clj)从txt文件中读取数据并将其存储在向量列表中,第二个(menu.clj)显示包含多个选项的菜单。
我的条件是工作的:表格显示得如我预期,但是我最后遇到了一个奇怪的错误,导致我的应用停止运行。
详细信息
db.clj
(ns db)
(defn load-data
[filename]
(with-open [rdr (io/reader filename)]
;; https://docs.clojure.org/clojure.core/doall
(doall
(for [line (line-seq rdr)
:let [[id & data] (clojure.string/split line #"\|")]]
[(Integer/parseInt id)
data]))))
menu.clj
(ns menu
(:require [db])
)
(def customers (db/load-data "cust.txt"))
(def products (db/load-data "prod.txt"))
(def input_ 0) ; initialize input
(defn menu
[]
(if (not= input_ "6")
(do
(newline) ; insert a new line
(println " ** MENU ** ") ; display menu
(println "
1. Display Customer Table
2. Display Product Table
3. Display Sales Table
4. Total Sales for Customer
5. Total Count for Product
6. Exit")
(println "Enter an option > ")
(flush)
(def input_ (read-line)) ; put the input in input_ variable
(cond
(= input_ "1")
((println "Table of customers: ")
; run >> every item in a newline
(run! println (db/load-data "cust.txt")))
(= input_ "2")
((println "Table of products: ")
; run >> every item in a newline
(run! println (db/load-data "prod.txt")))
:else (println "Option not handled!")
)
;(println (str "The option entered is: \"" input_ "\""))
(menu))
(println "Goodbye!") ; If user prompts 6
))
控制台
** MENU **
1. Display Customer Table
2. Display Product Table
3. Display Sales Table
4. Total Sales for Customer
5. Total Count for Product
6. Exit
Enter an option >
2
Table of products:
[1 (shoes 14.96)]
[2 (milk 1.98)]
[3 (jam 2.99)]
[4 (gum 1.25)]
[5 (eggs 2.98)]
[6 (jacket 42.99)]
Syntax error (NullPointerException) compiling at (menu.clj:42:1).
null
Full report at:
/tmp/clojure-4521524603536338044.edn
谢谢!