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

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

Related

Printing 2 different variables in the same line Java VM [duplicate]

This question already has an answer here:
How to print both bool and double in same line? [duplicate]
(1 answer)
Closed 2 years ago.
I am new to java and i have 2 different Variables one is a double and one is a boolean. I am having difficulty printing them into the same line, this is my code.
class Main {
public static void main(String[] args) {
boolean isTrue;
isTrue = false;
double money;
money = 99999.99;
System.out.println(isTrue, double);
}
}
Thanks a lot if you answer
Many ways to do this and if you visited the javadocs you would see that there is also System.out.print which does not do a line feed, so you could use that as
System.out.print(isTrue);
System.out.println(money);
Or you could use System.out.printf, or convert values into a String first
There are few methods to do this, one is mentioned in the above answer and the other is below, that you can do this with a professional approach by using printf.
public static void main(String[] args) {
boolean isTrue;
isTrue = false;
double money;
money = 99999.99;
System.out.printf("%b%.2f%n", isTrue, money);
}

Eclipse shows an error on every function and I dont know why [duplicate]

This question already has answers here:
Why can't we implement a method inside the main method in Java? [closed]
(5 answers)
Closed 3 years ago.
Hello I´m pretty new to java and eclipse. When im writing a function, eclipse always shows an error (at the line public double makeChange(double itemCost, double dollarsProvided)) { and I dont know why. This is the Code:
package lernen;
public class udacity {
public static void main(String[] args) {
public double makeChange(double itemCost, double dollarsProvided) {
double change = dollarsProvided - itemCost ;
return change;
}
}
}
It says: Multiple markers at this line
- Syntax error, insert ";" to complete
LocalVariableDeclarationStatement
- Syntax error on token "rollDice", AnnotationName expected after
this token
- Syntax error, insert "[ ]" to complete Dimension
- Illegal modifier for parameter rollDice; only final is permitted
Can you help me?
You cannot create a method inside a main method.
public static void main(String[] args) {
double d = makeChange(0,0);
}
public static double makeChange(double itemCost, double dollarsProvided) {
double change = dollarsProvided - itemCost ;
return change;
}

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.

Calling a class in main

Hi I seem to have a problem calling a class in a main. Can somebody point it out?
KilometerTabel.java
package pratikum31d;
public static double mijlToKilometer() {
double mijl;
mijl = 0;
for (int i = 1; i < 11; i++) {
mijl = i;
}
double kilometer = 1.609 * mijl;
System.out.println(kilometer + " kilometer" + " dat is " + mijl + " mijl");
return kilometer;
}
Main.java
package pratikum31d;
public class Main {
public static void main(String[] args) {
kilometer = mijlToKilometer();
}
}
You never defined a variable called mijl in main. What value do you expect to get passed to mijlToKilometer?
===UPDATE ===
Your new code will have the following problems:
mijlToKilometer is still declared to expect an argument, so you won't be able to call it with no arguments. You must remove the double mijl from the definition of mijlToKilometer.
Your for loop doesn't do what you think it does, though I'm having a hard time identifying what it's supposed to do.
You must declare mijlToKilometer as public.
public static double mijlToKilometer(double mijl)
What are the packages for KilometerTabel and the main class? You haven't put any public/private/protected modifier before your static method. so by default, it will have default visibility. Which is visible within the package. make sure that you bave both classes in same package OR put a public keyword before methods.
secondly, can you please post exact exception?

Trouble initializing local/class variables

public class ClassName {
public static void main(String[] args) {
//code: depending on user input runs Methodname1();
}
public static void MethodName1 {
double kgs;
double totalIn;
//code: do/while try/catch etc.
double ImpToMetBmi;
double InchToMtrH;
InchToMtrH = totalIn*2.54/100;
ImpToMetBmi = (kgs/(InchToMtrH*InchToMtrH);
System.out.printf("\nYour BMI is: %.3f\n" ,ImpToMetBmi);
}
}
Really sorry for the long and badly written code. I think all code/layout must be seen to figure out the problem.
Errors I'm getting:
Exception...Uncompilable source code - variable totalIn might not have been initialized
Exception...Uncompilable source code - variable kgs might not have been initialized
This formula worked before I inserted do/while try/catch statements for exception handling.
I have spent hours reading about declaring and initilizing variables, local and class variables. I've tried a few different ways but nothing I try fixes the problem.
I'm confused as to what is causing this and how to fix it. I'd like to figure this out and understand the solution.
Where do I initialize 'totalIn' and 'kgs'? and What to I initialize them as?
These varialbles are populated by values inputted by the user through Scanner if that makes any difference.
Please help!
Here is an example that explains the cause you are getting and why you are getting that -
double test;
if( isTrue){
test = 2.0d;`enter code here`
}
// This will give you a error stating that test might have not initialized
double calculate = test * 5.0;
Reason is clear if condition in if block is true then the test value will be initialized with 2.0 other wise it will be uninitialized.
A quick fix to this might be initializing test to some value (may be 0).
Coming to you point, to initialize those variables you can do following things -
static double kgs;
static double totalIn;
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
kgs= sc.nextDouble;
totalIn = sc.nextDouble();
}
or pass them as method parameter like below -
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
double kgs = sc.nextDouble;
double totalIn = sc.nextDouble();
}
public void yourMethod(double kgs, double totalIn){
// do whatever you want with above passed variables
}
Declaration of MethodName1 method is wrong. You missed argument section. Change it to public static void MethodName1().
public class ClassName {
public static void main(String[] args) {
//code: depending on user input runs Methodname1();
}
public static void MethodName1(double KGS, double TOTAL) {
double kgs = KGS;
double totalIn = TOTAL;
//code: do/while try/catch etc.
double ImperialToMetricBmi;
double InchesToMtrHeight;
InchesToMtrHeight = totalIn*2.54/100;
ImperialToMetricBmi = (kgs/(InchesToMtrHeight*InchesToMtrHeight));
System.out.printf("\nYour BMI is: %.3f\n" ,ImperialToMetricBmi);
}
}
You could basically initialize both kgs and totalIn right where you have declared them but it would be better if the method takes those value as arguments(As of this point both the value never get initialized). Also then you would need to call the static method with both those arguments like
double value1 = 123.1;
double value2 = 24
MethodName1(value1, value2)
Further reading the question I realised that you might be trying to initialize the value inside a conditional statement or a loop .
Understanding in simple terms what happens when the condition does to run the statement is not satisfied ?
The answer is that the value never gets initialized which is the case happening here.

Categories