-1

I added in the System.err.flush(); and System.out.flush(); however the problem still exists. It appears as if these printf statements are being output at a random unorthodox order.

When the (absoluteFinalGrade % 1) ranges from 0.45 to 0.49, I would like the output to be printed in red. This is because since I am given hundreds of lines of output, I would like the important ones to stand out. However, system.err.printf seems to be glitching my output in an unorthodox way. Is it possible to use a different method to make my text standout?

import java.util.Scanner;

public class FinalChemistryGradeCalculator {
    public static void main (String [] args) {

            Scanner input = new Scanner (System.in);
            Scanner input2 = new Scanner (System.in);

            double termGrade;
            double examinationGrade;
            double examinationMarkScore;
            double examinationMarkTotal;
            double lowestTestGrade;
            double lowestTestMarkScore;
            double lowestTestMarkTotal;
            double initialFinalGrade;
            double absoluteFinalGrade;

            System.out.printf("Input the term grade.%n");
            termGrade = input.nextDouble();
            System.out.printf("%nThe term grade is %.2f.%n", termGrade);

        System.out.printf("%nInput the lowest test mark score.");
        lowestTestMarkScore = input.nextDouble();
        System.out.printf("%nInput the lowest test mark total.");
        lowestTestMarkTotal = input.nextDouble();
        lowestTestGrade = (lowestTestMarkScore/lowestTestMarkTotal) * 100;
        System.out.printf("%nThe lowest test grade is %.2f.%n", lowestTestGrade);

        System.out.printf("%nInput the examination mark total.");   
        examinationMarkTotal = input.nextDouble();

        for (examinationMarkScore = 0; examinationMarkScore <= examinationMarkTotal; examinationMarkScore += 0.5) {
            examinationGrade = (examinationMarkScore/examinationMarkTotal) * 100;
            initialFinalGrade = (termGrade * 0.7) + (examinationGrade * 0.3);
            if (lowestTestGrade < examinationGrade) {
                absoluteFinalGrade = initialFinalGrade - (lowestTestGrade * 0.05) + (examinationGrade * 0.05);
            }
            else {
                absoluteFinalGrade = initialFinalGrade;
            }
            if (absoluteFinalGrade % 1 >= 0.45 && absoluteFinalGrade % 1 <= 0.49) {
                System.err.printf("%n%n--------------------------------------------------------------------------------------------------------------");
                System.err.printf("%nTerm Grade: %.2f ||||| Examination Grade: %.2f (%.1f/%.1f) ||||| Lowest Test Grade: %.2f (%.1f/%.1f)", termGrade, examinationGrade, examinationMarkScore, examinationMarkTotal, lowestTestGrade, lowestTestMarkScore, lowestTestMarkTotal);
                System.err.printf("%nInitial Final Grade: %.2f ||||| Absolute Final Grade: %.2f", initialFinalGrade, absoluteFinalGrade);
                System.err.printf("%n--------------------------------------------------------------------------------------------------------------");
                System.err.flush();
            }
            else {
                System.out.printf("%n%n--------------------------------------------------------------------------------------------------------------");
                System.out.printf("%nTerm Grade: %.2f ||||| Examination Grade: %.2f (%.1f/%.1f) ||||| Lowest Test Grade: %.2f (%.1f/%.1f)", termGrade, examinationGrade, examinationMarkScore, examinationMarkTotal, lowestTestGrade, lowestTestMarkScore, lowestTestMarkTotal);
                System.out.printf("%nInitial Final Grade: %.2f ||||| Absolute Final Grade: %.2f", initialFinalGrade, absoluteFinalGrade);
                System.out.printf("%n--------------------------------------------------------------------------------------------------------------");
                System.out.flush();
            }
        }           
    }
}
KittMedia
  • 7,368
  • 13
  • 34
  • 38
  • Please explain _glitching my output in an unorthodox way_. – Sotirios Delimanolis Feb 02 '19 at 01:58
  • Basically, it is printing out the System.err and the System.out in a messed up order on rare occasions. – Joseph Lofaro Feb 02 '19 at 01:59
  • 1
    Edit your question body with your concern about the duplicate, not the title. And always be clear, "glitching" can mean a million things. Also, demonstrate that the `flush` solution doesn't work with a [mcve]. – Sotirios Delimanolis Feb 02 '19 at 02:42
  • *"Okay, this question is not a duplicate because the solution that was offered on the other questions only solves println, not printf."* - The (current) linked dups all address your problem. The "println" versus "printf" distinction is not relevant. – Stephen C Feb 02 '19 at 02:47
  • Okay, well I offered a complete and verifiable example in the edit I just made to the question. I cannot make a minimal example because everything is essential to run the program. If you do not believe me when I say this is verifiable, you can use Eclipse yourself to determine the problem still exists. – Joseph Lofaro Feb 02 '19 at 02:51
  • I think my problem is that I am using the flush incorrectly because it was barely specified in all of the previously answered threads. I am very new to Java, if someone can possibly explain to me how to fix these errors, that would be much appreciated. – Joseph Lofaro Feb 02 '19 at 02:52
  • You are aware that `%` is the modulus division operator and `absoluteFinalGrade % 1` will always return `0`? – Powerlord Feb 02 '19 at 02:53
  • Just a heads up, if someone is actually willing to test themselves on Eclipse to see if this problem still exists, let me know because I would like you to input the same values I am. 94.64, 29.5, 44, 115. – Joseph Lofaro Feb 02 '19 at 02:54
  • Powerlord, it would return 0 if my for loop was using increments of += 1, however it uses increments of += 0.5. Also, I am using more decimal numbers in my input. – Joseph Lofaro Feb 02 '19 at 02:55

1 Answers1

-3

You can use jansi:

System.out.println( ansi().eraseScreen().render("@|red Hello|@ @|green
 World|@") );

This example is quoted from Jansi Github README.

Note that this won't probably work from Eclipse or any IDE wrapper.

NoDataFound
  • 11,381
  • 33
  • 59