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?
Related
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.
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);
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 7 years ago.
I'm trying to create this two dimensional array in java and print the output, but when it outputs I am not getting the numbers or a two-dimensional structure. Java appears to be giving me what looks like memory addresses for each array result.
My code
public static int[][] generateSampleTable(int smallest, int largest, int sampleSize)
{
int sampleTable[][] = new int[sampleSize][2];
for (int i = 0; i < sampleSize; i++)
{
sampleTable[i][0] = 5150;
sampleTable[i][1] = 2738;
}
return sampleTable;
}
public static void main(String[] args){
System.out.println(Arrays.toString(generateSampleTable(1,1,10)));
}
My output appears like this:
[[I#803816, [I#22b4b7, [I#1079ed1, [I#231c5d, [I#178cf47, [I#d65c13, [I#1fca60e, [I#1f99518, [I#fa7e97, [I#90925f]
Ignore the smallest and largest arguments in that method as I haven't implemented the calculations for them yet and am not using them right now.
Other than what you see here, I guess I should tell you that yes, I imported java.util.Arrays
Not sure what is going on. Any help would be appreciated.
Thanks
Arrays.toString() knows how to print 1 dimensional arrays. For 2-dimensional arrays you should use Arrays.deepToString().
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have doubt in using functions in java. I have wrote code for sum of natural numbers using a recursive function but I don't understand the error I am getting. I know it's a silly question though I'm a beginner and I need a brief explanation.
Here is the code
import java.util.Scanner;
public class natural {
public static int main(String args[]){
int a, s = 0,y;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number:");
int x = in.nextInt();
public static int SN(y)
{
if(x==1)
{
return 1;
}
else{
int N = SN(x-1) + x;
return N;
System.out.println("THE SUM IS :"+x);
}
}
Several problems:
You cannot declare a method within a method. Your SN method must be declared outside of the main method.
The parameter y in your SN method must have a type. Based on usage, it is probably supposed to be an int, so the method signature should look like SN(int y).
Despite the method parameter being called y, you appear to be using x everywhere. You should change x to y in the SN method, since that is the label of the data being passed to the method.
As others have pointed out, statements after the return line are unreachable, and as Matt Coubrough said, your IDE is likely warning you about this. Place it before the return line.
Well, one problem here is that you have an unreachable statement. Your System.out.println("THE SUM IS...") is never reached.
This question already has answers here:
Comparison Method violates its general contract in Java 7
(3 answers)
Closed 8 years ago.
I have the following compare function:
#Override
public int compareTo(OptimizedMatch another) {
long c = this.compareTime - another.compareTime;
if(c>0){
return 1;
} else if(c<0){
return -1;
}
return 0;
}
whereas compareTime is calculated in this way
this.compareTime = this.dateStamp + (LENGTH_OF_DAY - (this.dateTimeStamp - this.dateStamp));
Why am I getting this error?! I am comparing two long values, shouldnt that be just standard?
EDIT: I found the error, it was my IDE (Eclipse) being buggy. After cleaning my project and restarting it works.
There might be something wrong with your equals() method.
also, isn't
this.dateStamp + (LENGTH_OF_DAY - (this.dateTimeStamp - this.dateStamp))
equivalent to
2*this.dateStamp + LENGTH_OF_DAY - this.dateTimeStamp
I don't know what those fields do, but it looks like it's not what you want to do there.