Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: What is wrong with my code? pls help!

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default What is wrong with my code? pls help!

    This is my main class:

    import java.util.Scanner;
    public class apples {
    	public static void main (String args[]){
     
    		Scanner input = new Scanner (System.in);
    		tuna tunaObject = new tuna ();
     
    		System.out.println("Enter name of first gf here: ");
    		String temp = input.nextLine();
    	tunaObject.setName(temp);
    		tunaObject.saying();


    this is my other class:
    public class tuna {
    	private String girlName;
    	public void setName(String name){
    		girlName=name;
     
    	}
    	public String getName(){
    		return girlName;
    	}
    	public void saying(){
    		System.out.printf("Your first gf was %s", getName());
     
    	}
    and this is the message eclipse gives me:

    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 nikolai.tuna.saying(tuna.java:13)
    at nikolai.apples.main(apples.java:12)


    I copied this piece of code from a thenewboston.org tuturial that ive not been allowed to link because im new.
    If you could give me a fix and possibly an explanation as im learning java that would be really nice!

    I'd be much obliged of your help
    Last edited by nikolaiL; May 9th, 2014 at 06:56 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? pls help!

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link, preferably without line numbers.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? pls help!

    Quote Originally Posted by GregBrannon View Post
    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link, preferably without line numbers.
    is this ok? i edited it. i thought line numbers would help as eclipse makes reference to them.
    Last edited by nikolaiL; May 9th, 2014 at 05:26 AM.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? pls help!

    Not really. You've made an effort, and I appreciate it, but using code tags does not require as much effort. Please read the link I provided and see the part about code tags near the top, the first item I think.

    As for your code problem, I cleaned up your code so that I could test it, but I don't believe I changed the substance, and I don't have the error you are showing. Note that by convention in Java, class names are capitalized. Here's my version of your code, properly posted:
    import java.util.Scanner;
     
    public class Apples
    {
        public static void main (String args[])
        {
            Scanner input = new Scanner (System.in);
            Tuna tunaObject = new Tuna ();
     
            System.out.println("Enter name of first gf here: ");
            String temp = input.nextLine();
            tunaObject.setName(temp);
            tunaObject.saying();
        }
    }
     
    class Tuna
    {
        private String girlName;
        public void setName(String name){
            girlName=name;
     
        }
        public String getName(){
            return girlName;
        }
        public void saying(){
            System.out.printf("Your first gf was %s", getName());
        }
    }

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    nikolaiL (May 10th, 2014)

  6. #5
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? pls help!

    thank you for the effort but unfortunately im still getting the same problem, eclipse tells me that printf is used for (string, object) instead of (string, args) which is what i think it should be. i dont know how to fix this because im only a learner, is it to do with the version of eclipse/jdk?

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? pls help!

    I'm stumped. What version of Eclipse and JDK are you using? Unless really old - and I can't imagine how old - the code I posted should work without error.

    Have you successfully completed the typical "HelloWorld.java" application to ensure your environment is correctly configured? If you have the HelloWorld.java source code available that already works with a println() method, try changing the program to use a printf() statement, like this:

    System.out.printf( "%s\n", "Hello World." );

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    nikolaiL (May 10th, 2014)

  9. #7
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? pls help!

    This is the info i got about eclipse:

    Eclipse Java EE IDE for Web Developers.
    Version: Kepler Service Release 2
    Build id: 20140224-0627

    For the JDK, using "java -version" in command prompt:

    java version "1.8.0_053
    Java (TM) SE Environment (build 1.8.0_05-b13)
    Java hotspot (TM 64-Bit Server VM (build 25.5-b°2, mixed mode).

    I ran a simple "hello world" application which worked with both printf and println.

    --- Update ---

    Little update, i put the code into notepad and ran it through command prompt and the program worked, no problem so there must be some problem with eclipse.

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? pls help!

    Even more confused now that your HelloWorld program works with both.

    There is a plugin (or a Java 8 update, forget what it was called) to enable Eclipse to work with Java 8. You might find and try that.

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    nikolaiL (May 10th, 2014)

  12. #9
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? pls help!

    eclipse marketplace doesnt work for some reason, should i consider uninstalling java 8 and go for java 7?

  13. #10
    Junior Member
    Join Date
    Dec 2013
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: What is wrong with my code? pls help!

    I tested on my Eclipse and it ran fine (after fixing semicolon)

  14. The Following User Says Thank You to PitLv For This Useful Post:

    nikolaiL (May 10th, 2014)

  15. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? pls help!

    Quote Originally Posted by nikolaiL View Post
    eclipse marketplace doesnt work for some reason, should i consider uninstalling java 8 and go for java 7?
    You shouldn't have to. Were you following these instructions?

  16. The Following User Says Thank You to GregBrannon For This Useful Post:

    nikolaiL (May 10th, 2014)

  17. #12
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? pls help!

    oh... i uninstalled java 8 iin the meantime unfortunately.

    --- Update ---

    oh no, ive really dug a hole for myself now, ive managed to uninstall actual java from my computer, this means for example i cant launch a game like minecraft, this is really serious. do you know what i can do?

  18. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? pls help!

    Don't waste time playing Minecraft? That's probably not what you wanted.

    Why don't you reinstall Java? Download the correct version or install package from Oracle and install it.

  19. The Following User Says Thank You to GregBrannon For This Useful Post:

    nikolaiL (May 10th, 2014)

  20. #14
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? pls help!

    i did but it doesnt work. minecraft doesnt work (using this only as an example because its uses java) and the java test applet on java.com doesnt work.

    --- Update ---

    it seems we're back at the start, after re-installing everything i still get the problem with printf.

    --- Update ---

    i think eclipse just isnt working.

    --- Update ---

    https://bugs.eclipse.org/bugs/show_bug.cgi?id=89912

  21. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What is wrong with my code? pls help!

    The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String)
    Where is the definition for printf(String, String) defined?
    What version of the java program is being used to execute the class?
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    nikolaiL (May 10th, 2014)

  23. #16
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? pls help!

    im not really sure what you mean for the first question. For the other question are you talking about the java compiler compliance? if so its level is 1.7.

  24. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What is wrong with my code? pls help!

    what you mean for the first question.
    I could not find a definition for this method: printf(String, String) with those args.

    For the other question
    I was asking about the java program that executes the code, NOT the compiler.

    The error message looks like it happens when a class file was being executed. The java program found a reference to this definition for the method printf(String, String) in the class file it was trying to execute, but it could only find this definition for that method: printf(String, Object[]) in the PrintStream class it uses for execution.

    I'm probably confused by the way the IDE mixes compiler errors and runtime errors.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #18
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? pls help!

    Quote Originally Posted by Norm View Post
    I could not find a definition for this method: printf(String, String) with those args.
    Im not that sure what you mean, in my args ive put a string followed by another string and ive followed this code from a thenewboston.org tutorial, where it works.

    --- Update ---

    Quote Originally Posted by Norm View Post
    I was asking about the java program that executes the code, NOT the compiler.

    The error message looks like it happens when a class file was being executed. The java program found a reference to this definition for the method printf(String, String) in the class file it was trying to execute, but it could only find this definition for that method: printf(String, Object[]) in the PrintStream class it uses for execution.

    I'm probably confused by the way the IDE mixes compiler errors and runtime errors.
    im using eclipse, im sorry but im really very new to computer programming and im finding it hard to understand what you're asking, sorry. do you think you have a fix for this problem?

  26. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What is wrong with my code? pls help!

    Im not that sure what you mean
    Look at the API doc for that method. Are there ANY that have a String for the second arg? Copy and paste it here if you find one.

    No, I do not have a fix. I'm trying to find where the definition: printf(String, String) is coming from.
    The only definition I can find is: printf(String, Object[])
    If you don't understand my answer, don't ignore it, ask a question.

  27. #20
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? pls help!

    I don't really know what or how i did it but i re-installed eclipse and now i dont have a printf problem.

    --- Update ---

    Quote Originally Posted by Norm View Post
    Look at the API doc for that method. Are there ANY that have a String for the second arg? Copy and paste it here if you find one.

    No, I do not have a fix. I'm trying to find where the definition: printf(String, String) is coming from.
    The only definition I can find is: printf(String, Object[])
    im really sorry but what you're saying doesnt mean anything to me, i dont know what an API is. if printf is the wrong method for (String,String) do you know the right method?

  28. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What is wrong with my code? pls help!

    That's Good.

    Read the API doc for the printf() method and see what it says.
    If you have questions about it, copy it here and ask your questions.
    If you don't understand my answer, don't ignore it, ask a question.

  29. #22
    Junior Member
    Join Date
    May 2014
    Posts
    29
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? pls help!

    anyway, halleluiah its over!

Similar Threads

  1. Replies: 3
    Last Post: November 26th, 2013, 06:41 AM
  2. Pls check my code
    By javamonkey30 in forum Loops & Control Statements
    Replies: 12
    Last Post: March 18th, 2013, 02:26 PM
  3. Getting Error in code pls help
    By gokul1242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2011, 08:35 AM
  4. Code snippet pls?
    By srinivas0615 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 30th, 2011, 02:28 AM
  5. Could some pls help me out with this code??
    By Pearl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 11:01 AM