JAVA: Invalid literal hex number [closed] - java

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
Why am i getting a "Invalid hex literal number" for this
int number = 0xgetInt();
and not for this
int number = 0x555;

getInt() is a function call, not a "literal number" as the exception says. There's no reason to try to convert that int to hex at this particular spot, since it's stored as a variable anyway.

0x is the start of literal and is converted into the appropriate value at compile time where as getInt() is evaluated at runtime.
You probably want
int number = Integer.parseInt( getNext(), 16)

A "hex" or hexadecimal number can only contain the digits 0-9 and characters a-f (or A-F). In java, prefixing a number with "0x" indicates to the compiler that it's a hexadecimal integer literal. 0xgetInt() is therefore interpreted as an integral value, but it contains invalid hexadecimal digits. 0x555, on the other hand, is a valid hex value that corresponds to 1365 decimal. If 0xgetInt() is supposed to be a method name, you'll have to change it to get rid of the "0x" prefix to avoid this problem.

Because you can't start a Java identifier with a number, and because a number can start with 0x but it can't continue with getInt. It's not valid Java, at the lexical level.
It's unclear what you're trying to do here. If you're trying to convert the result of getInt() to hex, you're barking up the wrong tree, it's binary, you would need to convert it to a String with Integer.toString(getInt(), 16).

Related

Exception in thread "main" java.lang.ArithmeticException: Rounding necessary [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
when i try to get the longValueExact() :
BigDecimal bigDecimal = new BigDecimal(432.900).divide(new BigDecimal(1), 2, RoundingMode.FLOOR);
System.out.println(bigDecimal.longValueExact());
Exception in thread "main" java.lang.ArithmeticException: Rounding necessary
at java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4151)
at java.math.BigDecimal.needIncrement(BigDecimal.java:4207)
at java.math.BigDecimal.divideAndRound(BigDecimal.java:4115)
at java.math.BigDecimal.setScale(BigDecimal.java:2455)
at java.math.BigDecimal.longValueExact(BigDecimal.java:3093)
at com.tessi.bmd.specific.actil.utils.ActilUtils.main(ActilUtils.java:1281)
new BigDecimal(432.900)
This is a bad idea. 432.900 is a double literal and is therefore highly unlikely to actually represent 432.900. You're using BigDecimal, so presumably you know that there are only at most 2^64 numbers in existence that are exactly representable by a double. 432.900 is not one of them. Do not use this constructor - it has warnings all over it. Use new BigDecimal("432.9").
.divide(new BigDecimal(1),
Okay, divide by 1, not going to do anything. Also, use BigDecimal.ONE for this.
The value is still 432.899999999999434 or whatnot.
System.out.println(bigDecimal.longValueExact());
Of course that doesn't work - a long value can only hold integral values, and 432.9 (or something close to that) isn't.
Are you perhaps thinking that 432.900 is just a way of writing 432900 that is more readable to humans from certain locales where . is used as thousands separator?
. is the decimals separator. 432.900 is a double literal that represents the nearest representable double to the number 432 + 9/10ths. If that's your intend, remove the . - if you want to create some horizontal space for the yes, use _ which is legal in number literals and meaningless.
If that's not your problem and you really want 432.9 as an exact long - I guess, go back to square one and start learning java. Soon (as in, within a day or two, no doubt) you'll hit the part of the tutorial that explains the primitive data types. Pay extra attention to this section.
It is the normal behavior because the value you want to display as a long has decimal part (432.89, not 432.90 due to instatiating BigDecimal from a double) so is not an interger number. From the javadoc:
Converts this BigDecimal to a long, checking for lost information. If
this BigDecimal has a nonzero fractional part or is out of the
possible range for a long result then an ArithmeticException is
thrown.

Why boolean is set false at the beginning and is the code right? [closed]

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 3 years ago.
Improve this question
i would like to know first why Boolean is set to be false at the beginning and the pre-last "else" i ca not understand that condition that code is suppose to differentiate vowels from consonants ??
https://beginnersbook.com/2017/09/java-program-to-check-vowel-and-consonant-using-switch-case/
Please post the Code when you ask a Question and don't just put a link in here.
To answer your Question the Code is correct, just the 2nd ; in
boolean isVowel=false;;
isn't necessary. The boolean is set to false to Show that they assume by Default that the given char is no Vowel.
The switch basically checks if the given char is an a,e,i… and has to check for upper and lower case because they are treated different. If the given char Matches any of the given values the boolean is set to true because the char is a vowel.
The second last else Statement checks the UTF-16 values of the Alphabet, you can cast a char to an int which determines ist value in UTF-16 encoding for the lower cases it is 97-122 and for the upper cases their values are 65-90. If the int value of the char is not in this range the char is not in the Alphabet. You can refer to an ascii table to know which char is equivalent to which int.

CharAt in Java 8 [closed]

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 5 years ago.
Improve this question
The charAt isn't working... It's returning the hash code and not the value of a part in a structure.
Ex.: charAt(0) where is '1' is returning 49 and not 1
What Am I able to do?!
It >>is<< working. It is returning that character as a char which (presumably) you are assigning to an int and printing. The numeric value of the ASCII / Unicode codepoint for the character '1' is 49. If you want to print / display this as a character, cast the int to a char. (Or don't assign it to an int in the first place.)
For the record, the hashCode value returned by Character is identical to the character value. Strictly speaking a char doesn't have a hashCode because it is a primitive value, and primitives don't have methods.

Why when i loop a \n in an ' ' char + a number it counts wrong [closed]

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
Ok, so I have the following code. I know that making a new line in a char makes no sense but what I want to know is why it prints from 10-19 instead of 1-10 as normal. Can someone give me an answer?
public class Demo {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println('\n' + i);
}
}
}
The ASCII value for \n is 10. The + operator is being interpreted as an "add" operand between two numeric values, not as an "append" operation between two Strings. This is because you are using single quotes, not double quotes. Single quotes indicate a char.
A char can be treated as an actual number in Java. You can check all ASCII values on this chart.
JLS 4.10.1 - Subtyping Among Primitive Types pretty clearly displays that char is a subtype of int, if you're in to looking at the in-depth specifications. There's also JLS 5.1 - Kinds of Conversions for details on conversions.
Per Tom Blodget's comment below, it appears that Java actually uses UTF-16 for String literals, as detailed in the String documentation and JLS 3. This is likely an important fact to keep in mind, although I have used ASCII values successfully for many years and never encountered a bug or a problem of any kind.
String concatenation only applies when one of two operands of + is a String typed value. In this case, you have one char and one int, no String values. As such, you have integer addition. Java applies primitive widening conversion to convert the char value '\n' to an int value, 10. You can then see why it counts from 10 to 19.
As mentioned Java interprets '\n' as ASCII character, which correspond to a integer value of 10 (hex value 0x0A).
What you can do:
System.out.println('\n' + Integer.toString(i));
System.out.println("" + '\n' + i);
System.out.printf("\n %i", i));
System.out.printf("\n" + i));
But all ideas are connected to the same idea.

How to get a substring from a string [closed]

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 xml file from where I'm reading this string,
http://localhost:8080/sdpapi/request/10/notes/611/
My question is how can I get just the 611, which is of variable, can be 100000, for example, from this string?
Split the string
String input = "http://localhost:8080/sdpapi/request/10/notes/611/";
String output = input.split("notes/")[1].split("/")[0];
output is the value you need
What language?
Anyway, in most cases it's a syntax like:
String.substring(begin, length);
... where 'begin' is the number of the letter in the string-1. For extracting http from the above string you would write
substring(0, 4);
In case you always need the last string between the last two '/'s, you can retrieve the position of the slashes with index-functions (as stated in the answer of #Liran for example).
// EDIT: In Java the second parameter of substring is not length, but endIndex:
String s = "http://localhost:8080/sdpapi/request/10/notes/611/";
s.substring(46, s.lastIndexOf('/'));
It depends on programming language you use, but Regular Expressions should be the same in most of them:
/(\d+)\/$/
well, it depend in what language are you writing... in c# for example
string s = #"http://localhost:8080/sdpapi/request/10/notes/611/";
s.SubString(s.LastIndexOf('/'));
or
Path.GetFileName(s);
for java
new File(s).getName();

Categories

Resources