Results 1 to 14 of 14
- 12-06-2010, 11:54 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
problems with multiple methods and paramaters...
Hello all,
I'm pretty new to the Java language, but I've done quite a bit of programming with basic before so I have a good idea of how things are supposed to go. I have an assignment that I need help with. We're supposed to code a menu driven phone book using arrays and the scanner util. Here's what I've got so far:
It's throwing back this error:Java Code:/*PhoneBookProgram.java - a more complex version of the phonebook program *Gabriel Miller *12/2/10 */ import java.util.Scanner; //Importing the Scanner class public class PhoneBookProgram //Declairing the class { public static String[] name = {"John Smith", "Jane Smith", "Becky Jones", "Matt Jones"}; public static int[] number = {5553213, 5556732, 5553234, 5553234}; //Setting up the arrays public static void main(String[]args) //Begin the main method { Scanner scan = new Scanner(System.in); //Activating the scanner int input=0; //Setting up the variable "input" and initializing it to 0. while (input != 6) //Begining a "while" loop based on what the user enters { displayMenu(); //Calling the displayMenu method input=scan.nextInt(); //Setting the input variable to what the user entered if (input==1) //An if statement for if the user entered 1 { displayPhoneBook(); //Calling the displayPhoneBook method } if (input==2) //An if statement for if the user entered 2 { display(0); //Calling the display method } if (input==3) //An if statement for if the user entered 3 { setEntryNumber(); //Calling the setEntryNumber method } if (input==4) //An if statement for if the user entered 4 { setEntryName(input, newName); //Calling the setEntryName method } if (input==5) //An if statement for if the user entered 5 { size(); //Calling the size method } } } public static void setEntryName(int input, String newName) //Declairing the setEntryName method { Scanner input = new Scanner(System.in); System.out.println("Which phone book entry name do you want to change? :"); //Ask the user for input input = Scanner.nextInt(); //Setting the input to the entry variable. Scanner newName = new Scanner(System.in); System.out.println(name[input] + " is the current name listed. Please enter the new name: "); //Letting the user know current value and asking for a new one. newName = Scanner.nextLine(); //Setting the input to the appropriate array variable name[input] = newName; System.out.println("Name changed to " + name[input]); //Letting the user know the variable was changed } public static void setEntryNumber(int input, int newNumber) //Declairing the setEntryNumber { Scanner input = new Scanner(System.in); System.out.println("Which phone book entry number do you want to display? :"); //Ask the user for input input=scanner.nextInt(); //Setting the input to the entry variable. System.out.println(number[input] + " is the current number listed. Please enter the new number: "); //Letting the user know current value and asking for a new one. newNumber=scanner.nextInt(); //Setting the input to the appropriate array variable System.out.println("Number changed to " + number[input]); //Letting the user know the variable was changed } public static void display(int entry) //Declairing the display method { Scanner entry = new Scanner(System.in); System.out.println("Which phone book entry do you want to display? :"); //Asking user for input entry=scanner.nextInt(); //Assigning user input to the entry variable System.out.println(name[entry] + " : " + number[entry]); //Displaying the desired entries } public static void displayMenu() { System.out.println("Phone Book 1.0"); System.out.println("Options:"); System.out.println("1) Display Phone Book"); System.out.println("2) List individual entry by number"); System.out.println("3) Change entry's number"); System.out.println("4) Change entry's name"); System.out.println("5) Display the size of the phone book"); System.out.println("6) Exit"); } public static void displayPhoneBook() { for (int i = 0; i < name.length; i++) { System.out.println(name[i] + " : " + number[i]); } } public static int size() { for (int x = 0; x < name.length; x++) System.out.println("The current number of entries in the phone book is " + x + "."); } }
PhoneBookProgram.java:29: setEntryNumber(int,int) in PhoneBookProgram cannot be applied to ()
setEntryNumber(); //Calling the setEntryNumber method
I have no idea of what to do about this. Do I initialize the variables when I call the method? If so, why won't it just set the variables to the default?
I really need some help with this. Thanks guys!!Last edited by silafirion; 12-07-2010 at 12:12 AM.
- 12-06-2010, 11:56 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
bah, it didn't take my indents...
- 12-07-2010, 12:06 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
bah, it didn't take my indents...
Put [CODE] at the start of the code and [/CODE] at the end.
- 12-07-2010, 12:13 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
oh cool. Thanks! No thoughts on what's wrong?
- 12-07-2010, 12:14 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
You are calling setEntryNumber() at the line the compiler is talking about with no arguments. But the method itself is defined later on as requiring two arguments.
Java Code:public static void setEntryNumber(int [b]input[/b], int [b]newNumber[/b]) {
What are the two arguments that setEntryNumber() takes supposed to be for? AT the moment their values are never used - the values they have are thrown away and new values assigned from user input.
Either remove the arguments from the method definition, or supply two arguments when you call the method (arguments which provide the values that values that the method needs).
- 12-07-2010, 12:30 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
Wait, I don't see how int input and int newNumber are never used? Or are you saying that I should just call the method like this:
setEntryNumber(0, 0);
Just so they have some sort of value and it can be replaced later?
- 12-07-2010, 12:41 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
Crap, now I'm getting this error...
PhoneBookProgram.java:46: non-static method nextInt() cannot be referenced from a static context
input = Scanner.nextInt(); //Setting the input to the entry variable.
I don't get it. I've used almost this exact piece of code elsewhere and it worked fine.
-
You should call the method on the Scanner object, scan, not the Scanner class.
- 12-07-2010, 12:46 AM #9
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
input is an int, passed into the method as an argument.Java Code:public static void setEntryNumber(int input, int newNumber) //Declairing the setEntryNumber
Now input is a Scanner, so the int input is thrown away.Java Code:{ Scanner input = new Scanner(System.in);
Now you want input to be an int again? But you're still not using the one that was passed into the method, because you're assigning to it, rather than looking at its value.Java Code:System.out.println("Which phone book entry number do you want to display? :"); //Ask the user for input input=scanner.nextInt(); //Setting the input to the entry variable.
Now you're assigning a value to newNumber, ignoring the value that was passed in to the method.Java Code:System.out.println(number[input] + " is the current number listed. Please enter the new number: "); //Letting the user know current value and asking for a new one. newNumber=scanner.nextInt(); //Setting the input to the appropriate array variable
We're at the end of the method, and you never did anything with the newNumber you just got from the scanner.Java Code:System.out.println("Number changed to " + number[input]); //Letting the user know the variable was changed }
So it definitely looks like you don't need the two arguments you've declared in this method. Even after you get rid of them, you have other things to fix (hint: I think you meant to call your Scanner scanner and not input).
-Gary-
- 12-07-2010, 12:51 AM #10
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
yeah, I don't get what you mean here... I'm not clear on the difference between the two.
- 12-07-2010, 12:56 AM #11
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You have created a new Scanner object, and named it input. I'm pretty sure you meant to name it scanner, because a couple of lines later, you call a method on what seems to be a Scanner object called scanner.Java Code:Scanner input = new Scanner(System.in);
-Gary-Java Code:input=scanner.nextInt(); //Setting the input to the entry variable.
- 12-07-2010, 12:57 AM #12
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
I want to let the user choose which entry in the phonebook program to change and also show them the current entry. I see how I'm dropping the initial value, but I'm not sure what's going on after that.
Also, I forgot to put number[input]=newNumber at the end of the method.
- 12-07-2010, 01:06 AM #13
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I don't know how to say it more clearly. You have called your Scanner input.
You need to call it scanner.Java Code:Scanner [COLOR="Red"]input[/COLOR] = new Scanner(System.in);
It's not because a Scanner has to be named scanner -- it's perfectly legal to have a Scanner named input -- but because you already have an int variable called input, and you call your Scanner scanner in the rest of the method.Java Code:Scanner [COLOR="Blue"]scanner[/COLOR] = new Scanner(System.in);
That's not your only problem with this code, but if you're not seeing that one, you're just trying to go too fast, and you're missing important details. Slow down, read carefully.
-Gary-
- 12-07-2010, 01:10 AM #14
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Having problems with methods
By JavatastesGood in forum New To JavaReplies: 2Last Post: 10-02-2010, 10:57 PM -
using multiple methods for graphics
By jforce93 in forum Advanced JavaReplies: 3Last Post: 04-25-2010, 06:05 PM -
JDBC that multiple methods will use
By evermore in forum JDBCReplies: 3Last Post: 03-16-2010, 07:27 AM -
Problems With Array/Methods
By blueduiker in forum New To JavaReplies: 4Last Post: 01-19-2010, 01:49 AM -
text box listeners and returning multiple strings from methods
By int80 in forum New To JavaReplies: 5Last Post: 07-18-2008, 04:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks