Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Method Error

  1. #1
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Method Error

    Good afternoon all,

    I'm working on homework for my intro to programming class and am getting an error I can't figure out. Here's the question:

    Write a method that is passed three Strings (first, middle, and last name) and returns one String in the format lastName, firstName middleInitial. (Sample data: sent Henry Wadsworth Longfellow, returns Longfellow, Henry W.)

    Here's my code:
    Java Code:
    import java.util.Scanner;
    
    public class Test
    {
    	public static void main(String[] args)
       {
    		
    		System.out.println("Enter first name, middle name and last name");
    		Scanner in = new Scanner(System.in);
    		String first = in.next();
    		String middle = in.next();
    		String last = in.next();
    	}
    	public static void String nameRearrange(String first, String middle, String last)
    	{
    		System.out.println(last + ", " + first + middle.charAt(1) + ".");
    	}
    }

    Here's the error I'm getting:
    Test.java:14: error: '(' expected
    public static void String nameRearrange(String first, String middle, String last)

    (the ^ is under "n" in nameRearrange)

    Thank you for your help!!!
    ~Jaime

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Method Error

    A method has to return something or return "void" -- it can't return both.

    Also note that your nameRearrange method should not have a System.out.println(...) of any kind inside of it. The method's goal is not to display anything but rather to return something.

  3. #3
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Method Error

    Ok...this was the other way I was thinking, however I'm getting the following error:

    Test.java:13: error: method nameRearrange in class Test cannot be applied to given types;
    System.out.println(nameRearrange(name));

    required: String,String,String
    found: String
    reason: actual and formal argument lists differ in length

    I'm thinking it's because the method is looking for 3 Strings (first, middle and last) but it's only printing 1. However, I'm not sure why that's a problem.

    Here's the code:
    Java Code:
    import java.util.Scanner;
    
    public class Test
    {
    	public static void main(String[] args)
       {
    		String name;
    		System.out.println("Enter first name, middle name and last name");
    		Scanner in = new Scanner(System.in);
    		String first = in.next();
    		String middle = in.next();
    		String last = in.next();
    		System.out.println(nameRearrange(name));
    	}
    	public static String nameRearrange(String first, String middle, String last)
    	{
    		String name;
    		name = (last + ", " + first + middle.charAt(1) + ".");
    		return name;
    	}
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Method Error

    Your nameRearrange method is declared to expect three Strings when you call it. So look at where you are calling it, and check how many Strings are you passing into it.

  5. #5
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Method Error

    I'm not sure I'm following....from what I think I'm seeing, I'm passing it 3 Strings which is what it's calling for on line 15. The only thing I can think of is moving line 13 after the method. Like this:

    Java Code:
    import java.util.Scanner;
    
    public class Test
    {
    	public static void main(String[] args)
       {
    		String name;
    		System.out.println("Enter first name, middle name and last name");
    		Scanner in = new Scanner(System.in);
    		String first = in.next();
    		String middle = in.next();
    		String last = in.next();
    	}
    	public static String nameRearrange(String first, String middle, String last)
    	{
    		String name;
    		name = (last + ", " + first + middle.charAt(1) + ".");
    		return name;
    	}
    	System.out.println(nameRearrange(name));
    }
    ...which (of course) is giving me another host of errors.

    I always thought I was pretty smart until I started this class. I've never felt more stupid. Please continue to work me through this and don't lose patience!

    ~Jaime

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Method Error

    Quote Originally Posted by jmscarlet9 View Post
    I'm not sure I'm following....from what I think I'm seeing, I'm passing it 3 Strings which is what it's calling for on line 15.
    Check how you call the method:

    Java Code:
    nameRearrange(name)
    name is a single String, which is not how the method should be called. Since you've declared it to require three Strings when called (and this is correct by the way), you need to call it that way:

    Java Code:
    nameRearrange(string1, string2, string3)
    You've happened to have 3 Strings handy just before you call the method, and maybe you should use them... hmmmm...

    The only thing I can think of is moving line 13 after the method. Like this:

    Java Code:
    import java.util.Scanner;
    
    public class Test
    {
    	public static void main(String[] args)
       {
    		String name;
    		System.out.println("Enter first name, middle name and last name");
    		Scanner in = new Scanner(System.in);
    		String first = in.next();
    		String middle = in.next();
    		String last = in.next();
    	}
    	public static String nameRearrange(String first, String middle, String last)
    	{
    		String name;
    		name = (last + ", " + first + middle.charAt(1) + ".");
    		return name;
    	}
    	System.out.println(nameRearrange(name));
    }
    ...which (of course) is giving me another host of errors.
    As it should since you're trying to call a method naked in the class and outside of any method or constructor. Don't do this.

    I always thought I was pretty smart until I started this class. I've never felt more stupid. Please continue to work me through this and don't lose patience!

    ~Jaime
    Don't lose hope. None of us mortals were born learning to program (except a few Java deities), and have to learn through study and trial. So keep studying, keep trying and you will get better. Promise.

  7. #7
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Method Error

    THANK YOU. I hate that I can get it to a point where it's just something ridiculously simple to change.

    Any chance you'll be online for a while? I have 3 more pieces of code to write and am completely lost. :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Method Error

    You're welcome. I'll be here on and off over this weekend, but regardless there are many smarter people around who can help. If you have a new question, then you'll want to start a new Thread in this forum.

    Best of luck!
    jmscarlet9 likes this.

  9. #9
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Method Error

    Thank you. While there may be smarter people, you are encouraging about it. I appreciate that greatly. New thread started. Here we go!

Similar Threads

  1. Why am i getting this error in my method?
    By bdl1127 in forum New To Java
    Replies: 1
    Last Post: 03-12-2012, 10:12 PM
  2. Method- error?
    By katiebear128 in forum New To Java
    Replies: 7
    Last Post: 10-16-2011, 03:01 PM
  3. what does this error method mean?
    By berkeley in forum New To Java
    Replies: 1
    Last Post: 06-03-2010, 11:19 AM
  4. toString() method error?
    By blueduiker in forum New To Java
    Replies: 6
    Last Post: 02-07-2010, 03:19 AM
  5. Replies: 1
    Last Post: 02-10-2009, 10:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •