I try to use following code to initialize a BigInteger at time of compiling (using breakpoint) in JAVA. So, I cannot use BigInteger x = new BigInteger(String str); because this is at time of debug the code by breakpoint.
the code:
BigInteger x = BigInteger.valueOf(36894801013086644936129270378595901597221307673940538800722758616305130630160);
error:
The literal 36894801013086644936129270378595901597221307673940538800722758616305130630160 of type int is out of range.
If I use BigInteger x = new BigInteger(String str); and then run the code, it will be OK, but I want to debug the code by some breakpoint and initialize BinInteger x (or change previous value of x) manually at time of debug.
The problem is that x keeps its previous value regardless to new value. apparently, only the "first initialization" value is kept. But, I want to change it to another value at time of debug, by a new value that is : 36894801013086644936129270378595901597221307673940538800722758616305130630160.
I appreciate any help.
In fact, I found a solution to do my test. I needed the value of BigInteger in another class that I changed the type of BigInteger to public static. So, I think now it would be better I delete my question.
Related
Im working on a homework assignment for my intro to computer science class and we are are inputting basic commands to get the percentage of people who drink a certain kind of energy drink. We used JOptionPane to make text boxes and you can input the amount of people and the computer has a set percentage to get the output. My problem is i set up my variables as doubles and my answers are very long decimals. I want to convert the answers to Ints so i can get whole numbers. I have tried to do this through casting but i keep getting the error message" EnergyDrink.java:14: error: variable citrusEnergyDrinkers might not have been initialized". What can i do?
This can't be solved without code. The error is not due to any problem with the conversion, but simply as the compiler-error says:
variable citrusEnergyDrinkers might not have been initialized
This means that the variable might not hold a value at the time you attempt to convert it, which results in undefined behaviour, which java-designers didn't allow for a reason.
The problem is as the error-message tells: citrusEnergyDrinkers gets its value inside some try-catch-block or a block that is only run under certain conditions, like if. One way to work around this would be to simply initialize citrusEnergyDrinkers as 0:
double citrusEnergyDrinkers = 0;.
Note though that this might produce incorrect results depending upon what happens when the value isn't set in case the above mentioned block of code isn't entered/breaks off before setting a value.
For the conversion:
Math.round(citrusEnergyDrinkers) is most likely preferable to a simple cast to int, since double most of the time has some imprecision due to the way it's stored in memory and round will actually round the value, while a cast will simply remove the frictional part. For example:
(int) 0.75 //produces 0
Math.round(0.75) //produces 1
You could multiply the double by 100 and then cast to an int:
double d = .77583495;
int perc = (int) Math.round( d );
I prefer to not cast like that, but it works.
Good luck.
Iam trying to implement the buffer overrun problem of a C program using java with the help of eclipse CDT .
By giving a constant value as the array subscript, its working fine as I expected.
See the sample code:
CASTArraySubscriptExpression exprsn = (CASTArraySubscriptExpression)astName.getParent().getParent();
String size = exprsn.getSubscriptExpression().toString();
System.out.println("Size : " + size);
Using this code, able to detect the array subscript value of the below code:
int a[10];
a[12] = 4;//Here it detect the buffer overrun problem.
But if I give like this:
int a[10];
int i = 21;
a[i] = 4;
Here, not able to detect the value of the index i.
How I can detect the value using CDT?
In the second case it is not enough to just look at the AST of a subscript expression to detect an error, you need at least some basic data flow analysis.
However, according to CDT/designs/StaticAnalysis building a data flow graph is planned as future work, so you either have to do it yourself or wait until it is implemented.
As a simple solution for the special case like your second example, when you have a local variable reference in the subscript, you can check if the variable is not used anywhere in the AST between the initialization and its use in subscript.
I am having issues with Achartengine's XYSeries getRange(double, double, int) method.
I am new to Java programming so this issue sort of boggles me.
I am not sure how to use it, since I always get errors when I try it! What is the object that goes in front of it to get values from getRange? I.E:
object here.getRange(double, double, int);
and would I set that equal to something too? Thanks!
What is the object that goes in front of it to get values from
getRange?
XYSeries.getRange(double start, double stop, int beforeAfterPoints);
Returns submap of x and y values according to the given start and end
and would I set that equal to something too?
Use it like this :
SortedMap<Double, Double> map = XYSeries.getRange(double start, double stop, int beforeAfterPoints);
to get y for x value
map.get(X_Value);
Links:
Class XYSeries
Viewing Sub Maps
Hope this helped you.
I sometimes tend to use (double)(long)(a*b/c) to store the integer part of the result as double. This works well for negative numbers too.
Is there any better way to achieve the same thing as I believe typecasting is a costly operation.
Please note I'm looking for Integer part of the number and not the rounded value.
For eg :
MyObj.setDouble((double)(long)(522.99))
MyObj.getDouble() returns 522.0 and not 523.0
Thanks.
Try Math.rint(double) or Math.round(double). Regardless of performance differences it's at least more clear and concise.
[Edit]
In response to your clarified question - "how do I get the integer part of a double without casting" (despite your title asking about rounding), try this:
public static double integerPart(double d) {
return (d <= 0) ? Math.ceil(d) : Math.floor(d);
}
integerPart(522.99); // => 522d
integerPart(-3.19); // => -3d
Of course, this form is likely no faster than casting since it's using a comparison and a method call.
Performance is not an issue here. But code (double)(long)(a*b/c) is ugly. You actually do not need casting at all if you assign the result to `double variable:
double d = a*b/c; exactly the same as double d = (double)(long)a*b/c;
You actually never need to perform casting when moving from lower to upper types. It is correct for primitives (e.g. int -> double) and for classes (e.g. ArrayList -> List).
What about Math.floor(double) I cant see the difference between integer part and rouding it down.
I'm new to Java, and I'm using Processing to make some data visualizations. I'm getting this strange error in my code though, was wondering if anyone could help me out. It seems the Xspacing float keeps getting set to Infinity, however when I print out the expression it gets set to the proper value gets printed...
float Xspacing = (endX-(width*.04) - startX)/ values;
println((endX-(width*.04) - startX)/ values);
println(Xspacing);
Result is:
49.0
Infinity
Any help would be appreciated!
Sorry, I wrote this out very quickly and omitted some pretty necessary info:
49.0 IS what is should be. All other types are floats, besides values which is an integer.
The code DOES compile, and println is build into Processing, which is the framework (correct term?) that I'm using. It is basically a function that prints to the console in the Processing GUI.
Xspacing was intended to be data for my class "Graph," however when I define the variable within a public function "drawBasic" everything works fine. Now I am just curious....
Using System.out.println(0 yields the same results. Initial values or variables are:
float startX = 120.00001
float endX = 740.0
int values = 12
width is an integer (although not explicit) that is set to 800
The odd thing seems to be that within a function definition this works fine, its only when I try to define it within the class that it doesn't work...
Your code couldn't be like that because a number *.04 creates a double, and that would mean you'd need to cast the expression into a float.
For your code to compile it would have to be something like
float Xspacing = (float)((endX-(width*.04) - startX)/ values);
println((endX-(width*.04) - startX)/ values);
println(Xspacing);
Now, on the result. If your code had, for example:
System.out.println(3/0);
Java would give you a java.lang.ArithmeticException: / by zero
However, if you have
System.out.println(3f/0);
Then Java will give you "Infinity". Why? http://grouper.ieee.org/groups/754/
Try this:
float Xspacing = (endX-(width*.04) - startX)/ values;
println((float)((endX-(width*.04) - startX)/ values));
println(Xspacing);
float Xspacing = (endX-(width*.04) - startX)/ values;
Even assuming the variables are floats that line does not compile, because of the 0.4 double literal.
Also 'println' is not a standalone method, so you must have written your own.
What is your actual code?
you forget a ) and you should've put System.out.println(xspacing);
fyi you can also just type syso and ctrl spacebar and it will print out the print statement for you.