0

Can someone help me? why my code is not working?

I spend long time on trying to solve it myself but its still not working. Where is the mistake? help me pls... https://code.sololearn.com/ca119A99a56a/#java the instructions: The program you are given receives name and age of student as input. Complete the program to set the values for the corresponding attributes of the Student class and prints out the final result. If the age is <0, program should output "Invalid age" and assign a 0 value to the age attribute. Sample Input Olivia -2 Sample Output Invalid age Name: Olivia Age: 0

21st Apr 2021, 3:46 PM
Maja Jasinska
21 Answers
21st Apr 2021, 4:38 PM
A͢J
A͢J - avatar
+ 9
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); //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U if(age < 0){ System.out.println("Invalid age"); System.out.println("Name: " + student.name); System.out.println("Age: " + 0); }else{ System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } } class Student { public String name; public int age; public int getAge() { return this.age; //this keyword } public void setAge(int age) { this.age = age; } }
27th Jun 2022, 12:09 PM
Fazal Haroon
Fazal Haroon - avatar
+ 6
Try this one 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; //set the age via Setter student.setAge(age); if(age < 0){ student.setAge(0); System.out.println("Invalid age"); } System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; private int age; public int getAge() { //complete Getter return age; } public void setAge(int age) { //complete Setter this.age = age; } }
27th Jun 2022, 11:32 AM
Stanley K Ludovick
Stanley K Ludovick - avatar
+ 3
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); System.out.println("enter name"); String name = read.next(); System.out.println("enter age"); int age = read.nextInt(); Student student = new Student(); student.setAge(age); student.setName(name); if(student.getAge() <= 0){ System.out.println("Invalid Age"); }else { System.out.println("Name: "+student.getName()); System.out.println("Age: "+student.getAge()); } } } class Student{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
27th Jun 2022, 11:57 AM
Fazal Haroon
Fazal Haroon - avatar
+ 3
Yes, you are good to go 👍 , Bravo 👏 But in my opinion I think you should set the age to 0 through setter and output it via getter. But all in all you succeeded even without that approach.
27th Jun 2022, 12:13 PM
Stanley K Ludovick
Stanley K Ludovick - avatar
+ 2
You need to put the checking in setAge() In your current flow, setAge() is called before getAge(), and with input of -2, setAge() updates the age to 0, then in getAge(), the INVALID AGE condition will never be true, because age have been corrected to 0. See the logic?
21st Apr 2021, 3:53 PM
Gordon
Gordon - avatar
+ 2
Try this code 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; //set the age via Setter student.setAge(age); if (age < 0){ System.out.println("Invalid age"); } System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; private int age; public int getAge() { //complete Getter if (age < 0){ return age = 0; }else{ return age; } } public void setAge(int age) { //complete Setter if (this.age < 0){ this.age = 0; } else { this.age = age; } } } its working fine ....
29th May 2021, 7:23 AM
Abhishek Biswas
Abhishek Biswas - avatar
+ 2
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); //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U if(age < 0){ System.out.println("Invalid age"); System.out.println("Name: " + student.name); System.out.println("Age: " + 0); }else{ System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } } class Student { public String name; public int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
4th Sep 2021, 7:02 AM
Fazal Haroon
Fazal Haroon - avatar
+ 2
Thanks
27th Jun 2022, 12:19 PM
Fazal Haroon
Fazal Haroon - avatar
+ 1
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); //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U if(age < 0){ System.out.println("Invalid age"); System.out.println("Name: " + student.name); System.out.println("Age: " + 0); }else{ System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } } class Student { public String name; public int age; public int getAge() { return this.age; //this keyword } public void setAge(int age) { this.age = age; } }
4th Sep 2021, 7:04 AM
Fazal Haroon
Fazal Haroon - avatar
+ 1
I don't know if the above code working for you 🤔
27th Jun 2022, 12:01 PM
Stanley K Ludovick
Stanley K Ludovick - avatar
+ 1
Can you try my code in your system? The getter and setter concept must fulfill the encapsulation concept and never try to access data directly
27th Jun 2022, 12:03 PM
Fazal Haroon
Fazal Haroon - avatar
+ 1
The getter and setter concept is good, but according the the question you supposed to output your results as they recommend. Try my solution above 👆
27th Jun 2022, 12:07 PM
Stanley K Ludovick
Stanley K Ludovick - avatar
0
@Gordon thank you for your reply! I see it now :) however I still don't get the output at all... no mater if I use age -2 or for example 10. Is there any other mistake or my correction is wrong?
21st Apr 2021, 4:34 PM
Maja Jasinska
0
@Gordon @AJ Thanks so much!! I apreciate you taking time to reply to my question!
21st Apr 2021, 4:41 PM
Maja Jasinska
0
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; //set the age via Setter student.setAge(age); if(age < 0){ System.out.println("Invalid age"); System.out.println("Name: " + student.name); System.out.println("Age: " + 0); }else{ System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } } class Student { public String name; private int age; public int getAge() { //complete Getter return this.age; } public void setAge(int age) { //complete Setter this.age=age; } } This should work
22nd Nov 2021, 9:21 PM
Arbab Ansari
Arbab Ansari - avatar
0
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; //set the age via Setter student.age = age; if(age<=0){ System.out.println("Invalid age"); student.setAge(0); } System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; public int age; public int getAge() { //complete Getter return age; } public void setAge(int age) { //complete Setter this.age = age; } }
18th Dec 2021, 9:50 AM
Ika Purnamasari
0
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; //set the age via Setter student.setAge(age); if (age < 0){ System.out.println("Invalid age"); } System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; private int age; public int getAge() { //complete Getter if (age < 0){ return age = 0; }else{ return age; } } public void setAge(int age) { //complete Setter if (this.age < 0){ this.age = 0; } else { this.age = age; } } } //this is working fine
7th May 2022, 2:22 PM
U.L.F. Feroza
0
This code only runs test 1,3,4 correctly: I don't have a clue other than the if else statements are mandatory. Allow me to go back and try again. The solution shared is not working either. Phew! 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.setAge(age); student.name = name; if(age < 0){ student.setAge(0); System.out.println("Invalid Age"); } //set the age via Setter 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; //complete Getter } public void setAge(int age) { this.age = age; //complete Setter } }
2nd Sep 2022, 2:50 AM
Natalie Sequita Wilder
Natalie Sequita Wilder - avatar
0
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; //set the age via Setter student.setAge(age); if(age < 0){ student.setAge(0); System.out.println("Invalid age"); } System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; private int age; public int getAge() { //complete Getter return age; } public void setAge(int age) { //complete Setter this.age = age; } }
15th Nov 2022, 7:52 AM
Pooja Patel
Pooja Patel - avatar