Results 1 to 9 of 9
Thread: Method Error
- 03-31-2012, 10:19 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
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
-
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.
- 03-31-2012, 10:31 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
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; } }
-
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.
- 03-31-2012, 11:01 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
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:
...which (of course) is giving me another host of errors.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)); }
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
-
Re: Method Error
Check how you call the method:
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(name)
You've happened to have 3 Strings handy just before you call the method, and maybe you should use them... hmmmm...Java Code:nameRearrange(string1, string2, string3)
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.The only thing I can think of is moving line 13 after the method. Like this:
...which (of course) is giving me another host of errors.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)); }
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.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
- 03-31-2012, 11:34 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
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. :)
-
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!
- 03-31-2012, 11:41 PM #9
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
Why am i getting this error in my method?
By bdl1127 in forum New To JavaReplies: 1Last Post: 03-12-2012, 10:12 PM -
Method- error?
By katiebear128 in forum New To JavaReplies: 7Last Post: 10-16-2011, 03:01 PM -
what does this error method mean?
By berkeley in forum New To JavaReplies: 1Last Post: 06-03-2010, 11:19 AM -
toString() method error?
By blueduiker in forum New To JavaReplies: 6Last Post: 02-07-2010, 03:19 AM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks