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.
Related
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 1 year ago.
Improve this question
I have a problem concatenating two strings or more be one variable in a loop in java.
My expected result as below :
/AIP/FE/perindo/custrpt/res_cus_1744341512710002.xml,/AIP/FE/perindo/custrpt/res_cus_1744341512710003.xml
The result is saved in one variable and return the value like this:
/AIP/FE/perindo/custrpt/res_cus_1744341512710002.xml,/AIP/FE/perindo/custrpt/res_cus_1744341512710003.xml
The following that i implemented :
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class pathData {
public static void main(String[] args) throws Exception {
String pthData = "\\\\10.28.88.28\\document\\FE\\perindo\\custrpt\\res_cus_1744341512710002.xml,\\\\10.28.88.28\\document\\FE\\perindo\\custrpt\\res_cus_1744341512710003.xml";
String bk = splitPath(pthData);
System.out.println(bk);
}
private static String splitPath(String bk) throws Exception {
String[] Str = bk.split(",");
String result = "";
if(Str.length > 1) {
for(int i = 0; i < Str.length; i++) {
String[] ss = Str[i].split("\\\\");
int a = ss.length;
String as = "/AIP"+"/"+ss[a-4]+"/"+ss[a-3]+"/"+ss[a-2]+"/"+ss[a-1];
result = as.concat(",");;
}
return result;
}
return "";
}
}
Based on my code, the result is always returned a value like this
/AIP/FE/perindo/custrpt/res_cus_1744341512710003.xml,
please give instructions and correct where the error lies.
You need to concatenate not just a "," to "as" but also the value of "as" to result. Otherwise it will only contain the last assigned value.
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
I am a student and I am learning Java.I recently got a question which said I had to find the largest word in a given string..I wrote a code but it is giving me an error that string index is out of bounds even though I limited it to the length of the string..Can someone help me with the code..Please use simple language(I am not an expert)
Code
import java.util.*;
class word
{
void def()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s1=sc.nextLine();
int length=s1.length();
length++; //My name
int j=0; //0123456
int word=0;
int findex=0;
int lindex=0;
int lword=0;
for(int i=0;i<length;i++)
{
if(s1.charAt(i)==' ' && j==0)
{
lword=i;
findex=0;
lindex=i;
j=i;
}
else if(s1.charAt(i)==' ')
{
if(i-j-1>lword)
{
findex=j;
lindex=i;
lword=i-j-1;
}
j=i;
}
else if(i==length-1)
{
if(i-j-1>lword)
{
findex=j;
lindex=i;
}
}
}
System.out.println("Largest word is:"+s1.substring(findex,lindex+1));
}
}
By doing length++ you make sure that your length variable will be larger then the actual string length. at the last loop step there will be no char at s1.charAt(i)
Just remove the line with length++
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,}");
}
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 6 years ago.
Improve this question
I am trying to create a profanity test for my app, but it seems to malfunction!! why?
code:
public boolean filter(String message)
{
String[] words={*CUSS WORDS*};
for(int i=0; i< (words.length-1); i++ )
{
if(message.indexOf(words[i].toLowerCase())!= -1)
{
return true;
}
}
return false;
}
OR Another code (BUT SAME FUNCTION):
public boolean filter(String message)
{
String[] words={CUSS WORDS};
for(int i=0; i< (words.length-1); i++ )
{
if(message.contains(words[i}))
{
return true;
}
}
return false;
}
So the PROBLEM IS:
I tried these 2 pieces of codes with similar results. For example for "Fuck", if I enter "fu" into my app it stops it from being entered or for "ass", if I enter "as" it stop it from being entered! (Filter works to stop any profanity from entering the chat)
Store your curse words in a set, then break up the users sentence into individual words. Check each word to see if it's in your set of curse words.
public boolean curse(String str){
//Create your set here
HashSet<String> wordSet = new HashSet<String>();
//Use it's add function to add your curse words
wordSet.add("ass");
String array[] = str.split(" ");
for(String s : array){
if(wordSet.contains(s.toLowerCase()))
return true;
}
return false;
}
I can't comment because of my reputation, but, continuing Elroy Jetson's answer, you initialize the HashSet using Arrays.asList, as is described here in this answer: https://stackoverflow.com/a/16194967/2836264. The HashMap constructor takes in this case a List<String>, that is created from the String[].
String[] cussArray = {"fuck", "shit", "brocolli"};
HashSet<String> cussSet = new HashSet<>(Arrays.asList(cussArray));
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
{
public static int WordCount (String cString)
{
String currentWord;
int index;
int spacePos;
int validWordCount=0;
boolean validWord;
char upClowC;
cString=cString.trim()+" ";
spacePos=cString.indexOf(" ");
validWord=true;
for(index=0;index<cString.length();index++)
{
currentWord=cString.substring(0,spacePos);
upClowC=Character.toUpperCase(currentWord.charAt(index));
if(upClowC<'A'||upClowC>'Z')
{
validWord=false;
}
}
if(validWord==true)
{
validWordCount++;
}
return validWordCount;
}
public static void main(String[] args)
{
String sentence;
System.out.println("enter a sentence:");
sentence=EasyIn.getString();
WordCount(sentence);
}
}
I'm trying to create a method which takes a sentence and picks out the valid words (i.e. no numbers or symbols), but I keep getting an out of bounds error.
I can't use an array.
Your problem is here:
currentWord = cString.substring(0, spacePos);
upClowC = Character.toUpperCase(currentWord.charAt(index));
currentWord gets shorter, but index is still running from 0 to the length of the string.
General notes:
Follow Java naming conventions and change the name of your method to begin with small letter
if(validWord) is enough when you want to compare something to true, otherwise it's like asking "is it true that the value is true" instead of simply "is the value true"
Next time post your stack trace to get better and sooner help
In your code, you are doing
spacePos = cString.indexOf(" ");
And then inside the loop:
currentWord = cString.substring(0,spacePos);
upClowC = Character.toUpperCase(currentWord.charAt(index));
Now, because of the loop, the index will take values from 0 to your string length minus 1. If your substring (currentWord) is smaller than your string - which probably is -, then currentWord.charAt(index) will try to index out of the bounds of the substring, which is why you get the error.