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 7 years ago.
Improve this question
I have been working on a data compression and decompression program in java. At some point in my code, I want to visit only the nodes with keys. The part of the code looks like this:
//visit only nodes with keys
if(n.alpha != '\0') {
System.out.println("{" + n.alpha + ":" + s + "}");
charToCode.put(n.alpha, s);
codeToChar.put(s, n.alpha);
}
'\0' gives me an invalid character constant. I need to know what is going on, and how I can remedy the situation. Thanks!
You're trying to represent and treat a String as a char
n.alpha != '\0' // single quotes denote a character
You need to use a String instead
n.alpha != "\0"
Then, because the equality operator generally shouldn't be used with Strings, and because you're incorrectly testing the equality between a String and a char, you need to rewrite it as
if(!Character.toString(n.alpha).equals("\0"))
Note the use of Character.toString(n.alpha) to convert the char n.alpha to a String.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to learn Java. My current assignment is to build a simple four function calculator..... this would be easy given if/else and/ or switch statements, but I'm supposed to build this using methods.
The original input has to be put in as a single string, so, in my mind, I'm going to have to take the single string and create substrings, then somehow convert these substrings into double values, while deleting whatever whitespace could possibly be between characters. My current idea is to somehow identify the "+,-,*, or /" within the string and divide into substrings before and after these values, using the appropriate defined method for whichever operator to do the calculations....
The problem is that I can't see a good way to divide these up into substrings or how to convert the numbers involved into double values. Anyone got any advice for me? Keep in mind, what we have gone through is pretty limited and I feel like I'm missing something REALLY simple out there.
You can split a string based on a particular character using str.split("\\+"), for example. You can convert the split pieces of the string to doubles by using Double.parseDouble(str);
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 5 years ago.
Improve this question
Write a java code to check if the given string is even or not? Eg. aabbcc, aacbbc is even string.
I was asked this program in one interview. Actually i did not understand what is frequency here.
For a string s of length n, consider s[0] XOR s[1] ... XOR s[n - 1] where [i] is the (i)th letter of the string. Use java.lang.String#charAt(int) in java to extract a character.
If that is zero you have an even string, else you have an odd string.
Test n % 2 first for an immediate pay rise: If that is not zero then there must be at least 1 occurrence of a character that appears an odd number of times.
Normally folk who wrote computer games in machine code as kids in the 1980s will ask this question as it seems obvious to them. I doubt it is any more: XOR was a very fast way of writing sprite images.
Depending on what the interviewer was asking, string frequency is either,
how many times a string is found in another string.
how many times a character is found in a string.
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 7 years ago.
Improve this question
I got some data.txt file with N-string. Each string consists of 6 digits separating by space. I need to read this but before I need to check data format (I mean that each line must consist only 6 and only digits).
Do I need to use regexp?
Yes, you can do it with a regex.
One way to do this is to check if for each row you read, you have digits only, and add a constraint for the number of digits. The digits part can be done pretty easily, using a "/d" while the number of characters to be used is constrained by "{desiredNumber}".
To better understand regex in Java, use this link :D
If you still cannot solve it, this is the magic line:
if (!newLine.matches("\\d{6}")) {
return false;
}
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 7 years ago.
Improve this question
I am getting a StringIndexOutOfBoundsException for a long string. Basically the program is reading string from text file and for some reason i am getting error when I use long strings. For e.g. if use a=b+c or a=b or a=b+c-d*a this all works but when i put long strings such as "programming" or "javatutorial" this gives me a StringIndexOutOfBoundsException. At first I thought this was due to the fact that I am not checking whether or not x is empty but that is not the case this is occurring due to the length of the string itself. I would appreciate if someone could help.
while (scan.hasNext()) {
String x = scan.nextLine();
try
{
if(!x.isEmpty())
{
char ch=x.charAt(0);
s=String.valueOf(ch);
}
}
catch(StringIndexOutOfBoundsException siobe)
{
System.out.println("invalid input");
}
}
With the partial code you provided, all we can tell you is that the exception is being thrown because you are using the method:
String.charAt(int index)
on a String that does not have a character at the 'index'-th position.
For example, if String word = "cat", then word.charAt(8) would throw an exception because 'cat' only has three characters.
Search through your code for all the places that you used the charAt(int index) method and test that 'index' is indeed less than String.length(). Your error shows an argument of '11', so you can narrow your search to the location where you called the charAt() method with an index of 11. (This may be inside a loop).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I have a little/huge problem with String comparison in Java, I want to compare two Strings and .equals([...]) does not give me the correct result.
I also tried the following: ==, .compareTo([...]), .trim(), .equalsIgnoreCase([...]), creating a Collator with default Locale and using [collator].compare.
(All fail to work)
The first String comes from an already created object (the content of the string is from a database), the second String comes from a newly created object (but has been passed to a method), the content of this String is from the same database.
I am pretty clueless what to do now, the last thing I'd try is to convert it to some number (i.e. hex).
I already tried to write both Strings to console and manually look for differences, but there is none...
Code is this:
public static Lagerplatz hinzufuegen(Lagerplatz lagerplatz) {
boolean neu = true;
if (lagerplaetze.isEmpty()) {
lagerplaetze.add(lagerplatz);
System.out.println(lagerplatz.getBezeichnung() + " erstmalig hinzugefügt!");
}
for (int i = 0; i < lagerplaetze.size(); i++) {
System.out.println("'" + lagerplatz.getBezeichnung() + "'" + " - " + "'"
+ lagerplaetze.get(i).getBezeichnung() + "'");
if (lagerplatz.getBezeichnung().equalsIgnoreCase(lagerplaetze.get(i).getBezeichnung())) {
neu = false;
}
}
if (neu) {
lagerplaetze.add(lagerplatz);
System.out.println(lagerplatz.getBezeichnung() + " hinzugefügt!");
}
return lagerplatz;
}
The if-part with (lagerplaetze.isEmpty()) does work, after the first one is added it should check if the Lagerplatz (at least the name of it) is already in the lagerplaetze-ArrayList, if so then don't add, if not add.
Stepping through it revealed that the objects are correctly referenced...
Thanks very much in advance, and sorry if this question has been answered already but I can't find a helping answer under all these questions...
EDIT1: Normalizing does not help in this case, "umlaute" (german ä, ö or ü) are not causing the problem...
Converting the String to a byte[] and converting this byte-array to String like:
(Arrays.toString(lagerplatz.getBezeichnung().getBytes()).equalsIgnoreCase(
Arrays.toString(lagerplaetze.get(i).getBezeichnung().getBytes())))
and then comparing it does also not solve the problem, the bytes of the two strings are exactly the same: '[72, 55, 48]' - '[72, 55, 48]'
EDIT2: The problem is not with the String comparison, it is because the variables of the class "Lagerplatz" are static, they were replaced each time the loop is entered...
Maybe delete this question?
The only time two strings that look exactly the same but equals() does not result in true is when the unicode composition is different.
For example one can compose the A umlaut (Ä) with a single character: \u00C4
Or with a combination of the A character and the dots (the dieresis character ¨): \u0041\u0308
In essence, you are using two unicode characters for one letter. Because equals() compares characters, the form with dieresis is not equals to the form without.
To overcome this problem, one must decompose each string to a canonical form before comparison.
In Java one can create such a canonical form like this:
java.text.Normalizer.normalize("Your String", java.text.Normalizer.Form.NFD);
Once normalized, equals() will work as expected.
Obviously, since you didn't provide any data, this answer may or may not match your problem.
In any case, you might want to normalize all Strings in some form and then use a Set as data structure, not a list.