Comp Sci Programming with Java -- Reading From the Terminal

Click For Summary
To read input from the terminal in Java, the Scanner class is essential. Users can utilize the Scanner to capture user input and process it accordingly. The discussion highlights the need for modifying existing code to implement terminal input functionality. Reference to Oracle's documentation on the Scanner class provides additional guidance and examples for parsing input. Implementing these changes will enhance the program's interactivity and functionality.
ver_mathstats
Messages
258
Reaction score
21
Homework Statement
Make the required changes to the code so that it reads the input from the terminal. Your code should be able to handle an arbitrary but valid input (for example if we replace the order of the first two lines of the above example we still have a valid input). But if we have repetitive course names, or a typo in the command, like COURRSE, then the input is not valid and we don't check your code for such cases.
Relevant Equations
Java
Right now I am learning coding on Java using IntelliJ IDEA. I have coded before on C++ and then Python, so I understand a little on Java. I'm not really sure how exactly I would change code so that it would read input from the terminal? Would I have to use a Scanner for this? I can post the actual code I have, although there are four more parts to the code but I assume this is the window where we change the code:

Java:
import java.util.ArrayList;

public class Registrar {
    ArrayList<Student> students;
    ArrayList<Course> courses;

    public Registrar() {
        students = new ArrayList<>();
        courses = new ArrayList<>();
    }

    /* Hardcoded example. Instead, you need to implement new functions that read
    the input from the terminal and print the corresponding outputs. */
    public void runExampleCommands() {
        Course course1 = new Course("CS2XYZ");
        Course course2 = new Course("CS1ABC");

        Student student1 = new Student("EMMA", 23345);
        Student student2 = new Student("DAVID", 123345);

        course1.addSection(new Section("C01", 10));
        course2.addSection(new Section("C02", 1));

        boolean enrollmentResult = course1.enrollStudent(student1, "C01");
        TerminalPrinter.printEnrollmentResult(enrollmentResult, student1.getName(),
                course1.getUniqueName(), "C01");

        enrollmentResult = course1.enrollStudent(student2, "C01");
        TerminalPrinter.printEnrollmentResult(enrollmentResult, student2.getName(),
                course1.getUniqueName(), "C01");

        enrollmentResult = course2.enrollStudent(student1, "C02");
        TerminalPrinter.printEnrollmentResult(enrollmentResult, student1.getName(),
                course2.getUniqueName(), "C02");

        enrollmentResult = course2.enrollStudent(student2, "C02");
        TerminalPrinter.printEnrollmentResult(enrollmentResult, student2.getName(),
                course2.getUniqueName(), "C02");

        enrollmentResult = course2.unenrollStudent(student1, "C02");
        TerminalPrinter.printUnenrollmentResult(enrollmentResult, student1.getName(),
                course2.getUniqueName(), "C02");

        enrollmentResult = course2.unenrollStudent(student2, "C02");
        TerminalPrinter.printUnenrollmentResult(enrollmentResult, student2.getName(),
                course2.getUniqueName(), "C02");

        enrollmentResult = course2.enrollStudent(student2, "C02");
        TerminalPrinter.printEnrollmentResult(enrollmentResult, student2.getName(),
                course2.getUniqueName(), "C02");
    }
}

Any help would be appreciated, thank you.
 
Physics news on Phys.org
ver_mathstats said:
I'm not really sure how exactly I would change code so that it would read input from the terminal? Would I have to use a Scanner for this?
I'm not an expert on Java, and haven't written an Java code for about 25 years, but I'd say yes, you need the Scanner class. Take a look at these docs from Oracle on the Scanner class - https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
There are several examples in which an input string is parsed into its constituent parts.
 
  • Like
Likes Jody

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
11K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K