0

I am struggling with the method printHighest and printLowest, I need it to return the student score, name and ID, but I can't figure it out the only way that I was able to get it to work was only with the score, for the most part, the rest of the programs works just fine, I am just struggling with that part of the code.

import java.util.*;

public class FinalprojectV2
{

    public static void main(String args[])
    {
        Scanner console = new Scanner(System.in);
        int[] studentID = new int [5];
        String[] studentName = new String [5];
        int [] studentScore = new int [5];

        for (int i = 0; i < 5; i++)
        { 
            System.out.println("Student Number " + (i+1) + "");

            System.out.println("Student ID Number: ");
            studentID [i] = console.nextInt();

            System.out.println(" Student Name: " + console.nextLine()+"");
            studentName [i] = console.nextLine();

            System.out.println("Grade: ");
            studentScore [i] = console.nextInt();
        }

        printRoaster(studentID, studentName, studentScore);

        int max = printLowest(studentScore);
        System.out.println("Highest score is: "+ max);

        int min = printHighest(studentScore);
        System.out.println("Lowest score is: "+ min);
    }

    public static void printRoaster(int[] studentID, String []studentName, int [] studentScore)
    {
        for (int i = 0; i < 5; i++)
        { 
            java.util.Arrays.sort(studentID);
            java.util.Arrays.sort(studentName);
            java.util.Arrays.sort(studentScore);
        }

        for(int i=0;i<5;i++)
        {
            System.out.println(studentID[i] + " " + studentName [i] + " " + studentScore [i]  + " ");
        }
    }

    public static int printLowest(int[] inputArray)
    { 
        int maxValue = inputArray[0]; 

        for(int i=1;i < inputArray.length;i++){ 
            if(inputArray[i] > maxValue){ 
                maxValue = inputArray[i]; 
            }
        } 

        return maxValue; 
    }

    public static int printHighest(int[] inputArray){ 
        int minValue = inputArray[0];

        for(int i=1;i<inputArray.length;i++){ 
            if(inputArray[i] < minValue){ 
                minValue = inputArray[i];
            }
        } 

        return minValue; 
    } 
}
3
  • 1
    You could return instead the index of the max score then use it to retrieve the id and the name of the student with that score using that index. Commented May 24, 2018 at 3:13
  • can you provide a sample input and sample output Commented May 24, 2018 at 3:18
  • could you show me an example of how i would implement that into the code? Commented May 24, 2018 at 20:54

2 Answers 2

1

Better solution would be - create a student class with id, name and score.

And from printHighest and printLowest methods, return appropriate objects of Student class.

Hope you understood.

class Student {
    int studentID;

    String studentName;

     int studentScore ;
........
}

In main method, create

Student[] students = new Student [5];
Sign up to request clarification or add additional context in comments.

Comments

0
import java.util.*;

import static java.lang.Integer.MIN_VALUE;

public class FinalprojectV2

{

    public static void main(String args[])

    {

        Scanner console = new Scanner(System.in);

        int[] studentID = new int [5];

        String[] studentName = new String [5];

        int [] studentScore = new int [5];

        for (int i = 0; i < 5; i++)

        {

            System.out.println("Student Number " + (i+1) + "");

            System.out.println("Student ID Number: ");

            studentID [i] = console.nextInt();

            System.out.println(" Student Name: " + console.nextLine()+"");

            studentName [i] = console.nextLine();

            System.out.println("Grade: ");

            studentScore [i] = console.nextInt();

        }

        printRoaster(studentID, studentName, studentScore);

//        int max = printLowest(studentScore);

        printLowest(studentScore,studentID);

//        System.out.println("Highest score is: "+ );

        int min = printHighest(studentScore);

        System.out.println("Lowest score is: "+ min);

    }

    public static void printRoaster(int[] studentID, String []studentName, int []

            studentScore)

    {

        for (int i = 0; i < 5; i++)

        {

            java.util.Arrays.sort(studentID);

            java.util.Arrays.sort(studentName);

            java.util.Arrays.sort(studentScore);

        }


        for(int i=0;i<5;i++)

        {

            System.out.println(studentID[i] + " " + studentName [i] + " " + studentScore [i]

                    + " ");

        }

    }

    public static void printLowest(int[] inputArray, int[] id)

    {
        int pos = MIN_VALUE;
        int maxValue = inputArray[0];

        for(int i=1;i < inputArray.length;i++){

            if(inputArray[i] > maxValue){

                pos = i;
                maxValue = inputArray[i];

            }

        }

        print(maxValue,id[pos]);

    }

    public static void print(int score, int id){
        System.out.println("Highest score is: "+ score +" student ID is: "+id);
    }

    public static int printHighest(int[] inputArray){

        int minValue = inputArray[0];

        for(int i=1;i<inputArray.length;i++){

            if(inputArray[i] < minValue){

                minValue = inputArray[i];

            }

        }

        return minValue;

    }

}

I changed your code a little bit, so you will need to finish another method by yourself. Here i just separated method with print and added id array into calling printLowest mehod

1 Comment

This is the solution which introduce just a little changes in your code. The right and good practice is to write like @A J showed.

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.