Ok, so I need a bit of help on a small project I'm working on. Here's the run down of it: Write a Java program that allows the user to enter tests grades for yourself. The application should prompt the user to enter how many tests they wish to enter and enter the grades.

Here is the code:

import java.io.*;

public class Grades
{
		public static void main(String [] args) throws IOException
		{
			// declare and consturct variables
			String tests;
			int grades;
			float scores[] = new float[10];
			BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));

			// prints prompts and get input
			System.out.println("");
			System.out.println();
			System.out.println("\tThe score calculator");
			System.out.println();
			System.out.print("\t\tEnter how many test scores you wish to enter: ");
				tests = dataIn.readLine();
				grades = Integer.parseInt(tests);
			for(int count = 0; count < grades; count++)
			{
				System.out.println("Enter the test");
				tests = dataIn.readLine();
				scores[10] = Float.parseFloat(tests);
			}
	  	 }
}

The problem is the for loop part. When it asked for the first test, and gets the input, a message displays "Array is out of bounds" and I don't know how to fix that. Also, how do you dynamically allocate arrays in java?

Any advice would be most appreciated. Thank you :)

Line 25:

scores[10] = Float.parseFloat(tests);

Look closely at the part before the equals sign, decide what you are trying to do, and see if this line does what you want it to do.

In reference to what Vernon just said, check out this link. It is very important to learn how to read and interpret the javadoc documentation. And when I think of dynamic memory allocation, I think of malloc and calloc in C. . in Java, all you need to do is use 'new' to allocate an Object dynamically (which you already did in your code).

P.S. You might want to read on google a bit about memory allocation in Java, because some might have issues with what I said in this post about the subject. The jist of it being that the JVM handles the underlying memory allocation, and all you are doing is using new to get yourself an Object. But surely, at runtime if you need an Object, array, or whatever else, you can use the 'new' keyword. This is an interesting article: http://www.javaworld.com/javaworld/javaqa/1999-08/04-qa-leaks.html

Edited 6 Years Ago by BestJewSinceJC: n/a

Thanks guys, but I have another question. How do you swap the array to a method in java? I know how you use pointers in C++ to do it, but how does it work in java?

I want to be able to throw the array into a method for it to be added and averaged. Thanks for any hints you can give.

Oh, here is my updated code:

import java.io.*;

public class Grades
{
		public static void main(String [] args) throws IOException
		{
			// declare and consturct variables
			String tests;
			int size;
			float total = 0, average = 0;
			float scores[] = new float[size];




			BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));

			// prints prompts and get input
			System.out.println("");
			System.out.println();
			System.out.println("\tThe score calculator");
			System.out.println();
			System.out.print("\t\tEnter how many test scores you wish to enter: ");
				tests = dataIn.readLine();
				size = Integer.parseInt(tests);
			for(int count = 0; count < size; count++)
			{
				System.out.print("Enter test: ");
				tests = dataIn.readLine();
				scores[count] = Float.parseFloat(tests);
				total += scores[count];
			}
			average = total / size;

			System.out.println("The total of all tests is " + total);
			System.out.println("The average of all tests is " + average);
		}
}

Edited 6 Years Ago by Bemani_lover: n/a

Caller method

doTotal(scores);

Called method - the one which execute

public static float doTotal(float[] sc){
    float total = 0.0;
    //calculation
    return float;
}
This article has been dead for over six months. Start a new discussion instead.