Cannot get program to recognize capital letters when input? - java

Java beginner here - hoping someone can help me solve this problem. I am trying to write a simple program that will display a particular message depending on the character entered by the user. The problem I'm having is that it won't recognize the validity of capitalized characters in determining which message to print.
The code compiles OK but if I enter a capital letter, it prints out the message telling me it is not a valid character to start an identifier.
Here is a snippet of the source code:
choice2 = (char) System.in.read();
if(choice2 == 'q')
break;
else
if(choice2 == '_' || choice2 >= 'a' && choice2 <= 'z' && choice2 >= 'A' && choice2 <= 'Z' && choice2 > '0' && choice2 <= '9')
System.out.println("That is a valid character to start an identifier.");
else
if(choice2 == '$')
System.out.println("That is a valid character to start an identifier but should only be used by mechanically generated source code");
else
System.out.println("Sorry, that is not a valid character to start an identifier");
break;
Is there something I'm doing wrong or is it something inherent to the char data type?
Thanks

The problem is with your boolean groupings and a couple && should be ||
if (choice2 == '_' ||
((choice2 >= 'a' && choice2 <= 'z') ||
(choice2 >= 'A' && choice2 <= 'Z') ||
(choice2 > '0' && choice2 <= '9')))
This will evaluate to true if choice2 == '_' OR if choice2 is either between a and z inclusive, between A and Z inclusive, or between 0 and 9 inclusive.

Related

Email validation Method creation

The method returns true if such character
is a letter of the English alphabet (uppercase or lower case) or one of the arabic numerals. The method
returns false otherwise
Method for ASCII character set
boolean isEnglishLetterOrDigit(char letter) {
return (letter >= 'a' && letter <= 'z') ||
(letter >= 'A' && letter <= 'Z') ||
(letter >= '0' && letter <= '9');
}

How to prevent IntelliJ to stop continuing double-slash comments on the next line?

Short version
When pressing <enter> at the end of a // comment, Intellij sometimes decides to continue the // comment on the next line. How can I prevent that? Is there a setting somewhere to disable this automation?
Long version
There is a thing I do regularily, it is to break a long expression with a double-slash.
Let's say I have a line like
boolean isHex = c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
and I want to split it like that
boolean isHex = c >= '0' && c <= '9' //
|| c >= 'A' && c <= 'F' //
|| c >= 'a' && c <= 'f';
Note that I want the final // in order to prevent any formatter to join the lines again.
So I insert a double-slash-return after the '9', by pressing //<enter>. But Intellij will auto-continue the comment on the next line.
boolean isHex = c >= '0' && c <= '9' //
// || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
It forces me to uncomment and reindent the line manually.
I want Intellij to not continue the comment on the next line and optionally indent my code:
boolean isHex = c >= '0' && c <= '9' //
|| c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f';
So I want to disable this "continue // comment after <enter>" feature. Is it possible? I haven't found any setting related to that.
The closest you are going to get is to define a macro to insert a new line and remove the comment and then bind that macro to a suitable key.
Go to Settings → Code Style → Java → Wrapping and Braces and check "Line breaks" under "Keep when reformatting". This will make IntelliJ's formatter respect any manual line breaks, even if they contradict other formatting rules.

To check whether a character is of English Alphabet (a-zA-Z)

The method Character.isLetter(Char c) tells whether the character is a unicode letter. What if I want to check for English letters (a-zA-Z) without regex.
Easy
char c = ...;
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
//english letter
}

Backspace Ascii value vs integer?

So far I have a text field that accepts only numbers, backspace, delete, decimal and hyphens. The block of code with is:
if ( ((keyChar > '0') && (keyChar < '9')) ||
(keyChar == '.') || (keyChar == '-') ||
(keyChar == 8 ) || (keyChar == 127) )
This works however when I leave out the "(keyChar == 8 ) ||" and use
if ( ((keyChar > '0') && (keyChar < '9')) ||
(keyChar == '.') || (keyChar == '-') ||
(keyChar == 127) )
The backspace key does not work even though it is between 0 and 9?
The backspace key does not work even though it is between 0 and 9?
The backspace key does not generate a character, its value is undefined. A character is something that can actually be displayed in a text component.
Assuming this is Swing then don't attempt to solve this by using a KeyListener. Swing has newer and better API's:
See Using a Formatted Text Field for a component will built in support for this
See Implementing a DocumentFilter for implementing you own custom editing of valid characters.

Boolean value not working, need a fix?

What I want is to output A or Z.
Everything else between A and Z works except A and Z, I tried to add an "else if"
with equals and && but it's not working.
The "System.out.println(ConvertLetterToDigit(num2));"
takes the input and transfers it into a switch that'll give back an "int" value.
If you need more infos, please comment below ._.
char num1;
char num2;
String num3;
String equality;
System.out.println("\nThis game consists of inputting a digit and we will output the character linked to that digit.");
do
{
System.out.println("Please input a letter.");
equality= "^[a-zA-Z](1)";
num3 = console.next();
if (num3.length() < 2);
{
if (num3.matches(equality))
{
num2 = (char)num3.charAt(0);
System.out.println(ConvertLetterToDigit(num2));
}
else if (num3.length() > 1)
{
System.out.println("We said 1 letter.");
}
else
{
System.out.println("What do you think you're doing.");
}
}
} while (!num3.matches(equality));
break;
If I understand correctly, you want to check that your input is indeed a letter between a and z.
In that case, your if should be:
if( num2 >= 'a' && num2 <= 'z') {
// okay, do something
}
else {
// show error message
}
The condition you've written is always false: you require num2 to be equal to 'a' AND to 'z'. You should probably rewrite it as num2 >= 'a' && num2 <= 'z' (note the less/greater signs change).
Excluding 'a' and 'z' is as simple as changing >= to >, and <= to <.
&& means AND, || means OR. Use them appropriately. (A condition that matches both 'a' and 'z' would be num2 == 'a' || num2 == 'z'.)

Categories

Resources