How to calculate the average grade of the values from an array? - java

Please help me..I've been working on this nonstop and i still cant get this to run the way i want it to.I am new to programming. i am not very good at using methods. i have a sample of my work here and my problem is how do i get the average of the grades and how do i print it on the console. when i run this code the last values inside the variable grade are the only values that display on the console. I also need to check the attendance...how do i do it?
i need this code for my finals.
package studentmonitoring;
import java.util.*;
public class StudentMonitoring {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Date date=new Date();
int[] attend1={},attend2,attend3,grade={};
String line="-------------------------------------------------------------------";
String[] idNum1={},idNum2={},gender={}, name1={},name2={},name3={},name4={},address={},sub={};
Scanner input=new Scanner(System.in);
int check=0,num=0;
System.out.println(line);
System.out.println("\t\t****STUDENT MONITORING****");
System.out.println(line);
System.out.println("\nGood day teacher, to start using this program input the following information first.\n");
System.out.println(line);
System.out.print("NAME OF SCHOOL: ");
String sName=in.nextLine();
System.out.print("ADDRESS OF SCHOOL: ");
String sAdd=in.nextLine();
System.out.print("SCHOOL IDENTIFICATION NUMBER: ");
String sId=in.nextLine();
System.out.println(line);
System.out.println("STUDENTS INFORMATION");
System.out.println("Enter the year and section of this class");
String yearSec=in.nextLine();
System.out.println("How many student information do you want to input? ");
int numberStudents=in.nextInt();
idNum1=new String[numberStudents];
name1=new String[numberStudents];
name2=new String[numberStudents];
name3=new String[numberStudents];
name4=new String[numberStudents];
attend1=new int[200];
attend2=new int[200];
attend3=new int[200];
address=new String[numberStudents];
grade=new int[100];
gender=new String[numberStudents];
idNum2=new String[numberStudents];
System.out.println("Enter the number of subjects you have: ");
int numSub=in.nextInt();
sub=new String[numSub];
in.nextLine();
for(int x=0;x<numSub;x++) {
System.out.print("Subjec #"+(x+1)+": ");
sub[x]=in.nextLine();
}
for(int x=0;x<numberStudents;x++) {
in.nextLine();
System.out.println(line);
System.out.println("Student #"+(x+1));
System.out.print("LAST NAME: ");
name1[x]=in.nextLine();
System.out.print("FIRST NAME: ");
name2[x]=in.nextLine();
System.out.print("MIDDLE INITIAL: ");
name3[x]=in.nextLine();
System.out.print("GENDER: ");
gender[x]=in.nextLine();
System.out.print("ADDRESS: ");
address[x]=in.nextLine();
System.out.print("ID NUMBER: ");
idNum1[x]=in.nextLine();
System.out.println(line);
System.out.println("Enter the grades here.");
System.out.println(line);
for (int a=0;a<numSub;a++) {
System.out.print(sub[a]+": ");
grade[a]=in.nextInt();
}
}
System.out.println("This is the list of the students info you've inputted.");
System.out.println("ID Number\t Name\t\t GENDER\t\t ADDRESS");
for(int x=0;x<numberStudents;x++) {
System.out.println(idNum1[x]+"\t\t"+name1[x]+" "+name2[x]+","+name3[x]+".\t\t"+gender[x]+"\t\t"+address[x]);
}
System.out.println("Subject\t\tGrades");
for(int x=0;x<numSub;x++) {
System.out.println(sub[x]+"\t\t"+grade[x]);
}
System.out.println(line);
System.out.println();
}
}

public static void main(String[] args) {
System.out.println("Enter the number of students you want: ");
Scanner scanner = new Scanner(System.in);
int numOfStudents = scanner.nextInt();
double[] grades = new double[numOfStudents];
double total = 0;
double currentGrade = 0;
for(int i=0; i<grades.length; i++) {
System.out.println("Grade achieved: ");
currentGrade = scanner.nextDouble();
grades[i] = currentGrade;
total += currentGrade;
}
scanner.close();
System.out.println(Arrays.toString(grades));
System.out.println("Average grade " + (total / numOfStudents));
}
Here we first set the size of the double array (which will populate the grades). Then we fill in the double array using a for loop and getting user input via scanner.nextDouble().

Here's an example:
double[] values = { 2.2, 3.6, 6.3, 3.6, 2.8, 8.5, 8.3, 8.3, 1.2, 10, 2.2, 5.6 };
double sum = 0;
for (int i = 0; i < values.length; i++)
sum += values[i];
System.out.println("Average: " + sum / values.length);
That answers your initial question.
But if you also are having problems to populate the array, you can do it:
int MAX = 3;
double[] values = new double[MAX];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < MAX; i++) {
System.out.print("Type the grade: ");
values[i] = scanner.nextDouble();
}
scanner.close();

Related

Adding the code for ascending order of user input

import java.util.Scanner;
import java.util.Arrays;
public class searchSorting
{
public static void main (String[]args)
{
String line;
Scanner in = new Scanner(System.in);
System.out.print("How many numbers you want to input?: ");
line = in.nextLine();
System.out.print("Input Number 1: " );
line = in.nextLine();
System.out.print("Input Number 2: ");
line = in.nextLine();
System.out.print("Input Number 3: " );
line = in.nextLine();
System.out.print("Input Number 4: ");
line = in.nextLine();
System.out.print("Input Number 5: " );
line = in.nextLine();
}
public static void sortAscending (double[]arr)
{
Arrays.sort(arr);
System.out.printf("Sorted arr[] = %s",
Arrays.toString(arr));
}}
I am stuck on what the code is for putting the what the user inputs in ascending order. I have looked up and tried multiple resources on ascending order but nothing seems to work. I tried:
System.out.print("Input number 1: "+(i+1+":");
to try and add the inputs instead of writing all of them out but i was an unknown variable.
You should use an array to store input and a loop to read input.
System.out.print("How many numbers you want to input?: ");
int num = in.nextInt();
double[] arr = new double[num];
for(int i = 0; i < num; i++){
arr[i] = in.nextDouble();
}
sortAscending(arr);
You have to create an array. Then put the stuff you are reading into the array and after that call your method.
System.out.print("How many numbers you want to input?: ");
int amount = Integer.parseInt(in.nextLine());
double[] values = new double[amount];
for (int i = 0; i < values.length; i++) {
System.out.print("Input Number " + i + ": ");
values[i] = Double.parseDouble(in.nextLine());
}
sortAscending(values);
Note that the name sortAscending is not accurate, it is not just sorting (and then returning the result or in-place), but also printing. So maybe you should rename it to sortAndPrintAscending. Or just sort it and let your main method to the printing.
Or drop it completely, the method does not really serve any purpose as it is just calling Arrays.sort, might as well do that in main:
System.out.print("How many numbers you want to input?: ");
int amount = Integer.parseInt(in.nextLine());
double[] values = new double[amount];
for (int i = 0; i < values.length; i++) {
System.out.print("Input Number " + i + ": ");
values[i] = Double.parseDouble(in.nextLine());
}
Arrays.sort(values);
System.out.println("Sorted: " + Arrays.toString(values));

i wrote a code using array and methods to display numbers from the smallest o the largest numbers

i wrote a code using array and method that allow the user to enter any number of numbers and
display the numbers sorted from the smallest number to the largest number however the program works but it doesn't show the numbers here is the code that i wrote
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("How many numbers you want to enter? ");
int size = s.nextInt();
int i;
double[] numbers1 = new double[size];
System.out.println("Enter " + numbers1.length + " numbers: ");
getNumbers(numbers1);
double[] numbers2 = new double[numbers1.length];
for (i = 0; i < numbers1.length; i++) {
numbers2[i] = numbers1[i];
}
displayNumbers(numbers1);
System.out.println("The numbers after sorting are: ");
sortNumbers(numbers2);
displayNumbers(numbers2);
}
public static void getNumbers(double[] numbers) {
Scanner s = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = s.nextDouble();
}
}
public static void sortNumbers(double[] numbers) {
double temp;
double pass;
for (pass = 0; pass < numbers.length; pass++) {
for (int i = 0; i < numbers.length - 1; i++) {
if (numbers[i] > numbers[i + 1]) {
temp = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = temp;
}
}
}
}
public static void displayNumbers(double[] numbers) {
Scanner s = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = s.nextDouble();
System.out.print(numbers + " ");
}
System.out.println();
}
}
Your displayNumbers() method is wrong. In the loop you wrote:
numbers[i] = s.nextDouble();
System.out.print(numbers + " " );
You're trying to read again 4 doubles (everytime you call that method) and you're printing the whole array (which doesn't do what you'd expect). What you probably want is this:
System.out.print(numbers[i] + " " );
Your code is inefficient and unclear.
You don't need to create a new Scanner everytime and also why instead of println the array with Arrays.toString(double[]).
You should also make more cleaner instructions and variable names.
Here is an example of how the code should be
public static void main(String[] args)
{
//create a new Scanner with a name that defines it
Scanner scanner = new Scanner(System.in);
//print your instructions
System.out.println("How many numbers would you like to sort?");
//print "> " to let the user to know he should be entering values
System.out.print("> ");
//read the number the user has entered which we will define as how many numbers he would enter next
int totalNumbers = scanner.nextInt();
//create a new array the size of the total numbers
double[] unsortedNumbers = new double[totalNumbers];
//tell the user to enter his unsorted numbers
System.out.println("Enter your unsorted numbers: ");
//loop totalNumbers times until the whole unsortedNumbers is full
for(int index = 0; index < totalNumbers; index++)
{
System.out.print("> ");
unsortedNumbers[index] = scanner.nextDouble();
}
//Print the numbers he entered
System.out.println("You entered: ");
//Arrays.toString prints the array in format [number, number, ...]
System.out.println(Arrays.toString(unsortedNumbers));
//sort the arrays with Arrays.sort which sorts in ascending numerical order
Arrays.sort(unsortedNumbers);
//Print the final result - sorted numbers
System.out.println("Sorted: " + Arrays.toString(unsortedNumbers));
}

Two arrays in for loop

How can I use a for loop asking for student[0]'s grade, [1]'s etc? As of now, my for loop is pointless.
public static void main(String[] args) {
//declare grade and student array
int[] grades = new int[5];
String[] students = { "Bill", "Tom", "Mark", "Nic", "Miguel"};
Scanner sc = new Scanner(System.in);
for(int i = 0; i < grades.length; i++) {
grades[i] = i + 1;
//user input of student's grade
System.out.print("Enter grade for " + students[0] + ": ");
grades[0] = sc.nextInt();
System.out.print("Enter grade for " + students[1] + ": ");
grades[1] = sc.nextInt();
}
}
There must be a better way than hardcoding each student and grade array.
You can take the input and assign it in the loop itself.
for(int i = 0; i < grades.length; i++)
{
System.out.print("Enter grade for " + students[i] + ": ");
grades[i] = sc.nextInt();
}
That's all. You do not need to repeat code as that is what the loop will do by accessing each element with the index i.
However, I am not sure what the line grades[i] = i + 1; accomplishes in your code, since you anyways overwrite that value with the one you get as input.
If i am interpreting your question correct, what you want to do is accept grade values from user using for loop? My answer is as below, I have also printed grades which are accepted.
public static void main(String[] args) {
//declare grade and student array
int[] grades = new int[5];
String[] students = { "Bill", "Tom", "Mark", "Nic", "Miguel"};
Scanner sc = new Scanner(System.in);
System.out.println("please enter the grades of students");
for(int i=0; i<students.length; i++){
grades[i]= sc.nextInt();
}
for(int i=0; i<grades.length; i++){
System.out.println(grades[i]);
}
}

Array and looping structure

static Scanner scan = new Scanner(System.in);
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter number of arrays: ");
int noOfArrays = scan.nextInt();
String students[] = new String[noOfArrays];
System.out.println("----------------------");
while (true){
System.out.println("Enter Student names and scores:");
for (int idx = 0; idx < students.length; idx++){
int scores[] = new int[noOfArrays];
System.out.println("\t"+ (idx+1)+ ". ");
String studName = sc.nextLine();
students[idx] = studName;
for (int indx = 0; indx < scores.length; indx++){
System.out.println("\t Score: ");
int score = sc.nextInt();
scores[indx] = score;
}
}
}
}
I need to get this output, can somebody out there help me?
Enter number of arrays: 3
"----------------------
Enter Student names and scores:
1. Name
Score: 81
2. Name2
Score: 82
3. Name3
Score: 83
Your code have lots of bugs.
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of arrays: ");
int noOfArrays = scan.nextInt();
String students[] = new String[noOfArrays];
int idx=0;
System.out.println("----------------------");
System.out.println("Enter Student names and scores:");
int scores[]=new int[noOfArrays];
for ( idx = 0; idx < students.length; idx++){
System.out.print("\t"+ (idx+1)+ ". Name ");
sc.next(); // this is to skipping next line
students[idx] = sc.nextLine();
System.out.print("\tScore: ");
scores[idx] =sc.nextInt();
}
for (int i=0;i<students.length;i++) {
System.out.println("Name="+students[i]+"\tScore="+scores[i] );// for printing student name and score
}
}
output:
Enter number of arrays: 3
----------------------
Enter Student names and scores:
1. Name raja
Score: 54
2. Name gita
Score: 99
3. Name sita
Score: 88
The problem is here
for (int indx = 0; indx < scores.length; indx++){
System.out.println("\t Score: ");
int score = sc.nextInt();
the loop iterates until scores.length but scores is undefined.

Java - summing up Array list values

I am building an array that ask how many different inputs you have, then allowing you to enter each input. At the end I want to sum them up, but I keep getting an error.
Also when I go above 5 inputs, I lose one..... For example when I respond to the first question: Enter "10". When I start adding different numbers in it stops at nine. Please help.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
int[] numArr = new int[size];
System.out.println("Enter the number of CUs for each course: ");
for (int i=0; i<numArr.length; i++)
{
numArr[i] = input.nextInt();
}
int totalSum = numArr + totalSum;
System.out.print("The sum of the numbers is: " + totalSum);
Change your logic and code to the following:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
System.out.println("Enter the number of CUs for each course: ");
int totalSum = 0;
for (int i = 0; i < size; i++) {
totalSum+=input.nextInt();
}
System.out.print("The sum of the numbers is: " + totalSum);
try
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
int totalSum = 0;
for (int i=0; i< size; i++)
{
System.out.println("Enter the number of CUs for each course: ");
int cuNum = input.nextInt();
totalSum += cuNum ;
}
System.out.print("The sum of the numbers is: " + totalSum);
Pretty sure your intended result is
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
int[] numArr = new int[size];
int totalSum = 0;
System.out.println("Enter the number of CUs for each course: ");
for (int i=0; i<numArr.length; i++)
{
numArr[i] = input.nextInt();
totalSum += numArr[i];
}
System.out.print("The sum of the numbers is: " + totalSum);
}
Your other code didn't work mainly because of
int totalSum = numArr + totalSum;
You can't define totalSum to defines itself! And you can't just use numArr... numArr is an array - you have to access indexes, not the array as a whole!

Categories

Resources