Checking character properties in Java - java

Alright, I'm coding this program that will discard any characters that are not letters. And right now I am having trouble trying to have the program identify which is which. Here's some of the code I did.
System.out.println("Press enter every time, you type a new word, and press the period button to end it.");
Scanner question = new Scanner(System.in);
System.out.println("Press enter to continue, or tupe something random in");
String userInput = question.next();
while(!userInput.equals(".")){
String userInput2 = question.next();
System.out.println(userInput2);
if(userInput2.equals("Stop")){
break;
}
}

You can use a regular expression to remove all characters which are not either lowercase or uppercase letters:
String userInput2 = question.next();
userInput2 = userInput2.replaceAll("[^a-zA-Z]", "");
System.out.println(userInput2);

Go through the string and call Character.isLetter(char) for each char to test if it is a letter character.

Related

How to replace character in string without using the replace function in Java?

So I am asking for a string to be inputted, then I am asking what the user wants to do to the string, in this case, I am trying to replace the first occurrence of a character. I can ask for what wants to be replaced and what to replace it with. I also know how to do the replacement somewhat, but say if the string has 2 of the same characters, I get a new String for both
(Change i to o: (input)"Find Dime" --->(output) "Fond Dime" & "Find Dome")
I only need the first one.
else if (UserCommand.equalsIgnoreCase("replace first"))
{
System.out.println("Enter the character to replace");
String Replace = input.next();
System.out.println("Enter the new character");
String Replacement = input.next();
for (int i = 0; i<String.length();i++)
{
if (String.charAt(i)==Replace.charAt(0))
{
StringEdit = String.substring(0,i) + Replacement + String.substring(i + Replace.length());
}
}
Output:
Enter the string
lump sum
Enter the character to replace
u
Enter the new character
o
lomp sumlump som
if you only need the first String use
break;
in the if statement

Prompt the user to enter a password which matches a specific pattern

I have written some code but I cant seem to figure out how to get the exact number of characters, numbers, or symbols. I fixed my code a bit but it does not work and I'm not sure why.
My requirements are
Write a Java program that prompts the user to enter a password that matches a specific pattern. Your program must approve the user's entry.. Here is the pattern, in this order:
-1 or more upper case letters
-two lower case letters
-1 or 2 digits
-zero or 1 upper case letters
-any two of this group ##$%^&
My Code:
import java.util.Scanner;
public class TestingCenter {
public static void main(String[] args) {
int digit=0;
int special=0;
int upCount=0;
int upCount2=0;
int loCount=0;
String password;
Scanner scan = new Scanner(System.in);
System.out.println(" Enter Your Password:");
password = scan.nextLine();
for(int i =0;i<password.length();i++){
char c = password.charAt(i);
if(Character.isUpperCase(c)){
upCount++;
}
if(Character.isLowerCase(c)){
loCount++;
}
if(Character.isDigit(c)){
digit++;
}
if(Character.isUpperCase(c)){
upCount2++;
}
if(c>=33&&c<=46||c==64){
special++;
}
}
if(special==2&&loCount==2&&upCount>=1&&(digit==1||digit==2)&&upCount2<=1){
System.out.println(" Password is good:");
}
}
}
If I understand correctly, in this order literally means in that given order.
If that's the case, you want regex. Forget counting characters.
1 or more upper case letters [A-Z]+
two lower case letters [a-z]{2}
1 or 2 digits \d{1,2}
zero or 1 upper case letters [A-Z]?
any two of this group ##$%^& [##$%^&]{2}
So,
Scanner scan = new Scanner(System.in);
System.out.println(" Enter Your Password:");
String password = scan.nextLine();
System.out.println(password.matches("[A-Z]+[a-z]{2}\\d{1,2}[A-Z]?[##$%^&]{2}");
If that's not what your instructions meant, then check out your conditionals. Clearly not everything should be >= 1

Counting a String's Lowercase Letters

I was looking around forums and found a helpful code on how to count lowercase letters in an inputted string. Thing is, after testing it, I saw it only counts lowercase letters within the first word typed. So, for example, if I type: HeRE the counter will say I've typed in 1 lowercase letter (which is correct), but if I type in: HeRE i am the counter will still only say 1 instead of 4. It's only counting the lowercase letters in the first word. How do I get it to count lowercase letters in my entire string?
Code thus far:
import java.util.Scanner;
public class countingLowerCaseStrings {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your string: ");
String input = scanner.next();
int LowerCaseLetterCounter = 0;
for (char ch : input.toCharArray()) {
if (Character.isLowerCase(ch)) {
LowerCaseLetterCounter++;
}
}
System.out.println ("Number of lower case letters in this string is: " +
LowerCaseLetterCounter);
}
}
Thanks a bunch for the help!
scanner.next(); reads the first available word, not the entire line.
So if you input "HeRE i am" it will just read "HeRE".
Change it to scanner.nextLine():
System.out.println("Enter your string: ");
String input = scanner.nextLine();
DEMO - look at stdin and stdout panels.
As a matter of interest, Java 8 provides a fairly streamlined way of achieving the same thing:
scanner.nextLine().chars().filter(Character::isLowerCase).count()

Getting vowels from word phrase in java

Hi guys I'm trying to program a pig latin translator and I'm stuck in trying to prompt a user to enter the first vowel in the phrase that they entered. I feel like I'm not doing it properly.
When a user enters "white color"
I want the next two prompt to ask for the first vowel in the words. Then translate it to pig latin.
Any advice on how to get started? Thank you!
Here's my code:
import java.util.Scanner;
public class main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a 2 word phrase: ");
String input = sc.nextLine();
System.out.println("Please enter the first vowel in the first word: ");
String input1 = sc.nextLine();
System.out.println("Please enter the first vowel in the second word: ");
String input2 = sc.nextLine();
}
7 ways of generating pig latin are mentioned on Wikipedia.
Link : https://en.wikipedia.org/wiki/Pig_Latin.
So you don't need to ask the user for the vowels, you are the one whose supposed to identify the vowel. Choose one of the six ways, write a prototype and then come back if you are stuck.
Ps: I will recommend that you go with last way mentioned in the wiki as it is easier to implement.

Java: applying 2 expressions to 1 scanner input

I am trying to apply 2 conditions: read a character at a time and require alpha character input for the same scanner input:
Any suggestions on how to apply these conditions and continue checking each character user inputs?
Scanner lastname = new Scanner(System.in);
System.out.println("Please enter the first letter of last name:");
lastname.useDelimiter("(?<=.)");
if (lastname.hasNext())
System.out.println("character: [" + lastname.next() + "]");
if (!lastname.hasNext("[A-Za-z]+")); {
System.out.println("You are not a robot so do not use numeric characters.");
System.out.println("Please enter letter:");
lastname.next();
Don't change the delimiter; change your logic.
Always just read one character, but test it using code like this:
String s = scanner.next();
if (Character.isDigit(s.charAt(0)))
or using regex:
if (s.matches("[a-zA-Z]+"))

Categories