Class attributes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

Class attributes

Why does the code is returning to me 0 or Null values??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String firstName = read.nextLine(); String secondName = read.nextLine(); int age = read.nextInt(); int roomNumber = read.nextInt(); Customer customer = new Customer(); //set customer's data to object here customer.saveCustomerInfo(); } } class Customer { //add all necessary attributes here String firstName; String secondName; int age; int roomNumber; public void saveCustomerInfo() { System.out.println("First name: " + firstName); System.out.println("Second name: " + secondName); System.out.println("Age: " + age); System.out.println("Room number: " + roomNumber); } }

29th Apr 2021, 3:40 PM
Remus Tanasa
5 Answers
+ 7
[Page 2] // continue... class Customer { //add all necessary attributes here String firstName; String secondName; int age; int roomNumber; // constructor Customer(String fn, String sn, int a, int rn){ this.firstName = fn; this.secondName = sn; this.age = a; this.roomNumber = rn; } public void saveCustomerInfo() { System.out.println("First name: " + firstName); System.out.println("Second name: " + secondName); System.out.println("Age: " + age); System.out.println("Room number: " + roomNumber); } }
30th Apr 2021, 6:05 PM
Rohit
+ 1
[Page 1] Your class attributes is not getting values. One way is by creating a constructor. This will help you assign values to your class attributes during object creation. INPUT: John Doe 30 123 [CODE] import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String firstName = read.nextLine(); String secondName = read.nextLine(); int age = read.nextInt(); int roomNumber = read.nextInt(); Customer customer = new Customer(firstName, secondName, age, roomNumber); //set customer's data to object here customer.saveCustomerInfo(); } }
29th Apr 2021, 3:47 PM
Rohit
+ 1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String firstName = read.nextLine(); String secondName = read.nextLine(); int age = read.nextInt(); int roomNumber = read.nextInt(); Customer customer = new Customer(); //set customer's data to object here customer.setFirstName(firstName); customer.setSecondName(secondName); customer.setAge(age); customer.setRoomNumber(roomNumber); customer.saveCustomerInfo(); } } class Customer { //add all necessary attributes here String firstName; String secondName; int age; int roomNumber; public void setFirstName (String firstName){ this.firstName = firstName; } public void setSecondName (String secondName){ this.secondName = secondName; } public void setAge (int age){ this.age = age; } public void setRoomNumber (int roomNumber) { this.roomNumber = roomNumber; } public void saveCustomerInfo() { System.out.println("First name: " + firstName); System.out.println("Second name: " + secondName); System.out.println("Age: " + age); System.out.println("Room number: " + roomNumber); } }
21st Jul 2021, 8:56 PM
Кирилл Кулешов
Кирилл Кулешов - avatar
0
i ve tried several times but it doesn't work. Basically, how to assign the object attribute to the input??
29th Apr 2021, 4:34 PM
Remus Tanasa
0
21st Jul 2021, 8:56 PM
Кирилл Кулешов
Кирилл Кулешов - avatar