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 8 years ago.
Improve this question
Hi I'm trying to subtract an int from a char but I keep being told that the compiler "cannot convert from int to char".
I have tried changing the constant to a char but it didn't help.
is there any easy way to do this subtraction?
test[1] = characterArray[1] - ASCII_SUB;
Any help much would be appreciated.
The problem is that subtraction is never performed on char values in Java. Instead, both operands are promoted to int (via binary numeric promotion), and the result of the subtraction is an int as well. So you'll need to cast the result back to char:
test[1] = (char) (characterArray[1] - ASCII_SUB);
Related
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 3 years ago.
Improve this question
I am getting wrong output while using java Math functions.
expected output = 4.32
present output= 4.30287105016515
double startHeight= 60.00;
double initaldistance=1000.00;
double speed=120;
startDistance=(initaldistance-1.688*speed);
startAngle = Math.toDegrees(Math.atan(startHeight/startDistance));
System.out.println(startAngle);
Your expected output is incorrect. The arithmetic expressed in the code in the question does indeed have a result near 4.30287.
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 6 years ago.
Improve this question
I have a character array as
char[] bitsString = new char[16];
bitsString = {'1','1','1','1','1','1','0','1','1','1','1','0','0','1','1','0'};
Then I converted it to the corresponding integer as follows:
int givenNumber = Integer.parseInt(new String(bitsString), 2);
The above logic works fine when number bitstring array length is less than 10. But when it is increased to 11 or more, it is showing me java.lang.NumberFormatException why ?
After some hit and trials I found that, it was the case since the 16 bit value is crossing the Integer limit.
But when it is changed from int to long
long givenNumber = Long.parseLong(new String(bitsString), 2);
it is working perfectly fine. Since in this case long has 64 bits length.
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 7 years ago.
Improve this question
The problem seems to be in the increment, but I need it to decrease by 2. The "length" variable is for the length of a series of numbers
public int longMethodName()
{
int length = cardNumber.length();
longMethodName = 0
for(int i=length-1; i<0; i-2)
{
int cardNumberInt = Integer.parseInt(cardNumber.charAt(i));
int tempVar = cardNumberInt*2;
longMethodName = longMethodName + tempVar;
}
return longMethodName;
}
You need to change it to i=i-2 or i-=2 to decrement by 2.
You might be trying to emulate the i++/i-- syntax, which is simply shorthand for i = i+1 or i=i-1. However, that syntax only works for change by 1 (formally speaking ++ and -- are unary operators ), so i-2 won't work directly.
You also need to fix the other errors, as detailed in the other answer.
1.) longMethodName = 0 // Semicolon missing
2.) i-2, need to change to i = i-2
3.) Integer.parseInt(), cardNumber.charAt(i)returns char which is not allowed
You can also use i-=2 so you don't have to write i a second time ;-)
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 7 years ago.
Improve this question
This is very weird.
A class I wrote has the following data member:
final static long MAX_FILE_SIZE_BYTES = 50000000000L;
at one point in my code the following block is run
System.out.println("MAXFILESIZEBYTES: " + MAX_FILE_SIZE_BYTES);
and the output is:
MAXFILESIZEBYTES: -1539607552
My question is, why is this long value overflowing? Java is supposed to be machine independent, and longs are supposed to hold 64 bits. What gives?
Cannot reproduce.
50000000000L is 0x0000000BA43B7400.
-1539607552 is FFFFFFFFA43B7400, which is what you would get if you cast the value to int.
Ergo somewhere you are casting it to int. Maybe you have a shadowed variable.
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 8 years ago.
Improve this question
The error I got is-
Main.java:23: error: bad operand types for binary operator '||'
if(c=='a'||c=='e'||c=='i'||c='o'||c=='u'||c=='y')
^
first type: boolean
second type: char
I didnt really quite understand it.
The reason is this:
c='o'
in your if condition.
Probably you are just doing this assignment by mistake. So you may want to update this to comparison using
c=='o'
||c='o'||c=='u'
you were using an assignment operator.
||c=='o'||c=='u'
if(c=='a'||c=='e'||c=='i'||c='o'||c=='u'||c=='y')
change c='o' to c=='o'
Actually assignment was happening in your if statement which is not allowed