7 Replies Latest reply: Oct 10, 2012 8:05 AM by aksarben RSS

    Arrays and Methods

    962826
      The task is to write program to enter ten numbers, store them in an array, and then use two methods - aprint to print out the array in its entirity to the user and min to determine what the lowest element was and which number it was input as. I was able to write the program to perform correctly, but without using methods. I am haveing a hard time grasping the method idea. Here is my java file without using methods.
      public class ArraysMethods {
        public static void main (String[] args) {
      
             //Enter ten numbers
             Scanner input = new Scanner(System.in);
             System.out.println("Please enter ten different numbers, separated by spaces: ");
             
             //Create an array
             double[] myList = new double[10];
             
             //Store them in array
             for (int i = 0; i < myList.length; i++){            
                  myList[i] = input.nextDouble();
                  
             }
             
             //Find the smallest number and index of that number
             double min = myList[0];
             int indexOfMin = 0;
                    for (int i = 1; i < myList.length; i++) {
                  if (myList[i] < min){
                       min = myList;
                     indexOfMin = i;
                }
           }
           
           //Display the results
           System.out.println("The smallest number entered is " + min + " and it was entered as input #" + (indexOfMin + 1) +".");
           System.out.println("The array in its entirity is: ");
           for (int i = 0; i < myList.length; i++){
                System.out.println(myList[i] + " ");
           }
           
           input.close();
      }     
      }
      And here is where I am going trying just to do the aprint method first. I know this is wrong, but I am having a hard time grasping the relationship between these, even after several readings.
      import java.util.Scanner;

      public class ArraysMethods {
      public static void main (String[] args) {
           
           //Enter ten numbers
           Scanner input = new Scanner(System.in);
           System.out.println("Please enter ten different numbers, separated by spaces: ");
           
           //Create an array
           double[] myList = new double[10];
           
           //Store them in array
           for (int i = 0; i < myList.length; i++){           
                myList[i] = input.nextDouble();
                
           }
           
           //Call the aprint method
           System.out.println("The method in its entirity is ");
           aprint(myList[i]);
           
      //write the aprint method
           public static void aprint(){
           for (int i = 0; i < myList.length; i++)
                System.out.println(myList[i] + " ");
           }

      }
      }
      Errors I am getting:
      
      (when I call the aprint method):
      Multiple markers at this line
           - i cannot be resolved to a variable
           - The method aprint(double) is undefined for the type 
            ArraysMethods
      
      (when I write the aprint method):
      Multiple markers at this line
           - Syntax error, insert "EnumBody" to complete BlockStatement
           - Syntax error, insert "enum Identifier" to complete 
            EnumHeaderName
           - Syntax error on token "void", @ expected
      
      I just am not sure how to transition this into methods. Any help?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        • 1. Re: Arrays and Methods
          rp0428
                 aprint(myList);
               
          //write the aprint method
               public static void aprint(){
          You are calling the method using 'myList' and 'i' is not defined. You need to pass the entire array to the method.

          Except that your method declaration does not have an array parameter. You need to add a parameter to the method and then use the parameter within the method.
          • 2. Re: Arrays and Methods
            962826
            rp0428 wrote:
                   aprint(myList);
                 
            //write the aprint method
                 public static void aprint(){
            You are calling the method using 'myList' and 'i' is not defined. You need to pass the entire array to the method.

            Except that your method declaration does not have an array parameter. You need to add a parameter to the method and then use the parameter within the method.
            Ok - so a couple more questions then to help me understand. Here is my updated code:
            import java.util.Scanner;

            public class ArraysMethods {
            public static void main (String[] args) {
                 
                 //Enter ten numbers
                 Scanner input = new Scanner(System.in);
                 System.out.println("Please enter ten different numbers, separated by spaces: ");
                 
                 //Create an array
                 double[] myList = new double[10];
                 
                 //Store them in array
                 for (int i = 0; i < myList.length; i++){           
                      myList[i] = input.nextDouble();
                      
                 }
                 
                 //Call the aprint method
                 System.out.println("The method in its entirity is ");
                 
                 
                 
            //write the aprint method
                 public static double aprint(double[] myList){
                 for (int i = 0; i < myList.length; i++){
                      System.out.println(myList[i] + " ");
                 }

            }
            }
            I believe I worte the aprint method successfully. Now I am having problems calling it. I added the parameters to the method (double[] myList) - correct? So how do I call it?
            
            Edited by: restless8052 on Oct 4, 2012 10:41 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
            • 3. Re: Arrays and Methods
              rp0428
              >
              I believe I worte the aprint method successfully. Now I am having problems calling it. I added the parameters to the method (double[] myList) - correct? So how do I call it?
              >
              Not quite - the aprint method should not return anything; use void like you did before.

              You call it the same way you called it before but pass the entire array.
              • 4. Re: Arrays and Methods
                962826
                I am not sure what you mean by pass the entire array.

                This is how I called it before, paired with the new method for aprint:
                //Call the aprint method
                       System.out.println("The method in its entirity is ");
                       aprint(myList);
                }
                     
                     
                     
                //write the aprint method
                     public static void aprint(double[] myList){
                     for (int i = 0; i < myList.length; i++){
                          System.out.println(myList[i] + " ");
                     }
                Thanks for your help and patience - still learning obviously.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                • 5. Re: Arrays and Methods
                  DrClap
                  restless8052 wrote:
                  I am not sure what you mean by pass the entire array.
                  aprint(myList);
                  • 6. Re: Arrays and Methods
                    962826
                    That worked - thanks. Ok now to tackle the second part - finding the minimum and declaring the index of that number. I will repost if I have issues with that! Thanks!
                    • 7. Re: Arrays and Methods
                      aksarben
                      Try Googling some key terms such as Java min max & array. That should give you some good ideas & examples.