Ok so I'm trying to write a program that prints certain messages when the input contains a certain word in a certain case.
For example, if the word is orange, the program would detect that the word orange is in the input, and then say something depending on whether it exists, is lower case, or is upper case. As far as all that goes, I'm good, the only thing I can't figure out is how to make it detect the word when it's in mixed case, like oRange, or oranGe.
I think this would do it:
if (!yourString.toLowerCase().equals(yourString) && !yourString.toUpperCase().equals(yourString)) {
mixed = true
}
You can utilize a method to see if a particular string is mixed case, for example:
public static boolean isMixedCase (final String inputString) {
if (inputString == null || inputString.isEmpty()) {
return false;
}
Character c;
boolean hasUpper = false;
boolean hasLower = false;
for (int i = 0; i < inputString.length(); i++) {
c = inputString.charAt(i);
if (Character.isUpperCase(c)) {
hasUpper = true;
}
else if (Character.isLowerCase(c)) {
hasLower = true;
}
}
return hasLower && hasUpper;
}
From your question, it is not entirely clear what your exact requirement is.
If you want to detect whether a string matches another, regardless of case, you could just use String::equalsIgnoreCase:
return input.equalsIgnoreCase("orange");
Otherwise, I assume a string in mixed case is when it contains at least one uppercase and one lowercase character.
Well, there are many options:
Character::toUpperCase and Character::toLowerCase
var lower = false;
var upper = false;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isUpperCase(c)) {
upper = true;
}
else if (Character.isLowerCase(c)) {
lower = true;
}
if (lower && upper) {
return true;
}
}
return false;
String::toUpperCase and String::toLowerCase
return !(input.equals(str.toLowerCase()) || input.equals(str.toUpperCase()));
Pattern::matches
boolean upper = Pattern.compile("[A-Z]").matcher(input).find();
boolean lower = Pattern.compile("[a-z]").matcher(input).find();
return upper && lower;
In the abovementioned code, I assumed the string to contain only ASCII characters. If you want to support Unicode as well, then you may want to use Character.isUpperCase(int) or Character.isLowerCase(int).
Use this method to check all the cases: identical, lower-case, upper-case, mixed-case.
public class TestCase
{
public static void main(String[] args) {
String str = "orange";
String strInput = "oRange";
System.out.println(testCase(str, strInput));
}
public static int testCase(String str, String strInput) {
// identical
if(str.equals(strInput)) {
return 1;
}
// all lower-case
if(str.toLowerCase().equals(strInput)) {
return 2;
}
// all upper-case
if(str.toUpperCase().equals(strInput)) {
return 3;
}
// mixed-case
if(str.equalsIgnoreCase(strInput)) {
return 4;
}
// not present
return 0;
}
}
For example, to check if a string is mixed-case:
if(testCase(str, strInput) == 4) {
// mixed-case
}
Since testCase is static, you can also use it outside of this class.
Related
I am trying to run a recursion function where i check each character in the string to see if the character is a numeric number, however im a little stuck as the function runs and only checks the first character of the string
public static boolean isNumeric(String str) {
if(str == null)
{
return false;
}
if(str=="") {
return true;
}
char first = str.charAt(0);
if(Character.isDigit(first))
{
return true;
}
if(Character.isDigit(first) == false)
{
return false;
}
String reduced = str.substring(1);
return isNumeric((reduced)); }
Not 100% sure if I understood you correctly. If you want to check, if the string contains numeric values just see my other examples further down.
Otherwise, if you want to check, if the string contains exclusively numerical signs and digits you could do something like:
public static boolean isInteger(String numericValue) {
if (numericValue == null) return false;
try {
Integer.parseInt(numericValue);
return true;
} catch (NumberFormatException e) {
return false;
}
}
Please note, that this example does only work for Integers. If you
want to use bigger numbers, we can use Long or even BigDecimal
instead. But the main idea stays the same - check, if it can be parsed.
Or probably more appealing:
public static void main(String args[]) {
System.out.println(MyClass.isNumeric(""));
System.out.println(MyClass.isNumeric(null));
System.out.println(MyClass.isNumeric("abc"));
System.out.println(MyClass.isNumeric("a2b"));
System.out.println(MyClass.isNumeric("1a2b3"));
System.out.println(MyClass.isNumeric("42"));
System.out.println(MyClass.isNumeric("-100"));
}
public static boolean isNumeric(String str) {
if (str == null) return false; // handle null-pointer
if (str.length() == 0) return false; // handle empty strings
// to make sure that the input is numeric, we have to go through the characters
for (char c : str.toCharArray()) {
if (!Character.isDigit(c)) return false; // we found a non-digit character so we can early return
}
return true;
}
This will only print true for 42. The other examples contain other characters, too.
There are multiple issues with your code:
if(str=="") {
return true;
}
For string comparison you should use the String.equals() method - there are alternatives to that for example for your use-case you could use the convenience method String.isEmpty() or you could also just check the length of the string.
if(Character.isDigit(first))
{
return true;
}
if(Character.isDigit(first) == false)
{
return false;
}
This is some kind of if-else structure here. No need to check the same condition twice. Besides, it won't work with your recursive approach, because for what you want to achieve you need the current state which is "have I already found a digit". You could solve that with a memoized recursive function where you pass the current state along as a second argument to each subsequent call.
In your case it will return the result of the latest iteration / the
latest character.
You could do something like the following instead of using recursion. Recursion is fine, but it also comes with some costs. For example is it arguably harder to read and maintain.
public static void main() { ... } // same as above
public static boolean isNumeric(String str) {
if (str == null) return false; // handle null-pointer
if (str.length() == 0) return false; // handle empty strings
boolean atLeastOneDigit = false;
// to make sure that the input is numeric, we have to go through the characters
// until we find the first one then we can early return
for (char c : str.toCharArray()) {
if (Character.isDigit(c)) {
atLeastOneDigit = true; // we found a digit, therefore it must be numeric
break; // early return to save some iterations
}
}
return atLeastOneDigit;
}
The output of the program is:
false
false
false
true
true
true
true
Another alternative is to use Regex:
private static final Pattern digitRegex = Pattern.compile("\\d+");
public static main() { ... } // same as above
public static boolean isNumeric(String str) {
if (str == null) return false;
Matcher matcher = MyClass.digitRegex.matcher(str);
return matcher.find();
}
I am trying to explain you to solve this problem by two string methods that is isDigit(character) and charAt(i). In this case we will try to solve this problem by for loop. we will apply for loop in order to check every character of a string whether it is digit or not. If it is digit we will return true otherwise false.
Code is looks like this :
import java.lang.*;
import java.util.Scanner;
class Main {
public boolean containsDigit(String str) {
byte flag = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
flag = 1;
}
}
if (flag == 1) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string : ");
String str = sc.nextLine();
Main test = new Main();
boolean chkDigit = test.containsDigit(str);
if (chkDigit == true) {
System.out.println("String contains digit.");
} else {
System.out.println("String doesn't contain any digit.");
}
}
}
Example 1:
Input: S = "ab#c", T = "ad#c"
Output: true
Explanation: Both S and T become "ac".
Example 2:
Input: S = "ab##", T = "c#d#"
Output: true
Explanation: Both S and T become "".
Example 3:
Input: S = "a##c", T = "#a#c"
Output: true
Explanation: Both S and T become "c".
Example 4:
Input: S = "a#c", T = "b"
Output: false
Explanation: S becomes "c" while T becomes "b".
class Solution {
public boolean backspaceCompare(String S, String T) {
Stack<Character> stack1 = new Stack<Character>();
Stack<Character> stack2 = new Stack<Character>();
for(int i=0;i<S.length();i++){
if(S.charAt(i)!='#'){
stack1.push(S.charAt(i));
}else{
stack1.pop();
}
}
for(int j =0;j<T.length();j++){
if(T.charAt(j)!='#'){
stack2.push(S.charAt(j));
}else
stack2.pop();
}
if(stack1==stack2)
return true;
return false;
}
}
my output is false and answer should be true why is this not working?
The first mistake is pushing all the characters on the stack outside of the if statement.
Also you should check if stack is empty before removing items from it.
Otherwise EmptyStackException is thrown.
// stack1.push(S.charAt(i)); <-- remove this line
if (S.charAt(i)!='#') {
stack1.push(S.charAt(i));
}else if (!stack1.isEmpty()) { // <-- add this check
stack1.pop();
}
The second mistake is you can't use == to compare the contents of two stacks, use .equals method instead:
if(stack1.equals(stack2))
Answer by Joni correctly addresses the errors in the code, however there are some other issues I'd like to address:
You should use a helper method to eliminate repeating the same code.
You should use Deque instead of Stack. The javadoc says so.
Instead of using Stack/Deque, I'd recommend using StringBuilder, to prevent having to box the char values.
Something like this:
public boolean backspaceCompare(String s, String t) {
return applyBackspace(s).equals(applyBackspace(t));
}
private static String applyBackspace(String s) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != '#')
buf.append(s.charAt(i));
else if (buf.length() != 0)
buf.setLength(buf.length() - 1);
}
return buf.toString();
}
Your idea works, but it's expensive and unnecessary to copy the strings into stacks. If you work backwards from the end, no extra storage is necessary:
//given the string length or a valid character position, return
//the position of the previous valid character, or -1 if none
public static int previousCharPos(String s, int pos)
{
int bs=0; // number of backspaces to match
while(pos>0) {
--pos;
if (s.charAt(pos)=='#') {
++bs;
} else if (bs <= 0) {
return pos;
} else {
--bs;
}
}
return -1;
}
public static boolean backspaceCompare(String S, String T)
{
int spos = previousCharPos(S,S.length());
int tpos = previousCharPos(T,T.length());
while(spos >= 0 && tpos >= 0) {
if (S.charAt(spos) != T.charAt(tpos)) {
return false;
}
spos = previousCharPos(S,spos);
tpos = previousCharPos(T,tpos);
}
return spos == tpos;
}
I'm trying to write a method that returns if the string is or isn't a valid password in CodeHS.
It needs to be at least eight characters long and can only have letters and digits.
In the grader, it passes every test except for passwordCheck("codingisawesome") and passwordCheck("QWERTYUIOP").
Here's what I have so far:
public boolean passwordCheck(String password)
{
if (password.length() < 8)
{
return false;
}
else
{
char c;
int count = 0;
for (int i = 0; i < password.length(); i++)
{
c = password.charAt(i);
if (!Character.isLetterOrDigit(c))
{
return false;
} else if (Character.isDigit(c))
{
count++;
}
}
if (count < 2)
{
return false;
}
}
return true;
}
If anyone can help, I'd appreciate it. Thanks.
Try an approach using patterns (this is simpler than looping):
public boolean passwordCheck(String password)
{
return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");
}
Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
Assuming your requirement is as stated
It needs to be at least eight characters long and can only have letters and digits
Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,
public boolean passwordCheck(String password) {
if (password != null && password.length() >= 8) {
for (char ch : password.toCharArray()) {
if (!Character.isLetterOrDigit(ch)) {
return false;
}
}
return true;
}
return false;
}
It's failing those tests because your code checks that the password must have at least 2 digits:-
if (count < 2)
{
return false;
}
And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.
I'm currently trying to make a password validator work with boolean method, since the teacher asked us to do so. This is driving me nuts. To be correct, the password need to have one uppercase, one lower case letter, at least 10 characters and one number. I'm aware that right now, my method returns entirely with the value false, but I'm wondering how I can break the code once I have one uppercase, or one lowercase.
Thanks a lot for your help!
public class AtLeast1UppercaseLowercaseNumber {
public static void main(String[] args){
String password = "H";
System.out.println(password);
if(isSecurePassword(password)){
System.out.println("Yay it works");}
else {
System.out.println("you suck");}
}
public static isSecurePassword(String password) {
int uppercase = 0, lowercase = 0, number = 0;
for(int i=0; i<password.length(); i++) {
for(char c ='A'; c <='Z'; c++) {
if(password.charAt(i) == c) {
uppercase++;
if( uppercase >= 1) {
for(char t = 'a'; t <='z'; t++) {
if(password.charAt(i) == t) {
lowercase++;
if(lowercase >= 1) {
}
}
}
for(int j = '0'; j <='9'; j++) {
if(password.charAt(i) == j) {
number++;
if( number >= 1) {
}
}
}
}
return false;
}
}
I suggest you start by creating multiple private and static test methods, and then delegate to them in your public isSecurePassword(String) method. Implement test methods like boolean oneUpper(String), boolean oneLower(String), boolean oneDigit(String) and boolean tenCharacters(String) as an example
private static boolean tenCharacters(String str) {
return str.length() > 9;
}
and a second example
private static boolean oneUpper(String str) {
for (char ch : str.toCharArray()) {
if (Character.isUpperCase(ch)) {
return true;
}
}
return false;
}
Then your isSecurePassword(String) is just
public static boolean isSecurePassword(String str) {
return tenCharacters(str) && oneUpper(str) && oneLower(str)
&& oneDigit(str);
}
Since there is only one return in this method, which explicitly returns false, this method will always return false.
Method 1:
Define a boolean, which will be returned in the last statement of the method. This boolean is true by default and will be set false if one condition is wrong.
Method 2:
The last statement is an implicit return true statement, and whenever a condition is not fullfilled return false. This will prevent the method from executing more tests.
Method 3:
Make the method look like this
if (containsUpperCase(string) && contains...)
return true;
return false;
Edit: This is probably very bad code in the PasswordVerifier.Java
I'm doing a password verifier that checks if the entered password is at least 6 characters long, has an Uppercase letter, lowercase letter, and a digit.
I think I have the logistics of it somewhat correct, but for some reason my program isn't going to the next prompt. It asks me for my password and then hangs up and doesn't tell me whether my password is valid or not. I'm thinking that my for loops are correct so I don't know what my issue is.
PasswordVerifier.Java
import java.util.*;
public class PasswordVerifier{
//field
private static int MIN_PASSWORD_LENGTH = 6;
//methods
public static boolean isValid(String str){
boolean valid = false;
PasswordVerifier pass = new PasswordVerifier();
if(pass.hasUpperCase(str)|| pass.hasLowerCase(str) || pass.hasDigit(str)){
valid = true;
}
if (str.length() < 6){
valid = false;
}
return valid;
}
//UpperCase Boolean check
private boolean hasUpperCase(String str){
boolean valid = false;
int i = 0;
while (i < str.length()){
if (Character.isUpperCase(str.charAt(i)))
valid = true;
}
i++;
return valid;
}
//Lowercase Boolean Check
private boolean hasLowerCase(String str){
boolean valid = false;
int i = 0;
while (i < str.length()){
if (Character.isLowerCase(str.charAt(i)))
valid = true;
}
i++;
return valid;
}
//Number boolean check
private boolean hasDigit(String str){
boolean valid = false;
int i = 0;
while (i < str.length()){
if ((Character.isDigit(str.charAt(i))))
valid = true;
}
i++;
return valid;
}
}
PasswordDemo.Java
import javax.swing.JOptionPane;
public class PasswordDemo{
public static void main(String[] args){
String input; //To hold the user's input
input = JOptionPane.showInputDialog("Enter a Password");
if (PasswordVerifier.isValid(input)){
JOptionPane.showMessageDialog(null, "Valid Password");
}
else{
JOptionPane.showMessageDialog(null, "invalid Password, try again.");
}
}
}
Your while loops never increment i, because you placed i++ just past the end of the loop. The result is an infinite loop and the "hanging" you describe.
E.g. replace
while (i < str.length()){
if (Character.isUpperCase(str.charAt(i)))
{
valid = true;
// Added break because we found an uppercase letter already.
break;
}
i++; // Inside the while loop.
}
You'll need to make a similar change to each of your while loops.
Additionally, if you want to enforce all 3 provisions, don't use the logical-OR operator ||, use logical-and, &&:
if (pass.hasUpperCase(str) && pass.hasLowerCase(str) && pass.hasDigit(str)) {
valid = true;
}
That will make sure that the password has an uppercase letter and it has a lowercase letter and it has a digit.
I think your problem is that you are counting i++ outside the while loop.
Put it in the while like this:
private boolean hasUpperCase(String str){
boolean valid = false;
int i = 0;
while (i < str.length()){
if (Character.isUpperCase(str.charAt(i)))
valid = true;
i++;
}
return valid;
}
This is the problem in every while loop you got.
Right now you're making three passes through the password in order to verify the three character conditions - you can condense this down to one pass. In addition, you're checking the password's length last - you should check this first, as it's the fastest condition to verify.
public static boolean isValid(String str) {
if(str.length() < 6) return false;
int i = 0;
boolean hasDigit = false;
boolean hasLower = false;
boolean hasUpper = false;
while(i < str.length() && !hasDigit && !hasLower && !hasUpper) {
if(Character.isDigit(str.charAt(i))) {
hasDigit = true;
} else if(Character.isLowerCase(str.charAt(i))) {
hasLower = true;
} else if(Character.isUpperCase(str.charAt(i))) {
hasUpper = true;
}
i++;
}
return hasDigit && hasUpper && hasLower;
}
In your case the performance gain is probably negligible, but bear this in mind for the future when you may be dealing with more than three conditions and a string (or whatever) with a length much greater than six.