-2

I need help making a program that will take the int input of the user as the number of students. At the moment I have to manually add the students in the code if I want more student. ive added my other class aswell. please help if possible.

import java.util.Scanner;

// the name of our class its public
public class ClassArray {

 //void main
       public static void main (String[] args){

        //declare class
        Student[] s = new Student[2];
        s[0] = new Student();
        s[1] = new Student();

        //call functions

        s[0].getdata();
        s[1].getdata();

        s[0].finalmark();
        s[1].finalmark();

        s[0].finalgrade();
        s[1].finalgrade();

        System.out.printf("Name\tDefinitive\tLetter\tTest 1\tTest 2\tAssignments\tFinalExam \n");
        s[0].print();
        s[1].print();
       }
    }           
 }

        //declare class
    public static class Student {

        //declare variables.
        private Double finalmark;
        private int test1,test2,assignments1,finalexam;
        private String studentname,finalgrade;


        //functions should be public if needed to access from other class
        public void getdata()
                {
                //print message to enter numbers
                Scanner input = new Scanner(System.in);
                System.out.println("Enter name of student:");
                studentname = input.next();

                while (!studentname.matches("[a-zA-Z]+")) { // Checks to see if only letters are used in the name

                    System.out.println("Please re-enter your name, use alphabets only");
                    studentname = input.nextLine(); // if anything other than letters are used, the user must re-enter his/her name using letters
                   }

                System.out.println("Enter mark test 1 for student:");
                test1 = input.nextInt();

                while (test1 > 100 || test1 < 0){

                    System.out.println("Please enter a double value between 0 and 100");
                    while(!input.hasNextInt()){
                        input.next();
                    }

                    test1 = input.nextInt();
                }

                System.out.println("Enter mark test 2 for student:");
                test2 = input.nextInt();

                while (test2 > 100 || test2 < 0){

                    System.out.println("Please enter a double value between 0 and 100");
                    while(!input.hasNextInt()){
                       input.next() ;
                    }
                      test2 = input.nextInt();
                }

                System.out.println("Enter mark assignments for student:");
                assignments1 = input.nextInt();

                while (assignments1 > 100 || assignments1 < 0){

                    System.out.println("Please enter a double value between 0 and 100");

                    while(!input.hasNextInt()){
                       input.next() ;
                    }
                      assignments1 = input.nextInt();
                }  

                System.out.println("Enter mark final exam for student:");
                finalexam = input.nextInt();
                while ( finalexam > 100 ||  finalexam < 0){

                    System.out.println("Please enter a double value between 0 and 100");

                    while(!input.hasNextInt()){
                       input.next() ;
                    }
                      finalexam = input.nextInt();
                }


            }




         public void finalmark(){

             finalmark = (test1 * 0.15) + (test2 * 0.25) + (assignments1 * 0.25) + (finalexam * 
         0.35);

     }

         public void finalgrade()
               {
            if(finalmark >= 100)
                 finalgrade="A+";
               else if(finalmark >= 90)
                  finalgrade="A+";
               else if(finalmark >= 80)
                 finalgrade="A";
               else if(finalmark >= 75)
                  finalgrade="B+";
               else if(finalmark >= 70)
                  finalgrade="B";
               else if(finalmark >= 65)
                   finalgrade="C+";
               else if(finalmark >= 60)
                   finalgrade="C";
               else if(finalmark >= 50)
                   finalgrade="D";
               else 
                    finalgrade="F";
         } 



        public void print(){

            System.out.printf("%s\t%.2f\t%s\t%d\t%d\t%d\t\t%d\n", studentname, finalmark,              
            finalgrade, test1, test2, assignments1, finalexam);

    }


      }
tutak
  • 1,120
  • 1
  • 15
  • 28
user3440136
  • 1
  • 1
  • 1
  • 4

3 Answers3

2

Something like this:

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Number of Students:\t");
int numStudents = Integer.parseInt(scanner.nextLine());

Your complete code would be:

import java.util.Scanner;

public class ClassArray {

   public static void main (String[] args){

       Scanner scanner = new Scanner(System.in);

       System.out.print("Enter Number of Students:\t");
       int numStudents = Integer.parseInt(scanner.nextLine());


       Student[] s = new Student[numStudents];

       for(int i = 0; i < numStudents; i++ ){

            s[i] = new Student();
            s[i].getdata();
            s[i].finalmark();
            s[i].finalgrade();

       }

       System.out.printf("Name\tDefinitive\tLetter\tTest 1\tTest 2\tAssignments\tFinalExam \n");

       //Here it will iterate and print out the stored data as soon as the user has finished adding it.
       for(int j = 0; j < numStudents; j++ ){
         s[j].print();
    }

 }
tutak
  • 1,120
  • 1
  • 15
  • 28
  • ive tried this but would there be a way to print all the student data after inputting the data for each? – user3440136 Apr 17 '14 at 17:35
  • @user3440136 sure, you can iterate through the objects outputting their fields or methods. But I would need to see what is the structure of objects (Student) to figure out how to output their data. – tutak Apr 17 '14 at 18:02
  • hey, ive added the other class with the structure of the objects for student. please help if possible :) – user3440136 Apr 17 '14 at 18:30
  • @user3440136 edited the code, just had to take that one "printout" out of the first for cycle. Then introduced a new for cycle to iterate through the objects and printout the stored data. – tutak Apr 17 '14 at 19:57
  • hey, would it be possible to find the average of the listed definitives? i think i would have needed to use ArrayList to get this program running better. – user3440136 Apr 17 '14 at 21:02
1

Simply,

 import java.util.Scanner;

 public class ClassArray {

    public static void main (String[] args) {

       Scanner input= new Scanner(System.in); // create Scanner object

       System.out.print("Enter The Number of Students: ");

       int numOfStudents = input.nextInt(); // input an integer value

       // do whatever you like

   }// Ends main
}

Here I created an object of class Scanner as input and I've called the method nextInt() by the object of class Scanner (input).

0

See this post for user input: How can I get the user input in Java?

You also should not use an array which you have defined as having a set number of elements, in your example 2. Instead, consider an ArrayList of objects type Student which for your purposes can accept any number of Students.

ArrayList<Student> s = new ArrayList<Student>();
//Example add student
Student student1 = new Student();
s.add(student1);

See this post for ArrayList: Java: ArrayList of String Arrays

Community
  • 1
  • 1
asporter
  • 71
  • 7