Coco
Coco

Reputation: 45

Changing the values of a chosen element in an one dimentional array in JAVA program

Here is my program which stores grades (elements) into a Gradebook (array) enter image description here

I'm basically trying to allow the user to be able to change whichever grade the (element) they want in the Gradebook(array) as many times as they would like (loop basically). I've given the user the option to enter the index (which should start from 1 rather 0, in order to avoid confusion for the user) but after that step I'm stuck. I'm not sure how to search for the element in the array and then ask the user to replace it...

Here is my code so far:

import java.lang.reflect.Array;
import java.util.Scanner;

public class NewGradeBook {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        //Ask user to enter the amount of grades
        int grades = NumberReader.readPositiveInt(input, "Please enter the number of grades: ",
                "Error: Invalid data entered");

        int numOfGrades = grades;

        double[] mogrades = new double[numOfGrades];

        for (int i = 0; i < mogrades.length; i++) {

            //Allows user to enter each individual grade
            System.out.println("Enter grade (limit to two decimal places)" + (i + 1) + ": ");

            //Stores grades in array
            mogrades[i] = NumberReader.readPositiveDouble(input, "Enter grade " + (i+1) + " :", "Invalid data entered");

        }

        System.out.println("The Grade book contains: ");

        printArray(mogrades);

        System.out.println("___________________________");

        //Ask user if what grade they would like to change
        int index = NumberReader.readPositiveInt(input,
                "Enter the index of the grade to be changed: (1 to " + grades + ") : ", "Invalid index input");

    }

    public static void printArray(double[] mogrades) {

        for (int i = 0; i < mogrades.length; i++) {

            System.out.print("Grade " + (i + 1) + " is: " + mogrades[i] + ", ");

        }

    }

}

Any help would be appreciated.

Upvotes: 0

Views: 215

Answers (1)

nhouser9
nhouser9

Reputation: 6780

The structure should be similar to this:

 System.out.println("___________________________");

 System.out.println("Make changes? Enter Y or N");
 String makeChanges = System.console().readLine();
 while (makeChanges.equals("Y")) {
  //Ask user if what grade they would like to change
  int index = NumberReader.readPositiveInt(input,
   "Enter the index of the grade to be changed: (1 to " + grades + ") : ", "Invalid index input");

  System.out.println("Enter grade (limit to two decimal places)" + index + ": ");
  //offset the index by one
  mogrades[index - 1] = NumberReader.readPositiveDouble(input, "Enter grade " + index + " :", "Invalid data entered");

  System.out.println("Make changes? Enter Y or N");
  makeChanges = System.console().readLine();
 }
}

You want to continue looping while they still want to make changes.

Upvotes: 0

Related Questions