-2

I'm currently working on a project for a Java class where we have to print out our schedule. I've got my code to look the way I want the final code to be. We can only use one System.out.print() This is so we can practice using escape sequences. However, my teacher said that I need to condense my line of code and find a way to not make it dragged on. Any help would be appreciated.

Here is my current code:

class Main {
    public static void main(String[] args) {
        System.out.println("Period\t\t\t\tClass\t\t\t\t\tComment\n1\t\t\t\t\tPrecalculus\t\t\t\tBoring\n2\t\t\t\t\tJava\t\t\t\t\tCool\n3\t\t\t\t\tHonors Chinese\t\t\tInteresting\n4\t\t\t\t\tGym\t\t\t\t\t\tIrrelevant\n5\t\t\t\t\tEnglish\t\t\t\t\tEasy\n6\t\t\t\t\tAP World\t\t\t\tHard\n7\t\t\t\t\tAP Physics\t\t\t\tAlso Hard\n8\t\t\t\t\tVoices\t\t\t\t\tExciting");
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
kcash
  • 1

2 Answers2

1
  1. It is better to represent the data as an object(of a class).
  2. The object can be printed as per the need

The following code will

  1. Define a new class Schedule to represent the record
  2. Schedule can override toString method to print its internal state
  3. The toString can use a Formatter if needed
  4. Create multiple Schedule objects and print them using the toString
  5. Print the column before printing the actual objects (this is where my implementation is slightly offtrack)
import java.util.ArrayList;
import java.util.List;

public class ScheduleFormatter {

    static class Schedule {
        public static final String FORMAT = "%-15s %-20s %s";
        private final int period;
        private final String className;
        private final String comment;

        public Schedule(int period, String className, String comment) {
            this.period = period;
            this.className = className;
            this.comment = comment;
        }

        @Override
        public String toString() {
            return String.format(FORMAT, this.period, this.className, this.comment);
        }
    }

    public static void main(String[] args) {
        List<Schedule> schedules = new ArrayList<>();
        schedules.add(new Schedule(1, "Precalculus", "Boring"));
        schedules.add(new Schedule(2, "Java", "Cool"));
        System.out.println(String.format(Schedule.FORMAT, "Period", "Class", "Comment"));
        schedules.forEach(System.out::println);
    }
}

Even the List can be contained in another object and that container object can override the toString.

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

public class ScheduleFormatter {

    static class Schedule {
        public static final String FORMAT = "%-15s %-20s %s\n";
        private final int period;
        private final String className;
        private final String comment;

        public Schedule(int period, String className, String comment) {
            this.period = period;
            this.className = className;
            this.comment = comment;
        }

        @Override
        public String toString() {
            return String.format(FORMAT, this.period, this.className, this.comment);
        }
    }

    static class Schedules {
        private String[] headers;
        private List<Schedule> schedules;

        Schedules(String[] headers) {
            this.headers = headers;
            this.schedules = new ArrayList<>();
        }

        public void add(Schedule schedule) {
            this.schedules.add(schedule);
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append(String.format(Schedule.FORMAT, headers));
            schedules.forEach(builder::append);
            return builder.toString();
        }
    }

    public static void main(String[] args) {
        Schedules schedules = new Schedules(new String[] {"Period", "Class", "Comment"});
        schedules.add(new Schedule(1, "Precalculus", "Boring"));
        schedules.add(new Schedule(2, "Java", "Cool"));
        System.out.println(schedules);
    }
}
Thiyanesh
  • 2,360
  • 1
  • 4
  • 11
0

Text blocks

In Java 15 and later, you can use text blocks to make this code somewhat more readable.

To quote that JEP 378:

The escape sequences \b (backspace), \t (tab) and \s (space) are not interpreted by the algorithm; escape processing happens later.

System.out.println(
        """
        Period\t\t\t\tClass\t\t\t\t\tComment
        1\t\t\t\t\tPrecalculus\t\t\t\tBoring
        2\t\t\t\t\tJava\t\t\t\t\tCool
        3\t\t\t\t\tHonors Chinese\t\t\tInteresting
        4\t\t\t\t\tGym\t\t\t\t\t\tIrrelevant
        5\t\t\t\t\tEnglish\t\t\t\t\tEasy
        6\t\t\t\t\tAP World\t\t\t\tHard
        7\t\t\t\t\tAP Physics\t\t\t\tAlso Hard
        8\t\t\t\t\tVoices\t\t\t\t\tExciting
        """
);

When run.

Period              Class                   Comment
1                   Precalculus             Boring
2                   Java                    Cool
3                   Honors Chinese          Interesting
4                   Gym                     Irrelevant
5                   English                 Easy
6                   AP World                Hard
7                   AP Physics              Also Hard
8                   Voices                  Exciting
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154