Java error (basic) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Java error (basic)

What is wrong in this code. I know this is simple and something is missing, but what. https://code.sololearn.com/c0DwIVlq4aZ2/?ref=app

28th Nov 2021, 8:36 AM
𝓐𝓷𝓼𝓱𝓲𝓴𝓪 (Anshika)
𝓐𝓷𝓼𝓱𝓲𝓴𝓪 (Anshika) - avatar
5 Answers
+ 7
Anshika Singh As Denise has explained you can make that method as constructor or by creating an object. Currently in your code yoy have created an method inside class but not passed any argument or paremeters to that class which is first issue to be fixed. You need ti call the created function to be worked so call the function via creating an main function below is the fixed code attached... If you wanted to create via concept of object or constructor than you can update accordingly as above answer by Denise... class Program { static int display_details(String name, int age) { System.out.println("Name is "+ name); System.out.println("Age is " + age); return 0; } public static void main(String[ ] args) { display_details("A",4); display_details("B",55); } }
28th Nov 2021, 10:53 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 3
Anshika Singh It depends on what you want to do. You can add the main method inside the Program class which calls displayDetails (Java uses camelCase for method names) or you write another class with the main in it. In this case you need to create an object of Program to call displayDetails. And you need do decide how name and age gets their values. Via constructor or via method or directly?
28th Nov 2021, 9:46 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
static String name; static int age; public static void main(String[] args) { System.out.println("Name is "+ name); System.out.println("Age is " + age); }
28th Nov 2021, 8:59 AM
SoloProg
SoloProg - avatar
+ 2
standard way is to use toString() public class Person { public String name; public int age; public String toString() { return "Name is " +name +"\n"+ "Age is " +age; } } public class Program { public static void main(String[] args) { Person p = new Person(); p.name = "Brian May"; //to-do write constructor for it p.age = 74; System.out.println( p ); } }
28th Nov 2021, 5:12 PM
zemiak
+ 1
Try again and best of luck
28th Nov 2021, 4:34 PM
aniket khairnar
aniket khairnar - avatar