简体   繁体   中英

How to allow for user input java

I'm not sure how to make my code so that it allows the user to input the information, I know how to create an arraylist database but not how to make it so that once the user enters the information and presses 'enter' the database is complete...can anyone help me out? (in java)

thanks for the start tips for the user input!! They were so helpful! okay so now I have:

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

public class BestStudent 
{
private String studentName;
private String studentGPA;

public BestStudent()
{

}

public BestStudent(String nName, String nGPA)
{
    this.studentName = nName;
    this.studentGPA = nGPA;
}

public void setStudentName(String newStudentName)
{
    studentName = newStudentName;
}

public void setStudentGpa(String newStudentGPA)
{
    studentGPA = newStudentGPA;
}

public String getStudentName()
{
    return studentName;
}

public String getStudentGPA()
{
    return studentGPA;
}

public static void main(String[] args)
{
    List<BestStudent> students = new ArrayList<BestStudent>();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter number of students");
    int countStudents = input.nextInt();

    for(int i = 0; i < countStudents; i++)
    {
    BestStudent student = new BestStudent();
    System.out.println("Enter student details" + i);

    System.out.println("Enter student name");
    student.setStudentName(input.next());

    System.out.println("Enter student GPA");
    student.setStudentGpa(input.next());
    students.add(student);


    }

}
}

Is there a way to make it so the program calculates the highest GPA entered? I'm not looking for anyone to just give me answers, just a push in the right direction would be great, thanks so much!!

Since you know how to create an ArrayList I don't need to go over that.

Look in the Scanner class. It makes getting input easy.

Here is a short example:

Scanner input = new Scanner(System.in);
System.out.print("Input: ");
String str = input.nextLine();
System.out.print(str);

Don't forget to import java.util.Scanner .

The code will basically instantiate a new Scanner object, then Print out "Input: " and wait for input. Once you press enter after trying what you have it will print out what you typed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM