This question already has an answer here:
Will Java's System.out.print() buffer forever until println()?
(1 answer)
Closed 9 years ago.
Just to understand, what is happening I done the below code:
public class Loopp {
public static void main(String[] args) {
int i=1;
while(true) {
Employee e = new Employee("MyName", i);
i++;
System.out.print(i + " ");
}
}
}
But on console I do not see any output, but when I run this in debug mode, then it prints 2 3 4 ..
I understand that gc is activated again and again to collected the garbage objects, but does that clear console also :|
Edit:
As per the answer , it worked for me, and I learned a new thing today
System.out.println(i + " ");
System.out.flush();
You are using print without flush. Only println has autoflushing semantics.
Add System.out.flush() to your code.
Related
This question already has answers here:
How does the Java 'for each' loop work?
(29 answers)
Closed 2 years ago.
could someone please explain how the enhanced for loop in the code below would look if it was represented by a standard for loop? ex for(int loop = 0; loop < ____; loop++) etc. I am trying to understand what the enhanced for loop does but want to see how it is represented in a standard for loop.
public static void main(String[] args) {
// get the list of each winning team by year
ArrayList<String> allWinners = readAndPopulateWinnersList();
int startYear = 1956;
for(String teams : allWinners) {
System.out.println(startYear++ + " : " + teams);
}
}
If you want to change your for(String teams: allWinners) to standard loop here it is:
for (int i = 0; i < allWinners.size(); i++) {
System.out.println(startYear++ + " : " + allWinners.get(i));
}
In for each you have simply form without .get(i) etc. but you don't have index either.
So If you want to print allWinners item with its index then standard for loop will be better for you. You can also use Java Streams APi: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
so it will be allWinners.forEach(element -> System.out.println(startYear++ + " : " + element));
This question already has answers here:
commands in java to clear the screen
(11 answers)
Closed 5 years ago.
I'm writing a text-based unit converter and I want to be able to run a clear command so that the window that the program I want to know how I can do it.
I think you can System.exec("clear") but that depends on what operating system the program is running on.
This is my choice for clear:
public static void clearConsole() {
String value = "\n\r";
for (int i = 0; i < 5; ++i) {
value = value + value;
System.out.printf(value);
}
}
This question already has answers here:
How do I create a file and write to it?
(35 answers)
Closed 5 years ago.
I've made a program for determining the time of a murder. The code I have right now prints exactly what it should to the terminal. I want it printed to a file. However, I'm not supposed to use System.setOut() for this assignment. I'm supposed to print to a file instead of to the terminal.
I know how to write a simple String into a file, the problem here is I already have methods for printing my results to the terminal, and I'm not sure how I "convert" those methods into printing into a file instead.
This is my two printing methods and main method:
Printing 2d array method:
public static void printArray2d(String[][] array2d){
for(int i = 0; i < array2d.length; i++){
for(int j = 0; j < array2d[i].length; j++){
System.out.print(array2d[i][j]);
}
System.out.print("\n");
}
}
Printing full report method:
public static void printReport(String[][] array2d, double arrayMin, double arrayMax){
System.out.println("Time since death probability distribution");
double hours = (arrayMax-arrayMin)/(array2d.length-1);
System.out.printf("Each line corresponds to approximately %.2f hours\n", hours);
System.out.printf("%.2f hours\n", arrayMin);
printArray2d(array2d);
System.out.printf("%.2f hours\n", arrayMax);
}
Main method:
public static void main(String args[]) {
double[] array = cooldownSamples(27, 1000);
double[] counts = countsFromArray(array, 20);
String[][] array2d = array2dFromCounts(counts);
printReport(array2d, minFromArray(array), maxFromArray(array));
}
I can post the entire code if needed.
I am aware that there are similar questions asked earlier, but none of them gave me the help I needed. I also do not have enough reputation to ask follow-up questions to any of the answers given on those threads, so I was forced to ask a new question.
Thanks in advance for any help given!
Additional information:
Even though I said I'm not supposed to use System.setOut(), I have tried using the method and answers given in this thread, without any luck. If the best and most efficient way of doing this is via System.setOut(), I do appreciate answers that make me understand how I can implement this and make it work in my code, even though I'm looking for an alternative method.
It is really simple: right now, you are using a (static) object System.out to do all printing. This object has methods to print, println, and so on.
Instead of using System.out, you create an instance of say PrintWriter and call method on that object, like
PrintWriter writer = new PrintWriter("whatever.txt");
writer.println("whatever");
writer.close();
That is all there is to this. Or even simpler, you could instantiate a PrintStream object. You could then do things such as:
PrintStream out = System.out // or new PrintStream("filename");
doStuff(out);
... with:
public void doStuff(PrintStream out) {
out.println...
And now you have one central place where you decided if you want to print to System.out - or somewhere else!
This question already has answers here:
How can I return to the start of a line in a console?
(3 answers)
Closed 7 years ago.
Here is one while loop that will iterate 11 times.
public static void main(String[] args) {
int count=0;
while(count<=10){
System.out.print(count);
count++;
}
}
& the output will be definitely : 012345678910
But I want to display the same output in such a way that every iteration will overwrite the previous value while printing the value on console.
Here's the restriction is : We can not use file.
clearing the console on every iteration can be one of the ways, is there anything left we can do?
You can use \r which returns to the start of a line: System.out.print("\r" + count); should work.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following code:
class Hi
{
public static void main (String [] args)
{
int a = 1;
for (int i = 0; i < 50; i++) {
a = a + i;
System.out.println ("a before sleep is " + a);
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
}
System.out.println ("a after sleep is " + a);
}
}
}
I open two console windows and do java Hi in first one. Then wait for about 10 seconds seconds and do the same in the second window. Both outputs are the same:
a before sleep is 1
a after sleep is 1
a before sleep is 2
a after sleep is 2
a before sleep is 4
a after sleep is 4
a before sleep is 7
a after sleep is 7
a before sleep is 11
a after sleep is 11
with no interleaving. So what's the stir about concurrency issues if I even didn't bother to use synchronized statement? Is it because the code run from different console windows is executed on different processor cores? If so, I've carried out this experiment about 20 times and the results are still the same.
I open two console windows and do java Hi in first one. Then wait for about 10 seconds seconds and do the same in the second window.
You're starting two entirely separate process. That's not using multithreading at all - each process will have its own variables, its own output, everything.
To see multithreading, you need to create threads in a single process. For example:
import java.util.Random;
class Counter implements Runnable {
private int a;
public void run() {
Random random = new Random();
for (int i = 0; i < 10; i++) {
String name = Thread.currentThread().getName();
a++;
System.out.println(name + " Before sleep, a = " + a);
try {
// Add a little more uncertainty...
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
// Ignored
}
System.out.println(name + " After sleep, a = " + a);
}
}
}
public class ThreadingTest {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
Thread t1 = new Thread(counter);
Thread t2 = new Thread(counter);
t1.start();
t2.start();
}
}
You have two different JVM's so two main threads running on two different JVM's so concurrency dont apply.
Yes if you have two threads within same JVM then concurrency applies.
These are two separate processes on your computer. They do not share data or anything else. They are no more related then, for example, your web browser and your wordprocessor. They each have their own separate "a" variable, each completely unrelated to the other.
Concurrency issues arise when multiple threads in the same process space try to access the same variable. That does not apply here.
As here you are running two different processes, no matter how many times you run this code you will get same result. As both of these programs will be running within two separate processes.
To run these programs in multi threaded way you have to implement two threads and start them.