-22

The code is having some problem with class 2 line 12. Kindly help . The code is in the image.

import java.util.Scanner;
public class first{
    public static void main(String[] args){
        Scanner b = new Scanner(System.in);
        pro nc = new pro();
        System.out.println("Enter the name of your first true crush");
        String temp = b.nextLine();
        nc.setname(temp);
        nc.etc();
    }
}

public class pro{
    private String gname;
    public String getName() {
        return gname;
    }
    public void setName(String name) {
        this.gname = name;
    }
    public void etc(){
        System.out.printf("The name of your true crush was %s",getName());
    }        
}
Aroniaina
  • 1,252
  • 13
  • 31

3 Answers3

3

In class 2 you have not specified the location of the function in System.out.printf(). You should use the this keyword and re-write the function as

public class pro {
  private String name;

  public String getName() {
      return name;
  }

  public void setName(String name) {
     this.name = name;
  }
  public void etc(){
      System.out.printf("The carp is %s",this.getName());
  }

}
2

there is no problem with this

I AM USING NETBEANS IDE.



/*

    ///

public class pro {
private String gname;

    public String getname() {
        return gname;
    }

    public void setname(String name) {
        gname = name;
    }
    public void etc(){
        System.out.printf("Tht name is %s",getname());
    }
}

/////////

    import java.util.Scanner;

        public class first {
        public static void main(String[] args) {
        Scanner b= new Scanner(System.in);
        pro nc = new pro();
        System.out.println("enter the name");
        String temp = b.nextLine();
        nc.setname(temp);
        nc.etc();
    }
}



/////////

*/

Recheck this.

I have not changed your code, just typed it.
And yes I agree with Arc676 that people should post the code.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Doc
  • 10,831
  • 3
  • 39
  • 63
0

There is no problem on netbeans. You can try to use System.out.format instead of System.out.printf.

import java.util.Scanner;
public class first{
    public static void main(String[] args){
        Scanner b = new Scanner(System.in);
        pro nc = new pro();
        System.out.println("Enter the name of your first true crush");
        String temp = b.nextLine();
        nc.setname(temp);
        nc.etc();
    }
}
public static class pro{
    private String gname;
    public String getName() {
        return gname;
    }
    public void setName(String name) {
        this.gname = name;
    }
    public void etc(){
        System.out.format("The name of your true crush was %s", getName()); 
    }        
}
Aroniaina
  • 1,252
  • 13
  • 31