ryndgmn
ryndgmn

Reputation: 17

How to assign the number of elements in an array in Java?

I have a Java exercise here wherein you'll create a program that will allow you to enter your academic grades into an array of floats.

At the beginning of the program, the application should prompt the user for the total number of grades you intend to enter. After entering the grades, you will then calculate for the average of those grades. The code below is what I have coded so far.

import java.io.*;

public class Proj4exe1
    {
    public static void main(String args[]) throws IOException
    {
        BufferedReader dataIn = new BufferedReader(new  InputStreamReader(System.in));
        String strSize, strGrades;
        int laki = 100;
        int totalGrades = 0;
        float gradeAverage = 0;
        float[] grades = new float[laki];

        System.out.print("How many grades will you enter?");
        strSize = dataIn.readLine();
        laki = Integer.parseInt(strSize);


        for(int i=0; i<laki; i++)
    {
        System.out.print("Please enter grade # " + (i+1) + ": ");
        strGrades = dataIn.readLine();
        grades[i] = Float.parseFloat(strGrades);
        totalGrades += grades[i];
    }           
        gradeAverage = totalGrades / laki;

        System.out.println("Your grades average is " + gradeAverage);
    }
}

I have tried int[] grades = new int[laki.length] as the number of grades to input to the program but this results in a compile error "int cannot be dereferenced" so I just initialized int laki to 100 or I could have done int[] grades = new int[100] to hold 100 integers then delete int laki.

I didn't include a try and catch block here because I already did one and this is the only part of the program that I still don't know what to do appropriately.

Upvotes: 0

Views: 1375

Answers (5)

Grim
Grim

Reputation: 1974

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Proj4exe1
    {
    public static void main(String args[]) throws IOException
    {
        BufferedReader dataIn = new BufferedReader(new  InputStreamReader(System.in));
        String strSize, strGrades;
        int laki = 100;
        int totalGrades = 0;
        float gradeAverage = 0;
        float[] grades = new float[laki];

        System.out.print("How many grades will you enter?");
        strSize = dataIn.readLine();
        laki = Integer.parseInt(strSize);
        grades = new float[laki]; // reassign <-----------------------

        for(int i=0; i<laki; i++)
    {
        System.out.print("Please enter grade # " + (i+1) + ": ");
        strGrades = dataIn.readLine();
        grades[i] = Float.parseFloat(strGrades);
        totalGrades += grades[i];
    }           
        gradeAverage = totalGrades / laki;

        System.out.println("Your grades average is " + gradeAverage);
    }
}

Upvotes: 1

Suneet Bansal
Suneet Bansal

Reputation: 2702

@user3328588: 1. you program will not give the correct grand average of the grades as you are not typecasting average into float. So it will always give you the round figure.

gradeAverage = totalGrades / laki;

the above statement should be like this :

gradeAverage = (float) totalGrades / laki;
  1. Your code is not optimized and the initialization of the array is not at the correct place. (int[] grades)

3.Do not you * while importing package while use import for importing multiple classes.

The whole code with optimized version and the correct logic is attached below:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader;

public class Proj4exe1 {
    public static void main(String args[]) throws IOException {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(
                System.in));

        int totalGrades = 0;
        float gradeAverage = 0;

        System.out.print("How many grades will you enter?");
        int laki = Integer.parseInt(dataIn.readLine());
        float[] grades = new float[laki]; // reassign <-----------------------

        int count = 0;
        while (laki > 0) {
            System.out.print("Please enter grade # " + (count + 1) + ": ");
            grades[count] = Float.parseFloat(dataIn.readLine());
            totalGrades += grades[count];
            laki--;
            count++;
        }
        gradeAverage = (float) totalGrades / count;

        System.out.println("Your grades average is " + gradeAverage);
    }
}

Upvotes: 1

tddmonkey
tddmonkey

Reputation: 21184

You just need to reorganise your code a bit:

 int totalGrades = 0;
 float gradeAverage = 0;

 System.out.print("How many grades will you enter?");
 strSize = dataIn.readLine();
 laki = Integer.parseInt(strSize);
 float[] grades = new float[laki];

You were trying to get .length from an int which is wrong. Instead what this does is to convert your input to an int (laki) and then uses that to initialize the array

Upvotes: 5

maczikasz
maczikasz

Reputation: 1133

The problem is that you're calling a method on a primitive type whereas it doesn't have any. First you have to read the number from stdin into the laki variable

int laki = Integer.parseInt(dataIn.readline());

then you can do

float[] grades = new float[laki];

Upvotes: 1

ddz
ddz

Reputation: 526

You can create the vector after getting the numer of grades. Try this instead:

    int laki;

    System.out.print("How many grades will you enter?");
    strSize = dataIn.readLine();
    laki = Integer.parseInt(strSize);

    float[] grades = new float[laki];

By the way, why do you read the number of grades as a String and then convert it to int? Read it like int instead.

Upvotes: 2

Related Questions