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:
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 System.out.println("File to read: "):

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.