Skip to content
Advertisement

How can i solve the problem with creating a class in java using Eclipse

So I am using eclipse to practice coding using java and till now it was doing great, until I wrote this program:

import java.util.*;
public class cars {
    String name;
    int dom;
    Scanner in = new Scanner(System.in);
    void takedata()
    {
        name=in.next();
        dom=in.nextInt();
        }
    void displaydata()
    {
        System.out.print("Enter the name of the car"+name);
        System.out.print("Enter the Date of Manufacture of the car"+dom);
        }
    public static void main(String[] args) 
    {
        cars x = new cars();
        cars y = new cars();
        cars z = new cars();
        x.takedata();
        y.takedata();
        z.takedata();
        x.displaydata();
        y.displaydata();
        z.displaydata();
        }

}

whenever I am trying to run the code it is showing me nothing.

I need help.

Advertisement

Answer

It wont show anything. Your code will wait till you enter the care name. So, first you need to provide input as your takeData() method is getting called before displayData(). Modify your takeData() method as shown below:

void takedata()
{
    System.out.println("Enter Name of the Car: ");
    name=in.next();
    System.out.println("Enter Date of Manufacture: ");
    dom=in.nextInt();
}

Then after executing your program your will be able to see below message:

Enter Name of the Car:
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement