Programming with Java -- Reading From the Terminal

In summary: For example, the next code fragment reads a number from the terminal and prints the corresponding number on the console:import java.util.Scanner;public class Registrar {public void runExampleCommands() {Scanner input = new Scanner(System.in);System.out.println("Number: " + input.nextInt());}}
  • #1
ver_mathstats
260
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
  • #2
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 Joshy

What is the purpose of reading from the terminal in Java?

The purpose of reading from the terminal in Java is to allow users to interact with the program by providing input through the command line. This input can then be used by the program to perform specific tasks or calculations.

How do I read input from the terminal in Java?

To read input from the terminal in Java, you can use the Scanner class. First, you need to create a new Scanner object and pass in the System.in object as a parameter. Then, you can use the next() method to read a single word or the nextLine() method to read a full line of input.

Can I read different types of input from the terminal in Java?

Yes, the Scanner class in Java allows you to read different types of input from the terminal, including strings, integers, floats, and more. You can use the appropriate next methods, such as nextInt() or nextDouble(), to read specific types of input.

What happens if the user enters invalid input?

If the user enters invalid input, such as a string when the program is expecting an integer, an InputMismatchException will be thrown. It is important to handle these exceptions in your code to prevent the program from crashing.

Can I read input from the terminal in Java without using the Scanner class?

Yes, there are other ways to read input from the terminal in Java, such as using the BufferedReader class or the Console class. However, the Scanner class is the most commonly used method for reading input from the terminal in Java.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top