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

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

Related

Getting a nullpointer exception in variable initializer [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I am relatively new to Java and ML but have been trying to learn, so forgive any noob mistakes. I have been following an article about ML in python have been trying to kind of translate it to Java to train the language and learn about the subject in the process, I have been getting a NullPointerException at the line of the variable loss and when I try to initialize the method in the main class, I can`t seem to find the problem.
I have tried running the method in a non static way and just rewriting everything, but it doesn`t seem to work.
public static double calcLoss() {
for (int i = 0; i < y_true.length; i++) {
double a = (1 / y_true.length) * Math.pow(y_true[i] - y_pred[i], 2);
loss = a;
}
return loss;
}
I know it says not to do it but here is a pastr with the full code, maybe this helps. https://pastr.io/view/bSacVk
This is the error
>Exception in thread "main" java.lang.NullPointerException
>at Net.Neuron.calcLoss(Neuron.java:40)
>at Net.Main.main(Main.java:28)
I have been getting a NullPointerException at the line of the variable
loss and when I try to initialize the method in the main class, I
can`t seem to find the problem.
That's because one of y_true or y_pred is likely null. You can easily handle this case by adding a if statement like this,
public static double calcLoss() {
if (null != y_true && null != y_pred && y_pred.length == y_true.length) {
for (int i = 0; i < y_true.length; i++) {
double a = (1d / y_true.length) * Math.pow(y_true[i] - y_pred[i], 2);
// This is just re-assigning loss. Are you sure this is what you intended here?
loss = a;
}
return loss;
}
// y_true is NULL.
return -1;
}
Also, I'm not sure what is the original intent here but, you are just reassigning loss in every iteration of the loop.

Values stored in long variable is not as expected [duplicate]

This question already has answers here:
Why do these two multiplication operations give different results?
(2 answers)
Closed 6 years ago.
I am confused how it's happening. Output is not as expected.
public class Test2 {
public static void main(String arg[]){
int interval = 43200;
long tempInterval = interval * 60000;
System.out.println(tempInterval);
}}
Expected output is 2592000000 but I'm getting -1702967296. It might be naive
question but I'm stuck with this.
Add a L after 60000.
Java assumes that you're using int multiplication, which causes an int overflow before it's casted to a long.

NullPointerException in Java my java program [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I'm working on a program using eclipse that generates objects for runners in a 100 m race.
each runner has a lane, name and three separate times for completing the race.
However, when I try to generate the times for each object, I get java.lang.NullpointerException.
Here is the method for generating the times.
public double[] getTimes() {
for (int i = 0; i <= (times.length - 1); i++) {
rolled = 1.0 + roll.nextDouble() * 100.0;
// set rolled to a new random number between 1 and 100
times[i] = rolled;
// set index i of the array times to be the nearest
// double to roll's value.
}
return times;
}
and then the code in which the method is called.
public void testGetTimes() {
double[] times = performance.getTimes();
assertEquals(2, times.length);
assertEquals(9.2, times[0], 0.01);
assertEquals(9.4, times[1], 0.01);
}
I'd try to fix it through debugger, but every time i try to step-into the for loop, i get InvocationTargetException,(Throwable line: not available
initialization of times, roll and rolled:
public class Performance {
private int lane;
private String name;
double[] times = new double[3];
int rolling;
Random roll = new Random();
double rolled;
double average;
double best;
and of performance:
public class PerformanceTest {
Performance performance;
#Before
public void setup() {
performance = new Performance(1, "", new double[]{9.2, 9.4});
}
It looks like one of your objects is uninitialized. On the other hand, initializing it to null can cause the same problem. As of Java 8, I would suggest using an Optional to contain any values that you are not immediately certain of, or are not immediately aware of, and put their processing in an Optional.ifPresent(Consumable) statement. It minimizes the possibility of a NullPointerException, and invites functional programming methods, which will seriously slim down the amount of time it takes to get the job done.
As far as your assertion error goes, I'm sure you already know that this is only caused by an assert statement. Looks like times[0] is substantially larger than you were expecting. It appears, from what we can see, that you're setting it to 1.0 + random{0.0...1.0} * 100.0; I don't know what the precondition that leads you to expect 9.4 is, but this could easily hit the forties.

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?

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

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.

Categories