Is there Syntax ErroR. | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Is there Syntax ErroR.

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");this.age=0;} } }

9th Jun 2022, 3:31 AM
We Doru
We Doru - avatar
3 Antworten
+ 3
Run it in Code Playground. You'll see if there are errors, which ones, and in which lines. If you need help in the code, edit your question and include a link to your code in Code Playground and a description of your difficulties. This avoids an endless loop of questions to understand just that.
9th Jun 2022, 4:45 AM
Emerson Prado
Emerson Prado - avatar
+ 1
There is no syntax error, but take a look at your age setter 😉
9th Jun 2022, 5:28 AM
Roland
Roland - avatar
0
age is never set to Student.age field
10th Jun 2022, 12:34 PM
zemiak