Password checker Java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Write a function to return if the string is a valid password.
It must be at least 8 characters long and may only consist
of letters and digits.
This is my code so far:
for (int i = 1; i < password.length() -1; i++)
{
char l = password.charAt(i);
if (password.length() < 8 && !Character.isLetter(l) || !Character.isDigit(l))
{
return false;
}
}
return true;

public static boolean isValid(String pw) {
return pw.matches("[a-zA-Z0-9]{8,}");
}

You can use regular expressions to do this
private static boolean isPasswordValid(String password) {
return password.matches("(\\w+){8,}");
}

Related

How do i fix it in Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Hello i am trying to write a method that checks whether a
string is a valid password. I Suppose the password rules are as
follows:
A password must have at least ten characters.
A password consists of only letters and digits.
A password must contain at least three digits.
I wrote the code but i see this error i don't know why.
package javaapplication6;
import java.util.Scanner;
import javafx.beans.binding.Bindings;
public class JavaApplication6 {
public static boolean isvalidPassword(String nume){
int count = 0;
for(int i=0; i<nume.length();i++){
if(Character.isDigit(nume.charAt(i))){
count++;
}
}
if (count<3){
return false;
}
if (nume.length()<10){
return false;
}
for (int i = 0; i < nume.length(); i++) {
if (!Character.isLetter(nume.charAt(i)charAt(i)) && !Character.isDigit(nume.charAt(i))){
return false; }
}
return true;}
}
If you look closely at the line
if (!Character.isLetter(nume.charAt(i)charAt(i)) && !Character.isDigit(nume.charAt(i))){
you see, that charAt(i) is duplicate:
nume.charAt(i)charAt(i)
Remove one of the method calls and you should be fine.

I want to change the string of letters xxx-xxx-xxxx into 333-333-3333 I tried this but getting an error in the if(phoneNumber.charAt(0)) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
public class ABCTelephoneTranslator {
public static String translate(String phoneNumber) {
// Creating array of string length
char[] ch = new char[phoneNumber.length()];
// Copy character by character into array
for (int i = 0; i < phoneNumber.length(); i++) {
ch[i] = phoneNumber.charAt(i);
}
// Printing content of array
for (char c : ch) {
System.out.println(c);
}
System.out.println(ch.length);
if(ch.length > 12) {
throw new IllegalArgumentException("the format should be XXX-XXX-XXXX");
}
if(phoneNumber.charAt(0) == "x") {
phoneNumber.charAt(0) = "3";
}
return phoneNumber;
}
I need to change the string of letters xxx-xxx-xxxx into 333-333-3333 how do i do that I tried this but getting an error where it says if(phoneNumber.charat(0) == "x") and below that please help me resolve this thank you
There are easier ways to do this but to answer your specific question you need to do the following. At the end of your method:
for (int i = 0; i < ch.length; i++) {
if(ch[i] == 'x' { // single quotes.
ch[i] = '3';
}
}
return new String(ch);
Also, to get the array of characters you can do.
char ch[] = phonenumber.toCharArray();
And contrary to what others have told you,
phonenumber.replace("x","3");
Doesn't work because Strings are immutable. You need to reassign it.
phonenumber = phonenumber.replace("x","3");
charAt functionr returns a character. You cannot use it to compare it to a string. You need to compare it to a character. So, your if-condition should be phoneNumber.charAt(0) == 'x'
It would help you to go through the Java Documentation
Try using "phoneNumber.replace("x","3");" It should replace the 'X's with '3's

How to know what content a character variable contains? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Now if i need to print "alphabets" if the character contains alphabets , need to print "number" if the character contains number.
sorry if the questions seems silly, new to java.
Using charAt() function i've converted the string to characters, all i need is to find the content of the chararcter.
The code :
public class Example
{
public static void main(String[] args) {
String str ="abc123";
for(int i = 0; i < str.length(); i++) {
System.out.println(Character.isDigit(str.charAt(i)) ? "number" : "alphabet");
}
}
}
Output :
alphabet
alphabet
alphabet
number
number
number
String.contains might be you're looking for.
String str = "abc";
boolean isAlphabet = str.contains("a"); // is true
boolean isNumber = str.contains("1"); // is false
Pass your input string to below snippet, if its number then it will return true if its alphabets then it returns false
public static boolean isNumeric(String str) {
try {
double d = Double.parseDouble(str);
} catch (NumberFormatException | NullPointerException nfe) {
return false;
}
return true;
}
Another alternative is to use regular expression
public static boolean isNumeric(String strNum) {
return strNum.matches("-?\\d+(\\.\\d+)?");
}

Check Java String containing one alpha & numeric characters and one of these special characters(#, #,$) [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 7 years ago.
Improve this question
Hi i want to check whether a string contains one alpha, one numeric and at least one of these special characters(#, #,$). So could anyone please guide me to resolve the issue.
Note : I have apache common lang 3.0 library. It will help us to resolve the above issue?
Here is a simple implementation.
private static boolean isValidPassword(char[] password) {
boolean hasLetter = false, hasDigit = false, hasSpecial = false;
for (char ch : password)
if (Character.isLetter(ch))
hasLetter = true;
else if (Character.isDigit(ch))
hasDigit = true;
else if ("##$".indexOf(ch) != -1)
hasSpecial = true;
return (hasLetter && hasDigit && hasSpecial);
}
I can be adjusted as need for exact definition of "letter", "digit", and "special", and to stop searching early when all 3 found, if needed.
You can do this using String.matches() and using a regex. Consider Following Solution.
public boolean isAlphaNumeric(String s){
String pattern= "^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!##$*()_+])[A-Za-z\d][A-Za-z\d!##$*()_+]";
if(s.matches(pattern)){
return true;
}
return false;
}
Let me know if it works.

Counted and Terminated String in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Two methods of implementing a string. A counted string explicitly
records its length. The terminated string’s length is determined by an
end-of-string mark.
Can anyone give an example of counted string and a terminated string in java.
CountedString {
char[] string;
int length;
int getLength() {
return length;
}
}
TerminatedString {
char[] string;
final static char TERMINATOR = '$';
int getLength() {
for (int i = 0; i < string.length; i++) {
if (string[i] == TERMINATOR) return i;
}
}
}
If you look in to the String.java will find that the length of String is being calculated by counter which traverse through string's characters .
Please referString.java for more information. You should look in to this class to see implementation of length() method.

Categories

Resources