简体   繁体   中英

java.lang.NoSuchMethodException: Practice.Virual.main [class [Ljava.lang.String;] at java.lang.Class.getMethod(Class.java:2072) a error

What's wrong with my code? I'm new to coding so i may got something wrog here even though my code runs.. there's seem to be nothing run with my code though. the error that was showing was paste at the bottom of this post.

String name, streetname, barangay, city, region;
int zipcode, streetno;

   void display() {
     System.out.println("Name: " + this.name);
     System.out.println("Street No: " + this.streetno);
     System.out.println("Street: " + this.streetname);
     System.out.println("Barangay: " + this.barangay);
     System.out.println("City : " + this.city);
     System.out.println("Region: " + this.region);
     System.out.println("Zip Code : " + this.zipcode);
     
   }
}

class residentinfo {

  public static void main(String[] args) {
  
  Scanner in = new Scanner(System.in);
  Virual rsd = new Virual();
  
  System.out.println("Enter Name: ");
  rsd.name = in.nextLine();
  System.out.println("Enter Street No: ");
  rsd.streetno = in.nextInt();
  System.out.println("Enter Street Name: ");
  rsd.streetname = in.nextLine();
  System.out.println("Enter Barangay: ");
  rsd.barangay = in.nextLine();
  System.out.println("Enter City: ");
  rsd.city = in.nextLine();
  System.out.println("Enter Region: ");
  rsd.region = in.nextLine();
  System.out.println("Enter Zip Code: ");
  rsd.zipcode = in.nextInt();
  
  rsd.display();

  }
}

What's wrong with my code? When i run it this error showed up. there's seem to be nothing run with my code, and it runs already.

java.lang.NoSuchMethodException: Practice.Virual.main [class [Ljava.lang.String;] at java.lang.Class.getMethod(Class.java:2072)
at java.lang.Class.getDeclaredMethod(Class.java:2050)
at com.duy.android.compiler.java.Java.run(Java.java:105)
at com.duy.ide.javaide.run.activities.ExecuteActivity.executeDex(ExecuteActivity.java:147)
at com.duy.ide.javaide.run.activities.ExecuteActivity.exec(ExecuteActivity.java:124)
at com.duy.ide.javaide.run.activities.ExecuteActivity.access$100(ExecuteActivity.java:45)
at com.duy.ide.javaide.run.activities.ExecuteActivity$1.run(ExecuteActivity.java:88)
at java.lang.Thread.run(Thread.java:923

There is a several things which you should fix with your code. In general class names start with upper case letters in Java. And you should use getter and setters for you variables for best practice. Also you can't read streetName with your current code because when you call in.nextInt() method it doesn't consume new line character. And also for best practice you should use camelCase for you variable names.

Here is a better aproach to your code.

public class Virual {
    private String name;
    private String streetName;
    private String barangay;
    private String city;
    private String region;
    private int zipcode;
    private int streetNo;

    public Virual() {
    }

    @Override
    public String toString() {
        return String.format("Name: %s Street No: %d Street: %s Barangay: %s City : %s Region: %s Zip Code : %d", name, streetNo, streetName, barangay, city, region, zipcode);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStreetName() {
        return streetName;
    }

    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    public String getBarangay() {
        return barangay;
    }

    public void setBarangay(String barangay) {
        this.barangay = barangay;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public int getZipcode() {
        return zipcode;
    }

    public void setZipcode(int zipcode) {
        this.zipcode = zipcode;
    }

    public int getStreetNo() {
        return streetNo;
    }

    public void setStreetNo(int streetNo) {
        this.streetNo = streetNo;
    }
}


public class ResidentInfo {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        Virual rsd = new Virual();

        System.out.println("Enter Name: ");
        rsd.setName(in.nextLine());
        System.out.println("Enter Street No: ");
        rsd.setStreetNo(in.nextInt());
        in.nextLine(); //to consume /n character (new line)
        /*if you don't put in.nextLine after in.nextInt , in.nextInt method don't read newline char
        so you can't enter value for street name
         */
        System.out.println("Enter Street Name: ");
        rsd.setStreetName(in.nextLine());
        System.out.println("Enter Barangay: ");
        rsd.setBarangay(in.nextLine());
        System.out.println("Enter City: ");
        rsd.setCity(in.nextLine());
        System.out.println("Enter Region: ");
        rsd.setRegion(in.nextLine());
        System.out.println("Enter Zip Code: ");
        rsd.setZipcode(in.nextInt());

        System.out.println(rsd.toString());

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM