Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

System.out.print() not working but System.out.println() working

When I try to print on the console using System.out.print() the content in the parameter doesn’t get printed. But if I try System.out.println() it works.

I’m using TMCBeans as the IDE.

Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import java.nio.file.Paths;
import java.util.Scanner;
import java.util.ArrayList;

public class TextUI {

    private Scanner tscanner;
    private RecipeManager rm;

    public TextUI(RecipeManager rm, Scanner tscanner) {
        this.rm = rm;
        this.tscanner = tscanner;
    }

    public void start() {
        System.out.print("File to read: ");
        String file = tscanner.nextLine();
        try (Scanner fscanner = new Scanner(Paths.get(file))) {
            while (fscanner.hasNextLine()) {
                String name = fscanner.nextLine();
                int time = Integer.valueOf(fscanner.nextLine());
                ArrayList<String> ingredients = new ArrayList<>();
                String ing = fscanner.nextLine();
                while (!(ing.isEmpty())) {
                    ingredients.add(ing);
                    ing = fscanner.nextLine();
                }
                rm.add(new Recipe(name, time, ingredients));
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        while (true) {
            System.out.println("Commands:\n"
                    + "list - lists the recipes\n"
                    + "stop - stops the program\n"
                    + "\n"
                    + "Enter command: ");

            String command = tscanner.nextLine();
            if (command.equals("list")) {
                rm.listRecipes();
            } else if (command.equals("stop")) {
                return;
            }
        }

    }
}

To run this, you create an instance of this class and run the start() method passing an instance of another class(RecipeManager which is irrelevant to the question I reckon) and a Scanner instance with parameters as System.in

With System.out.print("File to read: "):
With just System.out.print

With System.out.println("File to read: "):
With System.out.println

System.out.print() not displaying anything whereas System.out.println() does.

>Solution :

This is your IDE’s fault, presumably also because it was written in java :^).

When you read a process stream (like the IDE is doing, the output stream of the running program), java caches the current line until a line separator is hit. In your case, because you aren’t printing a line sep, the IDE won’t show the line until one appears. Try print("something\n"), and it will most likely show up.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading