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 5 of 5

Thread: Assign a value in a void method

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Assign a value in a void method

    I'm having some trouble trying to figure out how to assign a value to a variable being passed through a method parameter. First of all here is my relevant code:

    public static void main(String[] args) throws FileNotFoundException
    	{
    		Scanner inFile = new Scanner(new FileReader("grades.txt"));
    		PrintWriter outFile = new PrintWriter("output.txt");
     
    		String name;
    		int gr1, gr2, gr3, gr4, gr5;
    		double avg = 0;
    		char letterGr;
     
    		while (inFile.hasNext())
    		{
    			name = inFile.next();
    			gr1 = inFile.nextInt();
    			gr2 = inFile.nextInt();
    			gr3 = inFile.nextInt();
    			gr4 = inFile.nextInt();
    			gr5 = inFile.nextInt();
     
    			getAverage(gr1, gr2, gr3, gr4, gr5, avg);
    			letterGr = getGrade(avg);
    			System.out.printf("%7s%7d%7d%7d%7d%7d%7.2f%7s%n", name, gr1, gr2, gr3, gr4, gr5, avg, letterGr);
    		}
    	}
     
    	public static void getAverage(int g1, int g2, int g3, int g4, int g5, double average)
    	{
    		average = (g1 + g2 + g3 + g4 + g5) / 5;
    	}

    Now as it is, when I print it out, avg is always 0. For the method, getAverage, it has to be a void method, yet still set avg equal to the sum of the grades divided by 5 when the method is called. When my teacher explained this, it didn't seem too difficult but I just can't seem to figure out what I need to do. Normally I would just make this a method returning a double and assigning the value of the method to avg in main, but for this program assignment we have to do it this way.

    So basically, I need help to pass the variable, avg, through the method as the average parameter, and then set avg equal to the calculation in the method if that makes any sense. Any help would be greatly appreciated and if you need any more info just ask.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Assign a value in a void method

    how to assign a value to a variable being passed through a method parameter.
    Java passes args by value. There is no way to change the original value.
    Can you return the value? The name of the method: get... implies it is going to return a value.
    avg = getAverage(gr1, gr2, gr3, gr4, gr5);

    getAverage() does not use the value of avg that is passed to it, so there is no need to pass it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Assign a value in a void method

    Calling it getAverge is actually a mistake on my part, I'm so used to calling things get I didn't realize it didn't make sense here. The book actually says to call it calculateAverage, so that was an oversight by me.

    This is what the book actually says to do for this method that I'm not clear on, it may explain it better than I'm trying to.

    "A void method, calculateAverage, to determine the average of five test scores for each student. Use a loop to read and sum the five test scores. (This method does not output the average test score. That task must be done in the method main.)"

    So since this method is supposed to be void, I am not sure how I would write it. It seems like it would make more sense how you explained it, and that's how I normally would too.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Assign a value in a void method

    The book must mean for you to use a "global" variable that is defined at the class level.
    I'm not sure that is a good practice. I'd name the method as you have done and have it return the value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Freezer Boy (December 12th, 2012)

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Assign a value in a void method

    Okay that's what I'll do, thanks for your help!

Similar Threads

  1. Why my void run method cannot be execute?
    By 90th century in forum Threads
    Replies: 2
    Last Post: March 30th, 2012, 12:09 PM
  2. Need a non void method to return the first three characters of a string
    By fallout87 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 9th, 2011, 10:00 PM
  3. So Lost with void method
    By tripline in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 27th, 2011, 01:45 PM
  4. Reverse character using void method
    By bgwilf in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 8th, 2010, 07:25 AM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM