Why does this Java code behave as it does? [duplicate] - java

This question already has answers here:
Static Block in Java [duplicate]
(7 answers)
Closed 5 years ago.
Why does the following code print "Bo-Bo Go-Go", instead of "Bo-Bo Hello, World! Go-Go?
public class Test {
static {
System.out.print("Bo-Bo ");
}
public static void main(String[] args) {
System.out.print("Hello, World! ");
}
static {
System.out.println("Go-Go ");
System.exit(0);
}
}

Because static initialization blocks run before the entry-point (both of them), and the second one exits thus main is never entered.

Related

cant find my main error, debugging at java [duplicate]

This question already has answers here:
Writing a function inside the main method - Java
(7 answers)
What is a classpath and how do I set it?
(10 answers)
Closed 2 years ago.
so I'm running a program in java and I can't really find the main error
this is my code:
public class Main
{
public static void main(String[] args) {
double myCheck = 50.00;
double yourCheck = 19.95;
double fiinalRATE = 0.15;
System.out.println("Tips are");
calcTip(myCheck);
calcTip(yourCheck);
public void calcTip(double bill);
{
tip = bill * fiinalRATE;
System.out.println("The tip should be at least " + tip);
}
}
and this is the error that I'm getting I think its the header but I don't really know what to put I'm kinda new at java though
You can't declare function inside function. You have to pull function out from main() to the Main.class
You cannot declare a method inside another method. So the compiler gets crazy :)
Just move you calcTip() function outside main() function (after closing curly bracket of main() or before declaration of main()).
public class Main
{
public static void main(String[] args) {
double myCheck = 50.00;
double yourCheck = 19.95;
double fiinalRATE = 0.15;
System.out.println("Tips are");
calcTip(myCheck);
calcTip(yourCheck);
}
public static void calcTip(double bill) {
// fiinalRate must be declared as parameter of calcTip()
// or as static field in Main class,
// otherwise the code doesn't compile.
double tip = bill * fiinalRATE;
System.out.println("The tip should be at least " + tip);
}
}

I'm a beginner here in java oop. my code is showing this error. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
(2 answers)
Closed 4 years ago.
THIS IS THE CODE
here I want to take value from cmd and use that to get the output. Please let me know if there is any other problem. Thanks
class Basic
{
int b;
public void gd(int c){
b=c;
}
}
class HRA extends Basic
{
double hra=(0.25*b);
}
class DA extends HRA
{
double da=(0.75*b);
}
class PF extends DA
{
double pf=(0.12*b);
}
class Netsalary extends PF
{
double ns=b+hra+da+pf;
void display()
{
System.out.println("The net salary = "+ns);
}
}
class Netsalmain
{
public static void main(String arb[])
{
int a= Integer.parseInt(arb[0]);
Netsalary ob=new Netsalary();
ob.gd(a);
ob.display();
}
}
error is showing like this
E:>java Netsalmain Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 0
at Netsalmain.main(iop.java:54)
You are not passing the command line argument and trying to access it that’s is why you are getting array index out of bound.
If you are using IDE then go into run configuration and give the parameter.
If you are trying to run via cmd then give the command line argument while running.

how to check if the variable is null? [duplicate]

This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 5 years ago.
I know this doesnt work but how can I know if the user added even one char?
public class Program
import java.util.Scanner;
{
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String b = a.nextLine();
//I know this doesnt work but how can I know if the user added even one char?
if (b!=null){
System.out.println(b);
}
}
}
you can use .equals("") or .isEmpty()
check check if the variable is null
You can do this
if (!b.isEmpty()) {
System.out.println(b);
}

Exchange of informations between classes in Java [duplicate]

This question already has answers here:
Cyclic reference in java [closed]
(3 answers)
Java classes reference each other
(3 answers)
Closed 5 years ago.
I've got two problems:
First: I'm getting StackOverflowError and I don't know how to fix it
Second: I'm using similar syntax in other projects and there was no problems with StackOverflowError. The second problem is in comments in my code. I want to change value of x in class ChangeValueOfVariable and use it in class DisplayValueOfVariable.
class DisplayValueOfVariable{
ChangeValueOfVariable change = new ChangeValueOfVariable();
public int x = 0;
public void showX(){
System.out.println("Value of x: "+x); //There is still x = 0
}
public void changedX(){
change.changeValue();
System.out.println("Value of x: " +x); // Unfortunately there is still x = 0, but I want here to use changed value
}
}
class ChangeValueOfVariable{
DisplayValueOfVariable disp = new DisplayValueOfVariable();
public void changeValue(){
disp.x++;
System.out.println("Value of x: "+disp.x); // Now x = 1
}
}
class Start{
public static void main(String[] args) {
DisplayValueOfVariable displayValueOfVariable = new DisplayValueOfVariable();
displayValueOfVariable.showX();
displayValueOfVariable.changedX();
}
}
I know, that I can change x in public void changedX(), but if I will have more variables it wouldn't help me in cleaning my code.
Thanks for your help ;)
I know that if I'll make something like this
class DisplayValueOfVariable {
public static void main(String[] args) {
int x = 0;
System.out.println(x);
x++;
System.out.println(x);
}
}
It will be my expected result, ok.
I understand that if I will use only one class(which I attached), my code would compile without errors and code would be simpler.
Unfortunately I have to split my code and save it as more classes.
I've got problem with operations(in other classes) on already existing variables.

Strings from input are never equal? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
import java.util.Scanner;
public class Michal {
public static void main(String[] args) {
reply();
}
public static void reply() {
Scanner input=new Scanner(System.in);
String name=input.nextLine();
if(name=="john"){
System.out.println("bear!");
}else if(name=="mary")
{
System.out.println("lovely lady!");
}else{
System.out.println("I don't know that person.");
}
System.out.println(name);
input.close();
}
}
I consider myself still a beginner to Java ,so please don't be harsh in your answers. I was trying to create a program that returns an answer every time it gets a certain input , however it seems to return "I don't know that person" all the time.
if(name=="john")
is not the right way to compare strings. Use equals() instead:
if(name.equals("john")){
System.out.println("bear!");
} else if(name.equals("mary")){

Categories