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);
}
}
Related
This question already has answers here:
Does Java have something like C#'s ref and out keywords?
(7 answers)
How to use an output parameter in Java? [duplicate]
(7 answers)
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
import java.util.*;
public class Main {
private static int call(int n) {
n = n - 1;
return n;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = 5;
call(n);
System.out.println(n);//output is still 5;
}
}
how to change the value of n using call method?
I did using void on call method like we use for integer array but that is also not working.
This question already has answers here:
Giving name to a loop
(6 answers)
"loop:" in Java code. What is this, and why does it compile?
(12 answers)
Can java labels be used appropriately outside of for loops?
(2 answers)
Closed 3 years ago.
I find a strange statement while reading the openjdk resources.
enter image description here
what does the "found:{}" means?
found:{} is a label in Java mainly used for goto statement but since Java doesn't use goto it can be used for break and continue statement. Here is an example of using a label in Java.
loop:
for (int i = 0; i < 10; i++) {
for(int j=0; j<10; j++)
{
if(j==5)
{
break loop;
}
System.out.println(j);
}
}
When j reach 5 it will trigger break to outside loop label.
This question already has answers here:
Possible lossy conversion from double to int
(3 answers)
Closed 6 years ago.
I keep getting an error but Im not understanding why. The error is about not being able to convert from double to int but I have everything declared in double so I dont understand why im getting this error.
class ConeArray{
public static void main(String[] args) {
double[] coneArray;
for(double i = 0; i < coneArray.length; i++) {
coneArray = Math.PI * Math.pow((i*2),2) * (1/3 * (4 * i));
System.out.println("Volume of cone: " + coneArray[i]);
}
}
}
You have declared double i in your for loop. Array indexes must be ints.
Also, I don't see you actually instantiating the array in the code sample you provided. The current implementation will throw a NPE if you try to access it.
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]+" ";
}
}
}
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
My Variable c is always zero. I dont understand why its not updating. can anyone please explain why this is happening. what should i do to avoid this
public static int linearSearch(Exam[] marks, String name) {
int c =0;
if( marks==null)
{
return -1;
}
else{
for(int i=0;i<marks.length;i++)
{
//System.out.println(a[i]);
if(performances[i].getName()==name)
{
c= i;
}
}
}
return c;
//to be completed
}
Modify this line as below
performances[i].getName().equalsIgnoreCase(name)
if you want to Ignore upper or lower case
else use the below
performances[i].getName().equals(name)
to check the content of the name instead of references.