Capital and Lower case - java

Im writing a program that holds the alphabet on a string. Along with this, I'm adding scanner that allows the user to input a character. When the user inputs their character it tells them what letter of the alphabet it is by using the charAt(index)
. I've gotten all this to work. The only thing I'm trying to get is the display to tell the user if he/she entered an upper case or lower case letter. Any clues on how I can do this?

You can use Character.isLowerCase() and Character.isUpperCase() to check an individual char.

You can use Character.isUppercase(Character toCheck)

Java have two string manipulation methods called toLowerCase() and toUpperCase() that do exactly what they sound like.

Related

Check if user input has a letter or number at specific indexes

I have need to check if a user input follows the Canadian postal code format, which is
"L#L #L#"
Where L is an uppercase letter and # is any digit
How do would I do this?
Would I have to break it down and look at each letter?
I am very new to java, so I would appreciate some pointers.
If any letter/number is OK:
if (str.matches("[A-Z]\\d[A-Z] \\d[A-Z]\\d"))
// OK
If only certain combinations are valid, you'll need something more complicated, like storing valid values in a Set and asserting the entered values exist there.

STRING EXTRACTION/TRUNCATE from a given character in java

Hi i am doing a UI PROJECT in java. i would like to truncate a string or sequence of character from a given string for example:
if 000101 is the user input
the output of the string will be 101.
P.S: the occurrence of 0's before 1 may be anything.
please help me!!
I'm sure there are many ways to do this, and I encourage you to figure out a way on your own.
Here's one answer to this question:
Why not try to take the user input and keep cutting off the first character of the string until you reach a nonzero number?
For example, let's say you have a string called "userInput".
while(userInput.substring(0,1).equals("0"))
userInput = userInput.substring(1);

Replacing parts of a string with characters JAVA

I know I was here earlier asking something similar, but I think I have narrowed down what i want to ask.
Ok, so I am making a program that plays the game of hangman on the jedit console. The user will guess one character at a time. At the beginning of the game, the program will display asterisks the same length of the word they are guessing. They have as many guesses as letters in the word. When they get a letter correct, the program will display the letters in place of asterisks. Here is an example of what the console should look like.
if the word is homework ********
they guess the letter e ***e**** (the bold e just happened because stars so that, it doesn't need to be bold)
then they guess the letter h h**e****
etc until there are no more asterisks
So I created a method that prints out the number of asterisks based on the number of letters in the word. I don't know how to place the letters in the place of the asterisks. I want to know if I should make a method that replaces the asterisks, or how else I can go about this. Thank you in advance for the help.
p.s I am not asking for anyone to dump code on me, that is not what I want. Just having help, and me having someone to ask questions to about things that I don't understand would be nice. by the way, I am in an intro to computer science class, so my knowledge of java is fairly low.
There are many ways you could approach this. The first that popped into my head is that you could start with a char[] the same length as the answer string. Look up the Arrays class for an easy way to fill it with asterisks. As the user guesses letters, search the answer string for that letter and replace the corresponding indexes of the char[]. Then construct a String from the char[] and display it.
Why not make something more clever, make a list of all the chars guessed so far and each time you want to print the word just go over each letter and replace it with * if not in the set.
Short: make a set of all the guesses so far. You don't have to work on the same data structure as you show the user.
I would use a list of characters instead of String for ****.
List<Character> hiddenWord = new ArrayList<Character>();
Instantiate the list with the number of * you need.
Create a function that will receive the guessed letter.
Check if the word contains that letter (use indexOf(int ch, int fromIndex) repeatedly until you get -1 - read about it here), and for each result you get that is !=-1, set the position in the array to be that letter (something like hiddenWord.set(poz, letter), where poz is the result of indexOf and letter is the guessed letter).
You can use StringBuffer insead String. In class StringBuffer exists method setCharAt.
Breifly, you will have variable String word - for guessing word, and StringBuilder guess for asterisks and guesed letters. When letter is guessed you will update guess with setCharAt.

How to make a scanner in Java that checks if the first letter is a character between A-V and if the second character is a number between 1-20?

How to make a scanner that checks if the first letter is a character between A-V and if the second character is a number between 1-20? Some examples are: '.B4', 'H10.', '**V1', 'L19*', 'M12', or 'N14'.
I'm kind a new to Java. Still learning it in school. I've followed the lessons for about half a year now.
Now I've got an assignment for school. It is about creating a text-based minesweeper. I succeeded in printing the board and placing the mines. But now I'm stuck on getting the right input.
If you use '*' in the scanner like * B4 or B4* it should mark a square.
If you use '.' in the scanner like .B4 or B4. it should unmark a square.
And if you enter B4 it should open.
But I can't get this done in a neat way. I've tried to make sub-strings of it to check if every character is the right one but after I did that my code was kind of chaotic and it didn't work as supposed to.
I've tried it like: "Example 3 : Validating vowels in: Validating input using java.util.Scanner" only I used a variable of the length of my board. So if the board was 10 by 10 it wouldn't go further than J10. But that didn't work either for me.
So I was hoping that you could help me solving this problem.
As this is an assignment, I'll just give you a guideline rather than actual code.
First, you need to get the input into some format. Consider reading the input in from the scanner and storing it into a string.
We can then make use of Java's String functions, a list of which can be found here. Try to find a function that could be useful, perhaps one that lets us get the character at a certain index.
We can then do checks on the string. First we check the first character (the character at index 0), we want to know if that is a letter from A-V. To do this we can do a check on the ASCII numbers. Assuming you just want capital letters, if we convert A to an int, then it will have the value 65. V has the value 86. All the numbers in between correspond to the ASCII values of the letters in between.
Thus we can do a check, convert the first character to an integer, let's call it x. If x >= 65 && x <= 86, then it's a letter we can care about.
Next, you need to do the number checking. For this, take a look at the function Integer.parseInt(String s). It takes a String and then converts it to an integer. You'll have to do some checks to see if it's >= 10 or <10.

Using Java, how do I accept and categorize a phrase that has numbers and letters in user input?

I'm writing an application that I'd like to be able to accept and hold the user's input of a combination of numbers and letters (06712A1, for instance), and then input that info into an array. I assume I can't use Integer since there are letters inside it. How do I do this?
Use a String and then validate it by examining all the letters it contains.
What do you mean by "input that info into an array"? Put each character into a separate position in an array?
You need to use the String datatype to represent a sequence of characters.
This tutorial on Strings in java might be useful.
Your question is ambiguous.
I assume you mean that you want to use that String as an index to another object. In that case, search for the java.util.Map subclass that suits you.

Categories