How to overwrite and print the values in console in java [duplicate] - java

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.

Related

what code can i use to clear the screen(java)? [duplicate]

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);
}
}

Printing to file rather than terminal [duplicate]

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!

fun(agr++) vs fun(arg+1) in java [duplicate]

This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 6 years ago.
I was trying to reverse String using recursion as follows.
But when I am calling reverse(arr,start++,end--) it is giving stackoverflow error.
I tried using reverse(arr,start+1,end-1) then it is working fine.
public class Reverse {
public static void main(String[] args) {
char inp[] = "TestString".toCharArray();
int n = inp.length;
reverse(inp, 0,n-1);
System.out.println(String.valueOf(inp));
}
public static void reverse(char[] arr, int start, int end){
if(start>=end)
return ;
char tmp;
tmp = arr[start];
arr[start] = arr[end];
arr[end] = tmp;
reverse(arr,start+1,end-1);//error line
}
What is the problem with reverse(arr,start++,end--)?
I want to know why value of start++ and end-- will not get passed to function.
Have a look at this SO question
SO post-increment-and-pre-increment-concept
Post
reverse(arr,start++,end--) when you do this the incremented/decremented value will not be passed to recursive method, means you are calling the method with original value of start and end result in SO error
Pre
When you do reverse(arr,start+1,end-1) or reverse(arr,++start,--end) incremented and decremented value of start and end will be passed to recursive method.
While debugging in Eclipse IDE check the values of start and end in variables panel if not using IDE write start and end values to console in reverse method

Finding duplicate elements in an array without using nested loops [duplicate]

This question already has answers here:
Find duplicate element in array in time O(n)
(24 answers)
Closed 7 years ago.
I recently had an interview which consisted of following problem. Please help with possible solutions.
Write a method in Java to find duplicate elements in an integer array without using nested loops ( for/ while / do while, etc ) and without using library functions or standard API's.
Hey the below solution has complexity O(n) and works fine. Check if it helps.
public class Main {
public static void main(String[] args) {
int a[] = new int[]{10,3,5,10,5,4,6};
String distinctElement="";
String repetitiveTerms="";
for(int i=0;i<a.length;i++){
if(i==0){
distinctElement+=a[i]+" ";
}
else if(distinctElement.contains(""+a[i])){
repetitiveTerms+=a[i]+" ";
}
else{
distinctElement+=a[i]+" ";
}
}
}
}

java GC and console [duplicate]

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.

Categories

Resources