JAVA OOP: help with assigning values to object | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

JAVA OOP: help with assigning values to object

hi (I'm new in Java) I'm missing something in the code and I don't know what it is. I'm trying to read an input and then assign it to an attribute, but I have to use a method to read the input, it can't be in the main. Can anyone tell me what's wrong? Thanks https://code.sololearn.com/cn826HVA0gCm/?ref=app

4th Dec 2021, 2:10 AM
Luz
Luz - avatar
8 Answers
+ 2
Luz Your scanner object is a local variable so you cannot access globally. And also no need to make two scanner objects. You can do with only one scanner object. https://code.sololearn.com/c6rHLQoJi4OT/?ref=app
4th Dec 2021, 2:42 AM
A͢J
A͢J - avatar
+ 3
Luz Setter and getter are methods which can use anywhere through the object of Class Libro.
4th Dec 2021, 3:27 AM
A͢J
A͢J - avatar
4th Dec 2021, 3:54 AM
Luz
Luz - avatar
+ 1
it is OK, as you do it in your actual code public void CargarLibro() { System.out.println("Ingrese ISBN"); setISBN (leer.nextInt()); Other problem is that next() can't read title or author where are more words with spaces because space is default separator used for next()
4th Dec 2021, 9:44 AM
zemiak
+ 1
I try this input format, separated by enter 134685997 Effective Java Joshua Bloch 416
4th Dec 2021, 7:01 PM
zemiak
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ oh okay so I was missing the l1.setISBN(leer.nextInt()); do you know if it's ok to put it inside the method? Because in the assignment it says to create a method that can be used multiple times
4th Dec 2021, 2:58 AM
Luz
Luz - avatar
0
zemiak oh thank you! I didn't noticed that
4th Dec 2021, 4:29 PM
Luz
Luz - avatar
0
zemiak I changed it to nextLine() and now it's throwing an error, do you know what's wrong??
4th Dec 2021, 4:32 PM
Luz
Luz - avatar