Check if string contains number after specific word [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
can someone help me with string validation? I tried to find the solution, but none was satisfied.
I have the uri e.g. /dog/cat/house,1/mouse/bird,1/rabbit.
I need to check if after word (with comma) bird, there is a number or not. In my case sometimes i receive uri with number: "bird,1" and sometimes with word: "bird,foo".
Thank you for any suggestions.

As #Federico klez Culloca and #The fourth bird suggested you could use a regular expression (\\bbird,(?:[1-9]|1[0-9]|20)\\b) but some security scans don't like regular expressions. In any case, one another (pure Java) solution would be:
Updated the answer after user added more conditions.
would look for range 1, 2 .. 20 (01, 02 would return false).
public static boolean isNumber() {
// you can parametrize these 2
String input = "/dog/cat/house,1/mouse/bird,10/rabbit.";
String strOfInterest = "/bird,";
boolean isStringEndingInLT20 = false;
int indxOfInterest = input.indexOf("/bird,") + strOfInterest.length();
char c1 = input.charAt(indxOfInterest);
char c2 = input.charAt(indxOfInterest + 1);
int i1 = Character.getNumericValue(input.charAt(indxOfInterest));
if (Character.isDigit(c1) && Character.isDigit(c2)) {
int num = Integer.parseInt("" + c1 + c2);
if ((i1 > 0) && (num >= 1) && (i1 <= 20)) isStringEndingInLT20 = true;
} else if (Character.isDigit(c1)) {
if ((i1 >= 1) && (i1 <= 9)) isStringEndingInLT20 = true;
}
return isStringEndingInLT20;
}
NOTE: I personally hate these verbose solutions and would prefer 1 line REGEX. Try to avoid it and use regex. The only times I avoid regex is when it becomes performance bottleneck and/or causes a security concern.

This is a practical algorithm, you can specify the keyword!
The premise is that the validity of the contains parameter is in line with your description.
keyword, (spaces are allowed)123/
public static void main(String[] args) throws IOException {
String contains = "/dog/cat/house,1/mouse/bird,a/rabbit";
FreeTest f = new FreeTest();
boolean has = f.hasNumber(contains, "bird");
System.out.println(has);
}
/**
* Check if string contains number after specific word
*
* #param contains string contains
* #param key the specific word (without comma)
* #return yes or not
*/
public boolean hasNumber(String contains, String key) {
int commaIndex = contains.indexOf(',', contains.indexOf(key));
int startIndex = commaIndex + 1;
boolean hasNumber = true;
while (true) {
char c = contains.charAt(startIndex++);
if (c == '/') break; // exit
if (c != ' ') {
hasNumber = Character.isDigit(c);
}
}
return hasNumber;
}

Related

Expansion of Molecular notation of Chemical Formula [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I looked around a bit for a solution to parsing out a chemical formula that contained molecular components that may have their own suffixes for the purposes of being able to parse a formula for its complete atomic fraction.
How would one do this in Java?
Not having been able to find a way to do this in short order (and not having done a fun algorithm in a while) I settled on a stack implementation, as it's actually less complicated than a math operation stack.
Going backward through the stack, you only have to be aware of a few things, as this is fundamentally a modified implementation of a common solution to parsing a mathematical statement.
1) Your integer suffix can be built of multiple characters
2) Your integer suffix could be a multiplier (next character being a ")")
3) You need to handle for implicit "1"s
The implementation pops characters off one stack and pushes only letters and numbers onto your "return stack".
String expandFormula(String s){
Stack<Character> stack = new Stack();
Stack<Character> hanoi = new Stack();
char[] ca = s.toCharArray();
Character c;
List<Integer> multipliers = new ArrayList();
String multiBuff;
int val;
boolean flag;
for (int i = 0; i < ca.length; i++)
stack.push(ca[i]);
while(!stack.isEmpty()){
c = stack.pop();
if (Character.isLetter(c)){
try{
//previous parse was end of Symbol, implicit "1"
flag = Character.isUpperCase(hanoi.peek());
}
catch(EmptyStackException ese){ //change exception
flag = false;
}
//push implicit 1
if (flag){
stack.push(c);
stack.push('1');
}
//continue as usual
else
hanoi.push(c);
}
//begin number parsing
else if(Character.isDigit(c)){
flag = false;
multiBuff = c +"";
//parse the integer out
while(Character.isDigit(stack.peek())){
c = stack.pop();
multiBuff = c + multiBuff;
}
//if next char is ), then value is a suffix
if (stack.peek() == ')'){
flag = true;
stack.pop();
multipliers.add(Integer.parseInt(multiBuff));
//pop successive )s
while(stack.peek() == ')'){
stack.pop();
multipliers.add(1);
}
}
if(Character.isLetter(stack.peek())){
val = flag ? 0 : Integer.parseInt(multiBuff);
//get full value of
for(Integer i : multipliers){
if (val == 0)
val = i;
else
val *= i;
}
//trim and push first decibit
while(val > 0){
hanoi.push(Character.forDigit(val % 10, 10));
val /= 10;
}
}
}
//end of nest, remove most recent multiplier
else if(c == '(')
try{
multipliers.remove(multipliers.size()-1);
}
catch(ArrayIndexOutOfBoundsException aioobe){
}
}
multiBuff = "";
while(!hanoi.isEmpty())
multiBuff += hanoi.pop();
return multibuff;
}
This solution can be converted directly to your output string by:
1) Change "hanoi" to string
2) Change "hanoi.push(c)" to hanoi = c + hanoi
3) Change "hanoi.peek()" to "hanoi.charAt(0)"
4) Change Exceptions as necessary (or use general exceptions anyway)
5) Just return hanoi instead of the multibuff thing at the bottom.

Returning a string minus a specific character between specific characters

I am going through the Java CodeBat exercises. Here is the one I am stuck on:
Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".
Here is my code:
public String zipZap(String str){
String s = ""; //Initialising return string
String diff = " " + str + " "; //Ensuring no out of bounds exceptions occur
for (int i = 1; i < diff.length()-1; i++) {
if (diff.charAt(i-1) != 'z' &&
diff.charAt(i+1) != 'p') {
s += diff.charAt(i);
}
}
return s;
}
This is successful for a few of them but not for others. It seems like the && operator is acting like a || for some of the example strings; that is to say, many of the characters I want to keep are not being kept. I'm not sure how I would go about fixing it.
A nudge in the right direction if you please! I just need a hint!
Actually it is the other way around. You should do:
if (diff.charAt(i-1) != 'z' || diff.charAt(i+1) != 'p') {
s += diff.charAt(i);
}
Which is equivalent to:
if (!(diff.charAt(i-1) == 'z' && diff.charAt(i+1) == 'p')) {
s += diff.charAt(i);
}
This sounds like the perfect use of a regular expression.
The regex "z.p" will match any three letter token starting with a z, having any character in the middle, and ending in p. If you require it to be a letter you could use "z[a-zA-Z]p" instead.
So you end up with
public String zipZap(String str) {
return str.replaceAll("z[a-zA-Z]p", "zp");
}
This passes all the tests, by the way.
You could make the argument that this question is about raw string manipulation, but I would argue that that makes this an even better lesson: applying regexes appropriately is a massively useful skill to have!
public String zipZap(String str) {
//If bigger than 3, because obviously without 3 variables we just return the string.
if (str.length() >= 3)
{
//Create a variable to return at the end.
String ret = "";
//This is a cheat I worked on to get the ending to work easier.
//I noticed that it wouldn't add at the end, so I fixed it using this cheat.
int minusAmt = 2;
//The minus amount starts with 2, but can be changed to 0 when there is no instance of z-p.
for (int i = 0; i < str.length() - minusAmt; i++)
{
//I thought this was a genius solution, so I suprised myself.
if (str.charAt(i) == 'z' && str.charAt(i+2) == 'p')
{
//Add "zp" to the return string
ret = ret + "zp";
//As long as z-p occurs, we keep the minus amount at 2.
minusAmt = 2;
//Increment to skip over z-p.
i += 2;
}
//If it isn't z-p, we do this.
else
{
//Add the character
ret = ret + str.charAt(i);
//Make the minus amount 0, so that we can get the rest of the chars.
minusAmt = 0;
}
}
//return the string.
return ret;
}
//If it was less than 3 chars, we return the string.
else
{
return str;
}
}

Creating a boolean method to check for consecutive numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need help writing a method that checks if numbers and text are consecutive. It needs to return a boolean value of true if an input like inputted deFgh or 456789 and false for anything else not consecutive. I don't understand how to make the loop be true for cases like xyZaBcD and 890123 or cbazyx
try this code:
public static boolean isConsecutive(final String s) throws IllegalArgumentException
{
if (null == s) throw new IllegalArgumentException();
if (s.length() <= 1) return true;
final String lc = s.toLowerCase();
char c = lc.charAt(0);
for (int cc=1; cc<lc.length(); cc++)
if ( (c+1) != lc.charAt(cc) )
return false;
else
c++;
return true;
}
public static void main(String[] args)
{
try
{
System.out.println(isConsecutive("456789"));
System.out.println(isConsecutive("deFgh"));
System.out.println(isConsecutive("xyZaBcD"));
System.out.println(isConsecutive("890123"));
}
catch(final Exception e)
{
e.printStackTrace();
}
}
but I really suggest you do not show it to teacher, as it will have more questions, use it only as direction to your own code
This can be implemented at easiest way:
public class Check {
private static boolean checkConsecutive(String str) {
str = str.toLowerCase();
if (str.length() == 1) return true;
for (int i = 1; i < str.length(); i++) {
String first = str.substring(i, i+1);
String beforeFirst = str.substring(i-1, i);
if (beforeFirst.compareTo(first) > 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Check obj = new Check();
System.out.printf("abcdef is: %s%n", obj.checkConsecutive("abcdef"));
System.out.printf("12345 is: %s%n", obj.checkConsecutive("12345"));
System.out.printf("54321 is: %s%n", obj.checkConsecutive("54321"));
System.out.printf("fedcba is: %s%n", obj.checkConsecutive("fedcba"));
}
}
Output will be next:
abcdef is: true
12345 is: true
54321 is: false
fedcba is: false
This line str.substring(i, i+1) return exactly one letter, and we can use compareTo() from String class it compares consecutive by itself.
Just iterate over string and check sequence of char codes. If needed, use toLowerCase() method.
You can cast (int) to the characters in the loop. If the integer is between 48 and 57 inclusive, that means that the character is a digit.
See ASCII Table for the integers given by casting from char.

splitting strings with operator relative to parentheses [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want the following strings to be split by the (relative to the pairs of parentheses) outer-most operator (in this case: '+'):
1: "((20 + 20) + a)"
2: "(20 + ((20 + 20) + 20))
The results should be like this:
1: "((20 + 20) " and " a)"
2: "(20 " and " ((20 + 20) + 20))"
You can't do this with regex, but you can try something like this:
// locations of top-level operators:
List<Integer> locations = new ArrayList<Integer>();
int level = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '(') {
level++;
}
else if (c == ')') {
level--;
}
else if ("+-*/".indexOf(c) >= 0 && level == 1) {
locations.add(i);
}
}
You can then "split" your string with substring() and whatever is in locations.
If you always want to split on the outermost operator (e.g. split on + in (((a + 1)))), then things get a little more tricky, but your overall approach doesn't have to change drastically. One idea that comes to mind is to build a Map<OperatorData, Integer> (where OperatorData is a class containing the operator token (e.g. +) and an int representing how far it's nested) that maps to locations in the string. OperatorData can be Comparable based on the nest level.
OperatorData might look something like this:
class OperatorData implements Comparable<OperatorData> {
private String token;
private int level;
// constructor etc.
#Override
public int compareTo(OperatorData other) {
return Integer.compare(level, other.level);
}
}
You can then go through this map and split on the operators that have the lowest nest level. The approach might look something like this:
// location of top-level operators:
Map<OperatorData, Integer> operators = new HashMap<>();
int level = 0;
int i = 0;
while (i < str.length()) {
char c = str.charAt(i);
if (c == '(') {
level++;
} else if (c == ')') {
level--;
} else if (isOperatorChar(c)) {
final int index = i;
StringBuilder token = new StringBuilder();
token.append(c);
while (isOperatorChar(c = str.charAt(i + 1))) {
token.append(c);
i++;
}
operators.put(new OperatorData(token.toString(), level), index);
}
i++;
}
// find smallest OperatorData in map

Password validation - adding additional requirments

I have some code that currently checks for minimum and maximum lentgh. I want to also require uppercase, lowercase, special char, and numeric. Any suggestions on what to add to this or where I can find some examples? I've been Googling and looking thru this forum and have been trying to add the additional password requirments and have been unsuccessful.
This is what I want to require.
At least eight characters in length
No more than 20 characters in length
at least lower-case letter and one upper-case
at least one special character from: !##$%^&*()~`-=_+[]{}|:";',./<>?
at least one number [0-9] character
Cannot match the account login name or email address
My current password validation code
public static final int MIN_PASSWORD_LENGTH = 8;
public static final int MAX_PASSWORD_LENGTH = 20;
public static boolean isAcceptablePassword(String password)
{
if(TextUtils.isEmpty(password))
return false;
int len = password.length();
if(len < MIN_PASSWORD_LENGTH || len > MAX_PASSWORD_LENGTH)
return false;
for(int i = 0; i < len; i++)
{
char c = password.charAt(i);
if (Character.isWhitespace(c))
return false;
}
return true;
}
When you're analyzing String data, you should erase the whitespaces on the right and left. This is done by the Strimg#trim function like this:
password = password.trim();
To analize every character of the String, you can transform it to a char array, so it will be easier to fulfill your requirements:
char[] arrPassword = password.toCharArray();
Now you can evaluate a char using these functions: Character#isUpperCase, Character#isLowerCase, Character#isDigit.
Last but not least, you can have a String with the special characters you need to check, and check if the actual character you're evaluating is inside that String. This could be achieved using String#indexOf and String#valueOf, this las one to convert the char to a String type.
Here is a code sample for all this explanation:
public static final String SPECIAL_CHARACTERS = "!##$%^&*()~`-=_+[]{}|:\";',./<>?";
public static final int MIN_PASSWORD_LENGTH = 8;
public static final int MAX_PASSWORD_LENGTH = 20;
public static boolean isAcceptablePassword(String password) {
if (TextUtils.isEmpty(password)) {
System.out.println("empty string.");
return false;
}
password = password.trim();
int len = password.length();
if(len < MIN_PASSWORD_LENGTH || len > MAX_PASSWORD_LENGTH) {
System.out.println("wrong size, it must have at least 8 characters and less than 20.");
return false;
}
char[] aC = password.toCharArray();
for(char c : aC) {
if (Character.isUpperCase(c)) {
System.out.println(c + " is uppercase.");
} else
if (Character.isLowerCase(c)) {
System.out.println(c + " is lowercase.");
} else
if (Character.isDigit(c)) {
System.out.println(c + " is digit.");
} else
if (SPECIAL_CHARACTERS.indexOf(String.valueOf(c)) >= 0) {
System.out.println(c + " is valid symbol.");
} else {
System.out.println(c + " is an invalid character in the password.");
return false;
}
}
return true;
}
The System.out.println(c + " is an invalid character in the password."); sentence is just to check the result of analyze the actual character.
How about some good old regular expressions? This seems to work correctly, although might have made slip in the escaping for special char check
Pattern[] checks = {
Pattern.compile("[!##\\$%^&*()~`\\-=_+\\[\\]{}|:\\\";',\\./<>?]"),
Pattern.compile("\\d+"),
Pattern.compile("[A-Z]+"),
Pattern.compile("[a-z]+"),
Pattern.compile("^.{8,20}$") };
for (String test : new String[] { "password", "Password1",
"Password1&", "toolongtoolongtoolong" }) {
boolean ok = true;
for (Pattern check : checks) {
ok = ok && check.matcher(test).find();
}
System.out.println(test + " " + ok);
}
Stephen is right with a bit of searching you would have found your answers easily around here. But the thread Stephen refers to is using a thirdparty library.
If you want to implement this yourself then before starting the for-loop initialize 4 booleans for your requirements with false. While looping check for all four requirements until one is true. Set the corresponding boolean to true.
How to check the 4 requirements:
The length req you already implemented.
Character(yourChar).isLowerCase()
Character(yourChar).isUpperCase()
Special character: see here: Java String Special character replacement - you can choose a similar approach
After the loop check for the 4 booleans and react accordingly.

Categories