0

Looking at my code, how can I have the text "Enter 3 collections of course codes one collection per line" and the "Size" and the "Sorted" have a different text style and underlined so it's easier to read? Thank you in advance.

public static void main(String[] args) {
    LinkedList<String> ListofCourses = new LinkedList<>();
    Scanner course = new Scanner(System.in);
    String line;
    String[] codes;
    System.out.println("Ron's Copy");
    System.out.println("\nEnter 3 collections of course codes one collection per line");

    /**The 'for' statement is used here to output the user input of courses 
     and loop back to have the user enter another set of courses until the user
     input has been completed 3 times successfully */

    for (int i = 0; i < 3; i++) {
        line = course.nextLine();
        codes = line.split(" ");
        //This statement adds input from sets
        ListofCourses.addAll(Arrays.asList(codes));

        //Sorting LinkedList with Collections.sort() method
        Collections.sort(ListofCourses);

        /*
         * The system.out.print statement print out the courses in a sorted
         * method and then loops back again to have the user input
         */

        System.out.print("\nSize: " + ListofCourses.size() + " Sorted: ");

        //The statements below will print sorted courses that were inputted by the user

        for (int r = 0; r < ListofCourses.size(); r++) {
            System.out.print(ListofCourses.get(r) + " ");
        }
        System.out.println();
        //clear the list so that next iteration gets a fresh empty list
        ListofCourses.clear();
    }
}
SamHoque
  • 2,978
  • 2
  • 13
  • 43
Husker_RC
  • 1
  • 5
  • Razro what did you add to the code? – Husker_RC Dec 10 '18 at 22:22
  • I think he only tried to just fix the formatting. Some of your code is not inside the code form. – chickity china chinese chicken Dec 10 '18 at 22:25
  • I didn't get what are you trying to achieve? – Ulad Dec 10 '18 at 22:29
  • So when you run my program the first thing you see in the output is: "Enter 3 collections of course codes one collection per line" statement. How do I change the text in this sentence to a different text style like "Arial' and also underline this sentence. The same goes for the Text that is called "Size" and "Sorted". I hope this makes sense. – Husker_RC Dec 10 '18 at 22:32
  • So what is the issue here? – SamHoque Dec 10 '18 at 22:36
  • Samz - So when you run my program the first thing you see in the output is: "Enter 3 collections of course codes one collection per line" statement. How do I change the text in this sentence to a different text style like "Arial' and also underline this sentence. The same goes for the Text that is called "Size" and "Sorted". I hope this makes sense. – Husker_RC Dec 10 '18 at 22:39
  • I need to have certain output text underlined and the font weight changed – Husker_RC Dec 10 '18 at 22:40
  • 1
    You can't, You need to make a custom GUI for that with a JTextPane. – SamHoque Dec 10 '18 at 22:46
  • 1
    Thank you Samz, thats what I was trying not to do, so I will just leave it as is. Thank you everyone for your input. – Husker_RC Dec 10 '18 at 22:55
  • 1
    Comment: Your Question title is not helpful. The title should actually tell the reader what the Question is about. In this case, the question you are asking is nothing to do with either array lists or sorting. It is actually about modifying the style / font characteristics of console output. – Stephen C Dec 10 '18 at 23:58

2 Answers2

0

If you are asking how to underline console text, the short answer is you can't but this question "How to print underlined string in console output in java" might help.

Chris Oler
  • 56
  • 3
0

What you are running into here is a limitation of console output.

Console output is (typically) rendered by an application that emulates a classical (hardware) terminal from the 1970s / 1980s. These devices worked by interpreting bytes (e.g. escape sequences) in the output stream to do things such as:

  • move the cursor on the (24x80) screen
  • block erase characters
  • switch between "black on white" and "white on black"
  • change character or background colors (advanced!)
  • character underline or strikeout (advanced!)
  • alternate font switching (advanced!)
  • and so on.

The problem is that different hardware terminals had different capabilities, and different (non-standard) ways of interpreting the escape sequences. Eventually, there was some industry-wide convergence on the capabilities of the DEC VT100 terminal and some standardization. The result was the ANSI / VT100 Terminal Control Escape Sequences:

With the advent of the IBM PC (and similar), hardware terminals became obsolete, and were replaced by terminal / console emulator software provided by your OS. However, some OSes emulator software continues to implement the "ANSI / VT100" system.

How would this help you?

Well ... if your console program / emulator supports VT100 sequences, then you should be able to do things like underlining by writing the appropriate sequences to your standard output. But if you do this and the user of your application runs it on a console that doesn't support VT100, then the result will be a mess.

This kind of thing is not supported by the standard Java SE libraries, but there are some 3rd-party alternatives; see What's a good Java, curses-like, library for terminal applications?


In summary, it is possible to do this kind of thing, but it is a lot of work.

In your case it would be a bad idea to try this because you don't have any say about the environment in which your application will be run. (By your instructors, when marking!)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216