What's the best way for my mini proje? (tuple or list)

Hello everyone,

I’m working on learn the Scala3 but I’m not sure which way is fix for my program. Can someone explain basicly and give me some advice for my project.

Problem: I want to save my log for if a user want to see old info so I’ll put a section 4 in my promth then user can show logs.

@main
def main() = {

userui()

}

def userui(): Unit = {
var continue = true
while (continue) {
println(“*************************\n" +
"
Weather Calculation **********\n” +
" 1. Fahrenheit \n" +
" 2. Celsius \n" +
" 3. Exit \n" +
" =========================================" )

val selection = readIntSafely("Please choose your options: ")

if (selection == 3) {
  continue = false
} else if (selection == 1) {
  val input = readIntSafely("Please enter Celsius: ")
  val f = input * 9 / 5 + 32
  println("Fahrenheit is: " + f)

  fahrenheit :+ (unput, f)

} else if (selection == 2) {
  val input = readIntSafely("Please enter Fahrenheit: ")
  val c = (input - 32) * 5 / 9
  println("Celsius is: " + c)
} else {
  println("Invalid selection. Please enter 1, 2, or, 3")
}

}
}

def logHistory() = {
val fahrenheit = list()
val celsiusLog = list()

}

def readIntSafely(prompt: String): Int = {
var continue = true
var result = 0
while (continue) {
try {
print(prompt)
result = scala.io.StdIn.readInt()
continue = false
} catch {
case e: NumberFormatException =>
println(“Invalid input. Please enter a valid integer.”)
}
}
result
}

Welcome to the Scala community, @dahbest

Your code formatting seems broken and it’s hard to read due to lack of indentation. Could you post your code on Scastie, then share a link to it here? That way we can help much more easily.

As for Scala 3, your code seems full of Scala 2 idioms and imperative idioms. You might be using a poor resource. What resource are you using to learn Scala 3? I recommend using a proper official source, these online courses or these books.

1 Like

Hello, accually I\m searching every website because I don’t want to use chatgpt :smiley: Thank you for books, I’ll check right now.

heres my codes" https://scastie.scala-lang.org/slxfHQHhSqqaQFyR0JHKPg.js

Your Scastie link has an extra .js at the end, here’s the correct link: Scastie - An interactive playground for Scala.

Not using ChatGPT is good! :+1: But searching every website is not. Especially beware of fake courses and Scala scams.

2 Likes

thank you so much for replay, I’ll check the new codes then try to imporve my code. If I have any question too, can I add here?

1 Like

Sure, but you might find Scala Discord more convenient, since you can keep a long conversation going on there, with live, fast responses. It’s quite active there, many people can give you lots of tips and pointers.

I rewrote the code in a more Scala 3 idiomatic style: Scastie - An interactive playground for Scala.

I’ll join discord too.

but there’s problem in our code.

Yes I know. I didn’t fix it because I don’t quite understand your intention. I think you meant to declare a list to keep the logs, but that’s not inside the main function…

It’s a bit hard to describe here, but from your code, your thinking seems to be very imperative, you’ll have to think about refactoring this code in a significant way, but first you have to learn Scala basics more deeply, and Scala’s emphasis on immutable values (instead of carrying your habits from other languages into Scala). I’d recommend putting this code aside for a few weeks, and taking courses / reading books.

1 Like

hmm… thank you for advice.

Accually my intention is calculate something then save logs in a list (right now), then I’ll write the list or tuple which is good I don’t know right now, then insert to postgres database. because I’m data engineering intern, I don’t want to be a new programmer. :slight_smile: I want to learn real program suck as Scala :smiley:

by the way I fall in love with your scala3 code :smiley: Thank you so much. I’ll join discord too. Thank you.

1 Like