Results 1 to 14 of 14
  1. #1
    silafirion is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default 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:
    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 + ".");
    	   }
    }
    It's throwing back this error:

    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 01:12 AM.

  2. #2
    silafirion is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    bah, it didn't take my indents...

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    bah, it didn't take my indents...

    Put [CODE] at the start of the code and [/CODE] at the end.

  4. #4
    silafirion is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    oh cool. Thanks! No thoughts on what's wrong?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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).

  6. #6
    silafirion is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    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?

  7. #7
    silafirion is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

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

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by silafirion View Post
    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?
    Java Code:
        public static void setEntryNumber(int input, int newNumber)  //Declairing the setEntryNumber
    input is an int, passed into the method as an argument.
    Java Code:
        {
            Scanner input = new Scanner(System.in);
    Now input is a Scanner, so the int input is thrown away.
    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 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(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
    Now you're assigning a value to newNumber, ignoring the value that was passed in to the method.
    Java Code:
            System.out.println("Number changed to " + number[input]);  //Letting the user know the variable was changed
    		
        }
    We're at the end of the method, and you never did anything with the newNumber you just got from the scanner.

    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-

  10. #10
    silafirion is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    yeah, I don't get what you mean here... I'm not clear on the difference between the two.

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by silafirion View Post
    yeah, I don't get what you mean here... I'm not clear on the difference between the two.
    Java Code:
           Scanner input = new Scanner(System.in);
    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:
            input=scanner.nextInt();  //Setting the input to the entry variable.
    -Gary-

  12. #12
    silafirion is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  13. #13
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    I don't know how to say it more clearly. You have called your Scanner input.
    Java Code:
           Scanner [COLOR="Red"]input[/COLOR] = new Scanner(System.in);
    You need to call it scanner.

    Java Code:
           Scanner [COLOR="Blue"]scanner[/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.

    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-

  14. #14
    silafirion is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Oh!!! I get it now. Sorry for being such a putz. Thanks for all the help guys!!

Similar Threads

  1. Having problems with methods
    By JavatastesGood in forum New To Java
    Replies: 2
    Last Post: 10-02-2010, 11:57 PM
  2. using multiple methods for graphics
    By jforce93 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2010, 07:05 PM
  3. JDBC that multiple methods will use
    By evermore in forum JDBC
    Replies: 3
    Last Post: 03-16-2010, 08:27 AM
  4. Problems With Array/Methods
    By blueduiker in forum New To Java
    Replies: 4
    Last Post: 01-19-2010, 02:49 AM
  5. Replies: 5
    Last Post: 07-18-2008, 05:30 PM

Tags for this Thread

Posting Permissions

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