# Need help for below Lab code-1

Write a code to generate a list of integers from `1` to `input` . Define a function `add` which adds 5 to an integer. Using the ‘for loop’, print `add(number)` for each number in the list as `value : number` . Write a code to print the `name : age` for each person on the list `personList` using the `for` expression.

object ForComprehensions2 extends App {
val input : Int = args(0).toInt
list = List (1 <- 1 to Input)
def add(number : Int) { for (i <- list to input)
yield i+5 }
for (i <- 1 to input)
case class Person(val name :String, val age : Int){}
val personList :List[Person] = List(Person(“Robert”, 56), Person(“Chris”, 48), Person(“Benedict”, 45), Person(“Peter”, 47))
for (i<- 1 to 4)
println(personList(i))
}

Tried as above but its not compiling;

expectation is while running with Input as 3 it should print below

value : 6
value : 7
value : 8
Robert : 56
Chris : 48
Benedict : 45
Peter : 47

You have several syntax errors, as well a mixing some concepts.

I would recommend you to step back. Try to solve each of the underlying parts of the problem one by one instead of trying to do everything at once.

Yes I am trying and going through the online material as well but i need to complete the above lab without that i am not able to proceed, so please help with the code for the requirement. Any help is much appreciated.
Thank you

The only way to succeed in Computer Science is never to go to bed until the homework is done!

True, but its not my home work its a lab assignment which is stopping me to move forward so any help with code on the requirement will be helpfull

Let me assume that you want to learn (rather than “move forward” by others doing your assignment). I would recommend the following:

• Run stuff in a REPL - ideally ammonite. Then you will know what is wrong (in this case plenty of things).
• Solve things one step at a time, for instance generate the list first before adding numbers to it.
• As you must have been told several times already, please enclose code in triple backticks when you post questions here (read Markdown documentation).
• And obviously, read the docs and make an honest effort before asking.

regards,
Siddhartha

2 Likes

I am also doing the same one.If I figure out will let you know.

problem is i need to complete everything in couple of days, there are so many labs, few i cracked it few m struck so looking for experts advise, definitely i’ll keep on going through material once the lab completed. so any help with code will be highly appreciated.

At the stage of learning in which you are, the less help you receive the better. All those labs are there for testing and enduring your skills (working under pressure being one).

As we have told you, you even have (basic) syntax errors.
So it seems you already have gaps, giving you the solution would only give you more gaps.

I would recommend you to ask this questions in the gitter channel, interactive help would be better. Also, please try to solve your problems by yourself first. Break things down, solve each step one by one.

One debugs a program one line at a time. Type in the first 2 lines and a println statement:
object ForComprehensions2 extends App {
val input : Int = args(0).toInt
println("input: " + input)
}
If the input is not correct, then fix it. If it is correct then delete the println, type in the third line and a new println statement:
object ForComprehensions2 extends App {
val input : Int = args(0).toInt
list = List (1 <- 1 to Input)
println("list: ", + list)
}
This should not compile because list must be defined as a var or a val. So fix it until the list is printed. Then continue on line by line. As you become more familiar with the language, this stuff will become familiar and automatic with you. If you are using an IDE (Integrated Development Environment) like Eclipse (free) or IntelliJ IDEA (expensive) then the IDE allows you to put in breakpoints (traps) which will stop the program and permit you to look at the values of variables with the mouse. It is much faster, but the basic idea of building a program a section or line at a time is the same.

Best of luck.