1

Currently working on a project that requests the number of classes a student has left, the amount of classes taken per term, and returns the number of terms left to graduate. I'm having some trouble figuring out how to cast an integer array to a double array to figure out the amount of terms needed. Also the result needs to round up as well. I'm very new to this so any suggestions are much appreciated and critiques on how to clean up my code, thanks in advance.

import java.util.Scanner;

public class main {

public static void main (String[] args) {
    
    Scanner input = new Scanner (System.in);

    System.out.print("Enter the number of rows: ");
    int rows = input.nextInt();
    System.out.print("Enter the number of columns: ");
    int columns = input.nextInt();
    
    int [][] studentArray = new int [rows][columns];
    
    
    for (int i = 0; i <= rows-1; i++) {
        for (int j = 0; j <= columns-1; j++) {
             
            if (j==0) {
                System.out.print("Please enter the number of classes left for student " +(i+1)+ " : ");
                studentArray[i][j] = input.nextInt();
                }
            
            else if (j>0) {
                System.out.print("Enter the number of classes taken per term : ");
                studentArray[i][j] = input.nextInt();
                
                while (studentArray[i][j] >= 6) {
                    System.out.println("The number of classes per term for student " +(i+1)+ " is not valid!");
                    System.out.print("Enter the number of classes taken per term : ");
                    studentArray[i][j] = input.nextInt();
                }
                }
            }
        }
    
    divide(studentArray);
}

public static void divide(int termsLeft[][]) {
for (int k = 0; k < termsLeft.length; k++) {
    double result = termsLeft[k][0] / termsLeft[k][1];
    if (k>=0) {
    System.out.println("Student " +(k+1)+ " has " + result + " terms left to graduate.");
    }
    }
}

}

2
  • Casting is not conversion; it does not change the underlying data value at all. An int[] is an int[], and not a double[]. Commented Aug 3, 2020 at 4:40
  • When you say "round up", do you mean, for example that 2.1, ..., 2.5, ..., 2.9 would all round up to 3? Or that 2.1, ... 2.4 would round to 2 and 2.5, 2.6, ..., 2.9 would round to 3? Commented Aug 3, 2020 at 4:46

1 Answer 1

1

First of all their are some problems in your code which make it very ineffecient.

1)

for (int i = 0; i <= rows-1; i++) {
    for (int j = 0; j <= columns-1; j++) {
    }

In your inner and outer loop you don't have to use <= sign and subtract 1 from right value. you can use i < rows & j < columns.

2)

if (k>=0) {
    System.out.println(...)
    }

Their is no need to use the if statement as it is always true.

Now coming to your question.

Your can use the Math.round method to round double values into long (can store larger values than int).

    double result = termsLeft[k][0]/termsLeft[k][1];
    long round_result = Math.round(result);

So your final code is below :

import java.util.Scanner;

public class Main {

    public static void main (String[] args) {

        Scanner input = new Scanner (System.in);

        System.out.print("Enter the number of rows: ");
        int rows = input.nextInt();
        System.out.print("Enter the number of columns: ");
        int columns = input.nextInt();

        int [][] studentArray = new int [rows][columns];


        for (int i = 0; i <= rows-1; i++) {
            for (int j = 0; j <= columns-1; j++) {
                if (j==0) {
                    System.out.print("Please enter the number of classes left for student " +(i+1)+ " : ");
                    studentArray[i][j] = input.nextInt();
                }

                else if (j>0) {
                    System.out.print("Enter the number of classes taken per term : ");
                    studentArray[i][j] = input.nextInt();

                    while (studentArray[i][j] >= 6) {
                        System.out.println("The number of classes per term for student " +(i+1)+ " is not valid!");
                        System.out.print("Enter the number of classes taken per term : ");
                        studentArray[i][j] = input.nextInt();
                    }
                }
            }
        }

        divide(studentArray);
    }

    public static void divide(int[][] termsLeft) {
        for (int k = 0; k < termsLeft.length; k++) {
            double result = termsLeft[k][0]/termsLeft[k][1];
            long round_result = Math.round(result);
            System.out.println("Student " +(k+1)+ " has " + round_result + " terms left to graduate.");
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.