Programming with Java -- Reading From the Terminal

Click For Summary
SUMMARY

The discussion focuses on reading input from the terminal in Java using the Scanner class within the IntelliJ IDEA environment. The user, who has prior experience in C++ and Python, seeks guidance on modifying their Java code to accept terminal input. It is confirmed that the Scanner class is essential for this functionality, and users are directed to Oracle's documentation for further examples and implementation details.

PREREQUISITES
  • Familiarity with Java programming language
  • Understanding of the IntelliJ IDEA development environment
  • Basic knowledge of the Scanner class in Java
  • Experience with object-oriented programming concepts
NEXT STEPS
  • Explore the Java Scanner class documentation on Oracle's website
  • Implement terminal input handling in Java using Scanner
  • Learn about exception handling in Java for user input validation
  • Investigate advanced input parsing techniques in Java
USEFUL FOR

Java developers, students learning programming, and anyone looking to enhance their skills in terminal input handling within Java applications.

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   Reactions: Jody

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · 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