Reputation: 81
I am a beginner at Java and I am currently writing a code where all the char symbols are printed out the same but the letters are printed out differently. How is it possible to exclude the ASCII values for the symbols? Is it possible to do something like this (where the values such as 32 and 64 represent the ASCII values corresponding to the characters) :
char notLetter = (originalMessage.charAt(i));
if ((32 <= notLetter <= 64) || (91 <= notLetter <= 96) || (123 <= notLetter <= 126)){
codedMessage += notLetter;
}
Or is there a simpler way to do it ? Thanks
Edit: when i try this code, I get the following error: "<= cannot be applied to boolean, int"
Upvotes: 1
Views: 1346
Reputation: 73
I would use the Character Object Class, with the isAlphabetic() function: http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isAlphabetic(int)
ASCII and Unicode should match up.
Upvotes: 0
Reputation: 37655
I am not sure exactly what you are trying to do, but here is some general information.
char
literals like 'a'
rather than their int
values. This makes programs much easier to follow.StringBuilder
rather than string concatenation in most cases.2 <= a <= 5
, so you have to do 2 <= a && a <= 5
instead.The following code prints , !
String x = "Hello, World!";
StringBuilder sb = new StringBuilder();
for (char c : x.toCharArray()) {
if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
sb.append(c);
}
}
System.out.println(sb.toString());
Upvotes: 1
Reputation: 5046
32 <= notLetter >= 64
is not legal in java, but 32 <= notLetter && notLetter >= 64
is allowed. However, that also will never be true — did you mean 32 <= notLetter && notLetter <= 64
?
One additional thing to note that may help you: you can actually use <=
with a char on both sides:
(' ' <= notLetter && <= '@')
If I am understanding what you want to do, this will do what you want:
char notLetter = (originalMessage.charAt(i));
if ((' ' <= notLetter && notLetter <= '@') || ('[' <= notLetter && notLetter <= '`') || ('{' <= notLetter && notLetter <= '~')){
codedMessage += notLetter;
}
Upvotes: 0
Reputation: 2136
char notLetter = (originalMessage.charAt(i));
if ((32 <= notLetter && notLetter <= 64) || (91 <= notLetter && notLetter <= 96) || (123 <= notLetter && notLetter<= 126)){
codedMessage += notLetter;
}
try this.
32 <= notLetter >= 64
This won't work for 2 reasons:
Upvotes: 0