Cannot find symbol Double.parseDouble(args[0]) [duplicate] - java

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 3 years ago.
Whenever I try to run the following code Java tells me it cannot find symbol.
public class Quadratic {
public static void main(String[] args) {
double b = Double.parseDouble(args[0]);
double c = Double.parseDouble(args[1]);
double discriminant = b * b - 4.0 * c;
double d = Math.sqrt(discriminant);
System.out.println((-b + d) / 2.0);
System.out.println((-b - d) / 2.0);
}
}
I think it worked last week. Why do I keep getting this error?

When you think, it worked last week, you should recall what you did within that week which could break your existing code.
I suppose you have created a new class named Double. Without it, java automatically imports java.lang.Doubleif you use Double, but if you have a class with the same short name within your package, that class is preferred. And that class does not have the method parseDouble.
So you can do either, refer explicitly to java.lang.Double instead of just Double or remove the new Double class from your package.
But that’s just a guess. There’s too little information about your environment.

Related

(beginner) Printf error that I just can't seem to fix [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 1 year ago.
I'm trying to code something simple that asks the user for a radius and an angle so that it can calculate the cartesian coordinates x and y, but I'm very new to any kind of coding and can't seem to get it right. At the moment, when I try to compile it I have the error: cannot find symbol (the final printf line) I've gone through my lecture slides and searched on google for ages now and nothing seems to work, I feel like I must be missing something super obvious and simple. That or my whole code is a mess, I really don't know what I'm doing here so any help would be appreciated.
Is the out.printf written wrong, what am I missing?? Or the following string, I don't fully understand the \n so maybe that's where i've messed up? Any help would be amazing, this is only my second piece of code and I'm really learning through trial by error.
import java.util.Scanner; //import scanner and math class
class Cartesian
{
public static void main(String[] args)
{
// Create a scanner that can read numbers, or words
Scanner input = new Scanner(System.in);
System.out.print("Enter the radius");
double radius = input.nextDouble();
System.out.print("Enter the angle");
double angle = input.nextDouble();
// given formulas
double xValue = radius*cos(angle);
double yValue = radius*sin(angle);
// formatted output
out.printf("x equals %.1f and y equals %.1f \n", (xValue),(yValue));
}
}
So you have code lines:
System.out.print("Enter the radius"); and System.out.print("Enter the angle"); ... similarly, your out.printf() should be called from the System object, so you should change out.printf(...); to System.out.printf(...);
I'm not sure your compiler would know cos and sin as well... those might have to change to Math.cos( ) and Math.sin( ) if you get further errors.

Java Beginner - Java Code Errors Advice Needed [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
First off, I am a BEGINNER in Java. I am finally taking my core classes in college. I am in Computer Science 1 and I'm correcting a code I got from a book as a practice so I can gain a better understanding on how to fix codes. It's a simple code, however, I keep running into 3 errors every single time. I need advice on how to correct them! I am new to all of this so it can be frustrating at times. Thanks!
public class Mistakes
{
public static void main(String[] args);
{
int y;
int x = 12;
y + x = y;
System.out.println("This is what y + x equals: "+y);
System.out.print("This is what t equals: "+t);
}
}
I keep running into 3 errors:
java:3: error: missing method body, or declare abstract
public void main(String[] args);
^
java:7: error: unexpected type
y + x = y;
^
required: variable
found: value
java:9: error: cannot find symbol
System.out.print("This is what t equals: "+t);
^
symbol: variable t
location: class Mistakes
Do I change t into x?
Do I change public class to public abstract?
Any advice will be greatly appreciated!
First, your main() method has a ; after it's declaration. This is allowed only if the method you are declaring is abstract and, thus, has no "body". In this case, you should remove the semicolon. Look below:
public static void main(String[] args) {
//Your code here
}
Second, your assignment operation is wrong. In Java (and in programming in general) you must first specify a variable and then the value it is going to receive (literally or, in your case, through an expression). You should do it as shown below:
y = x + y; //the value of y will be equal to x+y
In this case you could even use a shortcut, if you want to:
y += x; //this expression will have the same effect as the shown above
Finally, you are getting the last error because the variable t wasn't declared, so the method System.out.print() is trying to print a variable that doesn't exist. In this case, you should either remove the symbol t or declare a variable with this name, like I do below:
int t = 3;
System.out.print("This is what t is equals to " + t); //t will be 3

having trouble creating a rounding program java using command lines [duplicate]

This question already has answers here:
Variable might not have been initialized error
(12 answers)
Closed 6 years ago.
I'm fairly new to coding and I've been learning by creating simple programs.
I'm trying to create a program called roundGrade to round a grade to one decimal place by calling onto the command line.
The error stated:
Error: variable roundGrade might not have been initialized
Here's the code I've written so far:
public static String roundGrade(double grade){
String roundGrade;
double R = Double.parseDouble(roundGrade);
R = Math.round(grade*10)/10;
roundGrade = Double.toString(R);
return roundGrade;
}
You are attempting to parse roundGrade before you set it to anything (and for no apparent purpose). This
double R = Double.parseDouble(roundGrade);
R = Math.round(grade*10)/10;
should be something like
double R = Math.round(grade*10)/10;
And your entire method could be
return String.format("%.1f", grade);

Java Using Arguments in a Complex Formula

I am new to Java, and I am reading a book on it now. The book does not give me the answer. I am using the following code:
package loanpayments;
public class LoanPayments {
public static void main(String[] args) {
double years = Double.parseDouble(args[0]);
double P = Double.parseDouble(args[1]);
double r = Double.parseDouble(args[2]);
double R = r / 100;
double A = P*(Math.E*Math.exp(R*years));
System.out.println(A);
}
}
I am testing the code with the following values:
years = 3
P = 2340
r = 3.1
First I have to divide r by 100 to get a correct value (in this case it becomes 0.031). The new value of 0.031 becomes capitalized R. Then I use the formula to find A.
I am getting an incorrect output of ~6980.712, when the output should instead be ~2568.060.
I am thinking that I put in the formula wrong, it should be this:
Pe^R(years)
In this case e is Euler's number (~2.71828)
If anyone could advise me on how to fix the formula, or some other mistake, I would much appreciate it, thanks.
Not needed to multiply with another e because Math.exp() is already the exponential function.

Double number returns stackoverflow [duplicate]

This question already has answers here:
What is a StackOverflowError?
(16 answers)
Closed 8 years ago.
The highest number that double can represent is extremely high, I thought.
Though following code throws an exception. It's actually my full code.
public class Summe {
public static void main(String[] args) {
System.out.println(summe(20000));
}
public static double summe(double s) {
return s == 0 ? s : s + summe(s-1);
}
}
Thanks for the answers so far. My question is: How can I make my code work?
The problem here isn't the size of number a double can hold - the problem is the size of the stack. Here, you have 20K nested call to summe, which is way too much for the stack to handle, and hence, it overflows. If s were an int instead of a double, you'd have the exact same problem.
You made too many recursive call to summe.
You should read this question carefully to get a full explanation : What is a StackOverflowError?

Categories