Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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.
Improve this question
code will not compile
byte a = 127;
byte b = 1;
byte c;
c = (a + b); //eror
System.out.println("The value of c=" + c);
the compiler won't compile the code.
Java stores numeric types as int by default. Try:
c = (byte)(a+b);
The error occurs because an integer (4 bytes) cannot be contained within a single byte without losing information, which is why the compiler requests that you make this conversion explicit.
Additionally, FYI - single short and byte declarations are actually stored as a single word in memory (which is 4 bytes), so you are not saving memory while restricting the size of the number. This is not the case with arrays, where each slot in an array of byte or short will actually have 1 or 2 bytes respectively.
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 12 months ago.
Improve this question
This Code is giving error that lossy conversion from double to int.
int[] ans=new int[1e5+1];
The concept is simple: 1E5 is a 'double' floating-point number in Java because the Java language specification says it is.
Meanwhile, an array size must be integral, and the compiler is telling you that. If you really want to use a floating-point number there, you need to cast to int. However, it's easier to just write 100000.
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
I guess its a rather simple question but i just cant find my mistake.
int[] myIntArray = new int[20];
myIntArray[5] = 5;
int a = myIntArray[5];
TextIO.putf("arr[i]: d%",a );
The error I get is Illegal format string in TextIO.putf() method.
So I assume the value at the index 5 is not an int?
The error message says exactly what the problem is: your format string is wrong. You probably meant %d (or better yet, %d%n to add a newline).
The format string elements are in the form
%[modifiers]type
not
something%
Change d% to %d.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I want to convert integer type array that contain only 0 and 1 into string that should be simple sequence of array value not in any other form for example if array is
arr[] = {0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1}
than string should be
str = "0110100001100101"
than i want to divide it into two substrings like
str1 = 01101000(this is bit value of h as i know) and str2= 01100101 (this is bit value of e) and want to convert these substrings into character h from str1 and e from str2.
please help me .
You can use Arrays.toString()
Integer arr[] = {0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1};
String str= Arrays.toString(arr).replaceAll(",|\\[|\\]","").trim();
System.out.println(str);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
multiply any 2 numbers. The numbers can be extremely large (i.e. run into hundreds of digits) and are provided as strings.
The expected output is a string which represents the product of the two numbers.
example-
multiply("268435456","524288")="140737488355328"
multiply("12321412423524534534543","0")="0"
Use BigDecimal, which has a multiply method and a constructor which takes a String. It also contains corresponding toString() and toPlainString() methods to get your result as a string.
(If the numbers are always whole numbers, then use BigInteger instead.)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this code to generate a random key.
new BigInteger(130, random).toString(32);
From what I understand, the first part of the code will create a 130 bit, random, integer. Then, the .toString(int radix) method will be called on it and the number will be converted to an alpha-numeric string.
What I can't understand is which alphabet is used for the characters; in other words, which key is mapped to which character?
Note: I looked for how a Base 32 conversion can happen, but I couldn't come up with anything useful since it doesn't seem to be a unique method to do that.
The javadoc says:
" The digit-to-character mapping provided by Character.forDigit is used, and a minus sign is prepended if appropriate.".
And the latter javadoc says:
" If the digit is less than 10, then '0' + digit is returned. Otherwise, the value 'a' + digit - 10 is returned."