0

My first class...

import java.util.Scanner;

class MethodsANDInstances16A {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        MethodsANDInstances16B methObject = new MethodsANDInstances16B();
        System.out.println("Enter name of first girlfriend or good friend here: ");
        String temp = input.nextLine();
        methObject.setName(temp);
        methObject.saying();
    }
}

My second class...

public class MethodsANDInstances16B {
    private String girlName;
    public void setName(String name){
        girlName = name;
    }
    public String getName(){
        return girlName;
    }
    public void saying(){
        System.out.printf("Your first girlfriend or good friend was %s", getName());
    }
}

I just watched a Tutorial on YouTube with basically the exact same code... and yet the printf statement doesn't work for me... but it does in the video I watched. I heard I need to change Settings in Eclipse (IDE I'm using), but I'm not sure?

This is the error I'm getting when I run it...

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String)

    at MethodsANDInstances16B.saying(MethodsANDInstances16B.java:11)
    at MethodsANDInstances16A.main(MethodsANDInstances16A.java:10)"

Thank you.

Lisa Smith
  • 27
  • 6

1 Answers1

-1

You need to provide Locale.

public PrintStream printf(Locale, String, Object... args)

so use it like.

System.out.printf(Locale.ENGLISH, "Girlfriend or what not was %s", getName())
Miron Balcerzak
  • 886
  • 10
  • 21
  • The method `printf(String,Object...)` has been there since Java 1.5 so no you shouldn't need to do that. – Giovanni Botta Apr 04 '14 at 13:35
  • http://docs.oracle.com/javase/1.5.0/docs/api/java/io/PrintStream.html#printf%28java.util.Locale,%20java.lang.String,%20java.lang.Object...%29 – Miron Balcerzak Apr 04 '14 at 13:37
  • Your point being? That confirms what I just said: `public PrintStream printf(String format,Object... args)` is right there. – Giovanni Botta Apr 04 '14 at 13:40
  • It simply points i'm quite accurate with the answer as well. Why so aggressive? Hard day? – Miron Balcerzak Apr 04 '14 at 13:42
  • I apologize if I sounded aggressive. My point is that from JDK 1.5 the method signature `printf(String,Object...)` has been available. As far as I can tell, there was no `printf` at all in 1.4 so your answer does not address the question. – Giovanni Botta Apr 04 '14 at 13:44