Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Passing 2D array to method

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Passing 2D array to method

    Here's a friend's code. I included the entire thing in case you'd like to dump it into an IDE or whatever. Anyway, Eclipse is complaining about the following line:

    System.out.println(student[t] + "\t\t" +highGrade(grades) + "\t\t" +lowGrade(grades)
    + "\t\t" +(averageGrade(grades)));

    It says that 'grades' cannot be resolved to a variable. The intent of the code at this point is to pass the inner array of grades[][] to some methods, returning doubles to print. What is the proper syntax for this? (Note: I haven't checked out the methods yet, so there may also be errors there. But for now I'm just wondering about this syntax.)

    Thanks!

     
    import java.util.*;
     
    public class Midterm {
     
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Please enter the number of students: ");
    		int numofStudents = input.nextInt();
     
    		String student[] = new String[numofStudents];
     
    		for (int i = 0; i < numofStudents; i++)	{
     
    			// collect student name
    			System.out.println("Enter the " +  numName(i+1) + " student's name: ");
    			student[i] = input.next();	
     
    			// collect grades
     
    			double grades[][] = new double[student.length][];
    			grades[i] = collectGrades();
     
    		}	//end for
     
    	// print information
     
    		System.out.println("Student Name\tHigh Score\tLow Score\tAverage\tGrade");
    	    System.out.println("----------------------------------------------------------");
     
    	    for(int t = 0; t < numofStudents; t++) {
     
     
    			System.out.println(student[t] + "\t\t" +highGrade(grades) + "\t\t" +lowGrade(grades)  
    					+ "\t\t" +(averageGrade(grades)));
     
    	    }	//end for
     
    }	//end main
     
    	public static double[] collectGrades()	{
    		Scanner input = new Scanner(System.in);
    		System.out.println("Please enter the number of grades you will be entering for this student:");
    		int numOfGrades = input.nextInt();
     
    		double gradeArrayOut[] = new double[numOfGrades];
     
    		for (int i = 0; i < gradeArrayOut.length; i++)	{
    			System.out.println("Please enter the " + numName(i + 1) + " grade:");
    			gradeArrayOut[i] = input.nextDouble();
    		}	//end for
     
    		return gradeArrayOut;
    	}	//end collectGrades
     
    	public static double highGrade (double arrayIn[]) {
    		Arrays.sort(arrayIn);
    		return arrayIn[arrayIn.length -1];
     
    	} // end highGrade
     
    	public static double lowGrade (double arrayIn[]) {
    		Arrays.sort(arrayIn);
    		return arrayIn[0];
     
    	} // end highGrade
     
    	public static double averageGrade(double arrayIn[]) {
    		double avgGrade = 0.0;
    		for(int i = 0;i < arrayIn.length;i++) {
    			avgGrade += arrayIn[i];
     
    		} // end for
     
    		return avgGrade/arrayIn.length;
     
    	} // end averageGrade
     
     
    	public static String numName(int intIn) {
    		String numName = "";
    		switch(intIn) {
    		case 1:
    			numName = "first";
    			break;
    		case 2: 
    			numName = "second";
    			break;
    		case 3:
    			numName = "third";
    			break;
    		case 4: 
    			numName = "fourth";
    			break;
    		case 5:
    			numName = "fifth";
    			break;
    		case 6: 
    			numName = "sixth";
    			break;
    		case 7:
    			numName = "seventh";
    			break;
    		case 8: 
    			numName = "eight";
    			break;
    		case 9:
    			numName = "ninth";
    			break;
    		case 10: 
    			numName = "tenth";
    			break;
    		default:
    			numName = intIn + (intIn<20?"th" : (intIn%10==1?"st":
    			(intIn%10==2?"nd": (intIn%10==3?"rd":"rh"))));
    		} //end Switch
     
    		return numName;
     
    	}
     
    }	//end class
    Last edited by drgroove777; October 24th, 2012 at 12:15 AM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Passing 2D array to method

    highGrade() lowGrade()
    Both methods have a parameter of type double[], and the word grades does not refer to a variable of type double[].

    Please see the announcements page for instructions on the use of code tags.

  3. The Following User Says Thank You to jps For This Useful Post:

    drgroove777 (October 24th, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Passing 2D array to method

    To me you seem to be at pre beginner level. The grades is not visible in the second loop because it was declared the first for loop and is visible only inside the first for loop. It wont work unless:
    1. You move the System.out.println(...) statement from the second loop to the first loop.
    2. OR you declare grades outside the first for loop.

    Beside, you should be invoking method lowGrade as lowGrade(grades[t]) and not as lowGrade(grade). Similar for other two methods - highGrade and averageGrade.

Similar Threads

  1. [SOLVED] Cannot find symbol - Calling method passing array Pets
    By Rilstin81 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 02:27 PM
  2. method not passing across actionlistener interface
    By flamewolf393 in forum AWT / Java Swing
    Replies: 9
    Last Post: November 16th, 2011, 06:16 AM
  3. Question about object passing to a method? My VOCAB sucks
    By tripline in forum Object Oriented Programming
    Replies: 6
    Last Post: October 28th, 2011, 12:48 PM
  4. passing a string and int array into a static method
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 05:35 PM
  5. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM

Tags for this Thread