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.
Related
Hey Guys I am using a list of lists in java.The list is called deg_grp and the code below shows the output of the deg_grp at a particular index j.
for(int k=0;k<deg_grp.get(j).size();k++)
{
System.out.println(deg_grp.get(j).get(k));
}
The output is:
1.0
2.0
4.0
6.0
8.0
So these are my values. But when I replace the print command by
System.out.println((int)deg_grp.get(j).get(k));
It is throwing me an error.I need to perform int conversion for indexing purposes later on.The error is:
incompatible types: Float cannot be converted to int
So I am unable to run the code. Kindly help me out. Thanks :)
Use Float#intValue() for your conversion.
You can't unboxed and convert types in one step. You can do (int)(float) f however I suggest not using Float, use double or Double for more precision and using Math.round(d) to round the result, to minimise errors.
System.out.println(Math.round(deg_grp.get(j).get(k)));
i am trying to do some calculations in Java,
but for some reason a simple calculation in 1 or 2 lines of code gives me the wrong answer,
while on the other hand if i do it in 3 steps it works flawlessly.
i know that's it not that bad to do something in a couple more steps,
but why use extra text if it can be shortened??
Could someone give me a pointer if my math is wrong??
this is the 1 line code
percent1 = (((totaloutput1Int - Total1Int) / totaloutput1Int) * 100);
also = (((2232 - 1590) / 2232) * 100)
and this is the multiple steps code which does work.
percent1step1 = (totaloutput1Int - Total1Int);
percent1step2 = ((percent1step1 / totaloutput1Int)* 100);
percent1Tv.setText(String.valueOf(percent1step2));
Change totaloutput1Int and Total1Int from int to double and everything will work fine.
In the 1st method, int/int leads to rounding off of the value. Which leads to a different result.
You need to convert some of your variables to a double to get an accurate answer. int / int is going to give you an int Take a look at this question
So as this is tagged Android, I'm assuming you are using Android Studio. One of it's great features is in-lining (also available in most modern IDEs).
Take this:
float percent1step1 = (totaloutput1Int - Total1Int);
float percent1step2 = ((percent1step1 / totaloutput1Int)* 100);
If you rightclick percent1step1 and select "refactor->inline" android studio will so this:
float percent1step2 = ((((float)(totaloutput1Int - Total1Int)) / totaloutput1Int)* 100);
So it shows you how to achieve things inline, without multiple lines. In this case the result is convert the int from the subtraction in to a float.
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.
I am trying to divide two integers values to get a float value.. and I always get the value 0.0.. already tried to cast the values to float and no chance anyway, here is the code:
float othersFloat = (float) others;
float totalPixelsFloat = (float) totalPixels;
// this variables have the values:
// othersFloat : 621347.0
// totalPixelsFloat : 654336.0
// then I do the divison like this:
float percentage_white_on_screen = (float) othersFloat / totalPixelsFloat;
//But I get the value 0.0
Can someone help me?
There is nothing wrong with the code you have shown us. If the inputs are given by the comments, then the result should not be 0.0.
So if it is then either:
the actual inputs do not have the values that you think that they do,
the actual output value is different to what you think it is; e.g. you are printing a different variable ... or something after this code is changing it, or
that is not the code you are executing it; e.g. you've not recompiled it and the code you are running no longer matches the source code.
If this does not help you find the real cause of the problem, you will need to write an SSCCE ... so that other people can actually reproduce your problem for themselves.
i have a java code for finding out netsalary . I keep Getting "bad operand type for binary operator ' / '" error . The Line goes like this
netSalary = Double.parseDouble(principle2*rate2/12*Math.pow(rate2/12+1))/(Double.parseDouble(Math.pow(rate2/12+1)-1));
Could this be solved . Thanks in advance!
It should be commas you use, not slashes.
Math.pow(rate2/12+1) should syntactically be in the form of Math.pow(x,y) where both x and y are doubles. The first arg is the base, and second arg is the index you're raising it to. As the comment below mentioned, it's difficult to understand what you're trying to achieve, and you'll have to substitute x and y for the correct values - make sure they're of type double (you cannot use, for example, 12+1 as a parameter because it is an integer). If it is an integer, then type cast it using (double) in front of the value.
You also do not need to parseDouble everywhere since Math.pow will return double values anyways; it is redundant.
I agree with the comment above; please read the javadocs for any problems you're having with a method before posting here.
You mistake in using power function your power function should be like this
Math.pow((rate2/12+1),1)