1

I am trying to take input from the user on student data. First, I ask the user how many students they are inputing data on. Then, the code asks the user for the data on the exact number of students the user put in for the first question.

Below is the start of my code. I am having issues in getting the user input after the initial variable. I need to take that variable, say the user enters 5, I need to prompt the user 5 times to input the Students Name and grade. Like so:

Student 1 last name:
Student 1 first name:
Student 1 grade:

Student 2 last name:

I have to use an array, I just need to figure out how to properly get the user input.

import java.util.Scanner;

public class StudentScoresApp {

    public static Score score = new Score();
    private static Student student;

    public static void main(String[] args) {
        System.out.println("Welcome to the Student Scores Application.\n");
        getStudentScores();
    }

    public static void getStudentScores() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter number of students to enter:   ");
        int num = input.nextInt();
        int [] a = new int[num];
        for (int i = 0 ; i < num ; i++); {
            System.out.print("Enter Student " + (i + 1) + " last name:");
            a[i] = in.nextInt();
        }
    }
}
1
  • I am having issues: WHICH issues? Also, how do you plan to store a last name into an int? Commented Mar 2, 2013 at 16:32

3 Answers 3

1
String [] lastNames = new String [num];
String [] firstNames = new String [num];
int [] grades = new int [num];

for (int i = 0; i < num; i++)
{
    System.out.print ("Enter Student " + (i + 1) + " last name:");
    lastNames [i] = in.nextLine ();
    System.out.print ("Enter Student " + (i + 1) + " first name:");
    firstNames [i] = in.nextLine ();
    System.out.print ("Enter Student " + (i + 1) + " grade:");
    gradess [i] = in.nextInt ();
}
Sign up to request clarification or add additional context in comments.

Comments

1

In my opinion, it isn't a good practice to handle associations between arrays, anyway it's up to you to decide your design. If you want to do that, then @Mikhail Vladimirov 's suggestion is the way to go.

On the other hand, just design a class for your needs, and store objects of the class in an array or list.

public class StudentScore{
    String firstName;
    String lastName;
    int grade;

    pulbic StudnetScore(String firstName, String lastName, int grade){
        this.firstName = firstName;
        this.lastName = lastName;
        this.grade = grade;
    }

    //getters(), setters()
}

In the main class:

StudentScore[] studentScores = new StudentScore[num];
for (int i = 0; i < studentScores.length; i++){
    System.out.print ("Enter Student " + (i + 1) + " last name:");
    String lastName = in.nextLine ();
    System.out.print ("Enter Student " + (i + 1) + " first name:");
    String firstName = in.nextLine ();
    System.out.print ("Enter Student " + (i + 1) + " grade:");
    int grade = in.nextInt ();
    studentScores[i] = new StudentScore(firstName,lastName,grade);
}

Comments

0

I recommend you to use an arrayList to store the Student objects. Consider the below example for better understanding:

First, you can create a model class to store the student details with getters() & setters(). It must look something like this:

package com.stack.overflow.works.model;

public class Student {

    private String firstName;
    private String lastName;
    private int score;

    public Student() {}

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

Next, you may create your StudentScoresApp as shown below to read the input from user:

package com.stack.overflow.works.main;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.stack.overflow.works.model.Student;

public class StudentScoresApp {

    public static List<Student> getStudentScores() {
        List<Student> students = new ArrayList<Student>();
        Student student = null;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter number of students to enter: ");
        int numberOfStudents = scanner.nextInt();

        for (int i = 0; i < numberOfStudents; i++) {
            student = new Student();
            System.out.print("Enter Student " + (i + 1) + " First Name:");
            String firstName = scanner.next();
            student.setFirstName(firstName);
            System.out.print("Enter Student " + (i + 1) + " Last Name:");
            String lastName = scanner.next();
            student.setLastName(lastName);
            System.out.print("Enter Student " + (i + 1) + " Score:");
            int score = scanner.nextInt();
            student.setScore(score);
            students.add(student);
        }
        scanner.close();

        return students;
    }

    public static void displayStudentScores(List<Student> students) {
        int i = 1;
        for (Student student: students) {
            System.out.println("Student " + (i) + " First Name:" + student.getFirstName());
            System.out.println("Student " + (i) + " Last Name:" + student.getLastName());
            System.out.println("Student " + (i) + " Score:" + student.getScore());
            i++;
        }
    }

    public static void main(String[] args) {
        System.out.println("Welcome to the Student Scores Application");
        System.out.println("*****************************************");
        List<Student> students = StudentScoresApp.getStudentScores();
        System.out.println();
        System.out.println("Displaying Student Scores:");
        System.out.println("*************************");
        StudentScoresApp.displayStudentScores(students);
    }

}

Now, you can run the StudentScoresApp. Sample test result is shown below:

Welcome to the Student Scores Application
*****************************************
Enter number of students to enter: 3
Enter Student 1 First Name:Sandeep
Enter Student 1 Last Name:Thulaseedharan
Enter Student 1 Score:100
Enter Student 2 First Name:Sathya
Enter Student 2 Last Name:Narayanan
Enter Student 2 Score:100
Enter Student 3 First Name:Jayakrishnan
Enter Student 3 Last Name:Lal
Enter Student 3 Score:100

Displaying Student Scores:
*************************
Student 1 First Name:Sandeep
Student 1 Last Name:Thulaseedharan
Student 1 Score:100
Student 2 First Name:Sathya
Student 2 Last Name:Narayanan
Student 2 Score:100
Student 3 First Name:Jayakrishnan
Student 3 Last Name:Lal
Student 3 Score:100

Hope this helps..

Thank you..Happy coding...

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.