Home Answers Viewqa Java-Beginners Please help with this code

 
 


Rod Jones
Please help with this code
0 Answer(s)      2 months ago
Posted in : Java Beginners

I need some help getting the Search method below to work with the menu, and I also cannot figure out how to get my bubble sort to work. I've spent a long time on this and think my brain is just fried at this point! :(

import java.util.*;

public class StudentProcessor2 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // read in how many students to process
        System.out.print("How many students?");
        int numStudents = input.nextInt();

        Student[] students = new Student[numStudents];

        for (int i = 0; i < numStudents; i++) {
            // read in student data from Scanner
            System.out.println("What is the students name?");
            String name = input.next();

            System.out.println("What is the students grade?");
//          System.out.println("***Type -1 if no more grades***");
            int grade = input.nextInt();
            Student student = new Student();
            student.setName(name);
            student.setGrade(grade);
            students[i] = student;
        }
        UserSelection(input, numStudents, students);
    }

    // show menu with options
    public static void UserSelection(Scanner input, int numStudents,
            Student[] students) {
        // Repeatedly process menu selections by the user.
        int choice;
        do {
            System.out.println();
            System.out.println("*** Student Exam Results Menu ***");
            System.out.println("(1) Display the results");
            System.out.println("(2) Display the average result");
            System.out.println("(3) Display the highest grade");
            System.out.println("(4) Display the lowest grade");
            System.out.println("(5) Search for specific result");
            System.out.println("(6) Search for student grade by name");
            System.out.println("(7) Sort the results in ascending order");
            System.out.println("(8) quit");
            choice = input.nextInt();

            if (choice == 1)
            {
                System.out.println(Arrays.toString(students));
            } else if (choice == 2)

            {
                double average = getAverage(students);
                System.out.println("average grade = " + average);
            } else if (choice == 3)

            {
                int high = getHighest(students);
                System.out.println("high grade = " + high);
            } else if (choice == 4)

            {
                int low = getLowest(students);
                System.out.println("low grade = " + low);

            } else if (choice == 5)

            {
                System.out.print("Result to look for: ");
                int grade = input.nextInt();
                 if (result(grade, students)) {
                 System.out.println(grade +
                 " is in the collection of grades.");
                 } else

                 {
                 System.out.println(grade +
                 " is not in the collection of grades.");
                 }

                 } else if (choice == 6)
                 {
                System.out.print("Student to search for: ");
                String name = input.next();
                if (search(name, students))
                {
                    System.out.println(name +
                    " is in the list of Students.");
                } else
                {
                System.out.println(name +
                " is not in the list of Students");
                }
            }

        } while (choice != 8);
    }

    // get Lower Grade
    private static int getLowest(Student[] students) {
        int lowest = 100;
        Student result = null;
        for (Student student : students) {
            if (student.getGrade() < lowest) {
                lowest = student.getGrade();
                result = student;
            }
        }
        return result.getGrade();
    }

    // get Highest grade
    private static int getHighest(Student[] students) {
        int highest = 0;
        Student result = null;
        for (Student student : students) {
            if (student.getGrade() > highest) {
                highest = student.getGrade();
                result = student;
            }
        }
        return result.getGrade();
    }

    // get Average grade
    private static double getAverage(Student[] students) {
        int total = 0;
        for (Student student : students) {
            total += student.getGrade();
        }
        return total / students.length;
    }

    // Search for student
    private static Student search(String name, Student[] students) {
        Student result = null;
        for (Student student : students) {
            if (student.getName().equalsIgnoreCase(name)) {
                result = student;
                break;
            }
        }
        return result;
    }

    private static boolean result(int grade, Student[] students) {
        for (Student student : students) {
            if (student.getGrade() == grade) {
                return true;
            }
        }
        return false;
    }

    // !Bubble sort goes here!
    private void sort(Student[] students) {

        Student hold; // temporary holding area for swap

        for (int i = 0; i < students.length; i++) { // passes
            Student current = students[i];
            Student next = students[i + 1];
            if (current.getGrade() > next.getGrade()) // one comparison
            {
                hold = students[i]; // one swap
                students[i] = students[i + 1];
                students[i + 1] = hold;
            }
        }
    }
}
View Answers









Related Pages:
Please help with this code
Please help with this code  I need some help getting the Search method below to work with the menu, and I also cannot figure out how to get my bubble sort to work. I've spent a long time on this and think my brain is just fried
please help
please help  please send me the code of dynamic stack in java without using the built in functions
help please
file.. Or atleast help me with code here.. I tried checking session alive...help please  hi i am done with register application using jsps servlets htmls. But i couldnt imp one thing that.. Wen u login to ur account browser
could anyone please help with the code.
could anyone please help with the code.  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException...(request, response); } } could anyone please check the code. If i enter
ajax code please help to solve this........
ajax code please help to solve this.  in this i am trying to get data... null; } please help me when i am running this it show an error...;<a href="help.jsp">help instruction</a></li> <
urgent...pleAse help me.....please!
urgent...pleAse help me.....please!  please help me urgent! how can i do dictionary with the use of array code in java, where i will type the word then the corresponding meaning for that word will appear...thanks
please help me.
please help me.  Please send me a code of template in opencms and its procedure.so i can implement the code. Thanks trinath
Please help me.
Please help me.  Hi i am trinath in below there is a url.In that url there is a code of edit a jsp page.I understand that code but only one thing i not get it i.e; What is the work of "id".and what is the data type of id? http
please help me
please help me  Dear sir, I have a problem. How to write JSP coding... before. This name list should get from the database. Please help me. By the way, I'm using access database and jsp code. Thank you
help please!!! T_T
help please!!! T_T  what is wrong in this?: import java.io.*; class...("please enter your name:"); name1= input.readline(); System.out.println("please..."+ "name2"+ are+ friends); }   We have modified your code. Check
please help me to write a code for this program
please help me to write a code for this program   1 1 1 1 2 2 1 1 3 4 3 1 1 4 7 7 4 1
please help me to write a code for this program
please help me to write a code for this program   1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
help me please
help me please  how can i do dictionary with the use of array code in java, where i will type the word then the corresponding meaning for that word will appear...thanks
Please help me
Please help me  Hi Sir, please send me the code for the following progrems... 1) all sets are integer type: input: set1={10,20,30,40} set2={15,25,35} output: union={10,15,20,25,30,35,40} 2) input: "Hi what
Please can you help
Please can you help  I have a some code which uses a textarea comment...; Please could you change the code so it will do this Please. Many thanks. Colin... a new line rather than replacing it. Here is the code: <html> <
please help me to give code - Java Beginners
please help me to give code  Write a function, sliding(word, num)that behaves as follows. It should print out each slice of the original word having length num, aligned vertically as shown below. A call to sliding(examples, 4
please help me to give code - Java Beginners
please help me to give code  Write a function with a signature cheerlead(word) that prints a typical cheer as follows. The word robot: Gimme an R Gimme an O Gimme a B Gimme an O Gimme a T What did you give me? ROBOT
Not sure whats wrong with my code HELP PLEASE?!?!
Not sure whats wrong with my code HELP PLEASE?!?!  I cant figure out what I am doing wrong in my code can anyone help me out??? Grades function computeGrade( ) { var hw, lab, midt, fin, avg; hw = parseFloat
Load function ***PLEASE HELP
Load function ***PLEASE HELP   Hi I am writing a program that tracks... LOAD implementations. Thank you so much for your help. I really need help to get this done as this project is due on monday for me. My code right now looks
need help...................please
on this area. Please help me. By the way, I'm using access Database and JSP coding...need help...................please  I have a problem in my JSP Coding...("GET",url,true) xmlHttp.send(null) } else{ alert("Please Select Employee Id
please help me here
please help me here  please show me how can this show the right output that i need please continue doing this program using only if else and do while please please" here is the problem Automatic Teller Machine [B] Balance [D
please help me here
please help me here  please show me how can this show the right output that i need please continue doing this program using only if else and do while please please" here is the problem Automatic Teller Machine [B] Balance [D
help please - Java Beginners
help please  i wrote this program but the function newLine dosnt work with me and i dont know where is exactly the error this is the code i... friend, We check the code having some changes to correct the code
Java Variables Help Please!
Java Variables Help Please!  Hi, I just started with java and i need help with my school project, this is it so far: import java.util.*; public...("--------------------"); System.out.println("2.From The Question above, What is the ASCII code of the correct
please help me to give code - Java Beginners
please help me to give code  Write a program that prints an n-level stair case made of text. The user should choose the text character and the number of stairs in the stair case * ** *** ****   Hi friend
please help me to give code - Java Beginners
please help me to give code  Write a program that uses loops to generate an n x n times table. The program should get n from the user. As a model here is a 4 x4 version: | 1 2 3 4
Please help me fix this code - MobileApplications
Please help me fix this code   Please help me in this area of code... in the background of the forms in this code i want to sum all expenses amount... expenses)", "Please fill all required field \n \n * This signify required field
please help me to give code - Java Beginners
please help me to give code  Write a program that reads a file named famous.txt and prints out the line with the longest length. In the case of a tie, you may print out only one of them. For example in the file: Alan Turing
please help me to overcome this problem
please help me to overcome this problem  how to make it easy to write java code. can u please give me suggestions
Please help me to modify my java code from php code
]; } } I tried like this (see below JSP code) ... but this is not giving me the exact result as the above PHP code is giving. So please help me to convert...Modify Java code from PHP Code  i want to covert this php code int
Please help me to modify my java code from php code
Please help me to modify my java code from php code  i want to covert...) ... but this is not giving me the exact result as the above PHP code is giving. So please... same, as they alter it before. Please help me on this topic "how to order rows
Help me please!!! - Java Beginners
Help me please!!!  im badly needing the complete code for this project in java!!! can you please help me???!!! it is about 1-dimensional array... the answers 8Hours from now!!! Kindly help me!! please!!please!!! NOTE
please help me - Java Beginners
please help me   I have some error in this programe //write acomputer programe using java to generate following series : //output: //1,2,3,0... have done modification in your code and here is the code according to the series
need to fix errors please help
need to fix errors please help  it does have 2 errors what should i...; String name2; System.out.println("please enter your name:"); name1= input.readline(); System.out.println("please enter your friend's name:"); name2
Help needed for Form Validation please help!
Help needed for Form Validation please help!  I have created a page... clicking the submit button for sign up it also goes through the jsp code and returns incorrect Username/password combination. So how can I make the jsp code confined
Please help need quick!!! Thanks
Please help need quick!!! Thanks  hey i keep getting stupid compile... simulation program of sorts here is my code: RentforthBoatRace.java public...(); ^ 6 errors any help would be appreciated guys. Thanks so
Help please, some strange errors
Help please, some strange errors  Sorry about this messy formatting. As a beginner in java i got no idea which part of the code is creating... play the error hits on to the console view. Can someone tell me what in the code
please any one can help me to write a code for this question?
please any one can help me to write a code for this question?  1) Copy one file content to other? 2) Count the number of words in a file
Please help me to write a code for add contacts to groups in a website?
Please help me to write a code for add contacts to groups in a website?   Iam developing a site. In that site I need to write code for Groups... send a code for this? Please make it fast
HELP
HELP  I need this code to open in a new web browser. but no matter what i do it wont please help?? Directive 055 Storage Requirements for the Upstream Petroleum Industry
please any one can help me to write a code for this question?
please any one can help me to write a code for this question?  Q 1) In a class first day 25 students are joined. After two days that total students will increased to 60. We can develop a program by using ArrayList concept
please help//
please help//  Number square cube 1 1 1 3 9 27 5 25 125 7 49 343 9 81 729 total 165 1225 â?? this is the ouput..;;; i
please help//
please help//  Number square cube 1 1 1 3 9 27 5 25 125 7 49 343 9 81 729 total 165 1225 â?? this is the ouput..;;; i
please help//
please help//  Number square cube 1 1 1 3 9 27 5 25 125 7 49 343 9 81 729 total 165 1225 â?? this is the ouput..;;; i
Please Help
Please Help  How do I create an attribute that represents the following: A grayscale 'color' value, that is, an integer between 0 and 255 inclusive, where 0 corresponds to the darkest black and 255 to the brightest white
Please Help
Please Help  How do I create an attribute that represents the following: A grayscale â??colorâ?? value, that is, an integer between 0 and 255 inclusive, where 0 corresponds to the darkest black and 255 to the brightest white
help please?
help please?  Define a class named Circle with the following properties: â?¢ An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both data fields
help please?
help please?  Define a class named Circle with the following properties: List item â?¢ An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both
help please?
help please?  Define a class named Circle with the following properties: List item An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both
help please?
help please?  Define a class named Circle with the following properties: List item An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.