0

32.1 need help

i know it shows the answer i cant see what i am doing wrong import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String name = read.nextLine(); int age = read.nextInt(); Student student = new Student(); student.name = name; student.setAge(age); System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; private int age; public int getAge() { return age; } public void setAge(int age) { if(age< 0){ System.out.println("Invalid age"); } }

19th Dec 2022, 5:04 PM
Alex Williams
Alex Williams - avatar
1 Answer
+ 4
Shouldn't setAge() set the age? Please put your code in a script on sololearn playground, so it is easier to read and people can test the code.
19th Dec 2022, 5:24 PM
Lisa
Lisa - avatar