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 2 years ago.
Improve this question
for (int i = 0; i < alist.size(); i++) {
if ( alist.get(i).average() == d ) {
return alist.get(i).getCountry();
}
}
return "";
}
I need to return the name of the country which is in alist.get(i).getCountry(), but when I end the body with that it will say
error: missing {
When I put return "" it doesn't have an error.
What does return "" mean?
return ""; just returns an empty string.
If you put alist.get(i).getCountry() outside of the for loop it will not make sense. (I'm assuming that's what you mean by "end the body with that.") It depends on i, which only exists in the loop.
It's tough to see why you're getting that error without seeing more of your code.
Related
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 9 months ago.
Improve this question
private Boolean validatePass() {
String val = pass.getEditText().getText().toString().trim();
String passwordVal = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-]).{8,}$";
if (val.isEmpty()) {
pass.setError("*Required");
return false;
} else if (!val.matches(passwordVal)) {
pass.setError("Invalid Password");
return false;
} else {
pass.setError(null);
return true;
}
}
I used this code for password Validation, what's wrong with this?
As per what your regex code is it should satisfy for all the symbols mentioned there #,?,!,#,$,%,^,&,*,- it will fail if you use symbols other than these like _,+,~,`
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 2 years ago.
Improve this question
My requirement is to check divisibility of the input by 7, for the various test cases and I have written this code but it is throwing me NumberFormat Exception
class Solution{
int isdivisible7(String num){
// code her
long i= Long.parseLong(num);
// to convert string into long
if(i%7==0)
return 1;
else
return 0;
}
}
How can I handle the exception and return the result for any (both valid and invalid) input ?
If the input num is not number, then it will throw NumberFormatException, so you just have to catch it. Also, function names should be in camel case. And finally, it's better to make the function return boolean rather then int of values 0 and 1.
boolean isDivisibleBy7(String num){
try {
long i = Long.parseLong(num);
return i % 7 == 0;
} catch (NumberFormatException e) {
// print some error message if you want
System.out.println("You haven't passed number");
return false;
}
}
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 4 years ago.
Improve this question
I want to loop through and skip all inputs that are less or equal 0 or non integers.
I have written this code, but it doesn't work and I don't understand why.
while(!userInput.hasNextInt() || userInput.nextInt() <= 0) {
userInput.next();
}
return userInput.nextInt();
I think you need to change a logic a bit, for example:
while (userInput.hasNext()) {
if (userInput.hasNextInt()) {
int intValue = userInput.nextInt();
if (intValue > 0) {
return intValue;
}
}
userInput.next();
}
Because when you're trying to verify that int value is less or equal zero userInput.nextInt() <= 0, you're actually getting the value.
So, if it's not true, you will go to this line return userInput.nextInt();, but cursor will already be on the next value.
You can check whether there is any user input or not. If there is take the input and then process it else continue with the loop.
while(userInput.hasNextInt()){
int a=userInput.nextInt();
if(a>=0){
return a;
}else
continue;
}
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 5 years ago.
Improve this question
i made a "for" a while a back ago now i need to change it to a "While" and change that to a "do", any help would be appriciated
for(int i = 3; i <= 10; i++) {
System.out.print(" " + i);
if(i % 10 == 0)
System.out.print("\n");
}
System.out.println();
}
}
A for statement:
for (ForInit; Expression; ForUpdate) Statement
is equivalent to the while statement:
{
ForInit;
while (Expression) {
Statement;
ForUpdate;
}
}
Converting a for to a while is simply a matter of cutting+pasting into this template.
for loops don't map onto to a do/while cleanly, because the Expression needs to be evaluated before Statement. You could do this:
{
ForInit;
do {
if (!Condition) break;
Statement;
ForUpdate;
} while (true);
}
but that's just horrible; you should be using a while statement instead.
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
if I have
String a = "abc,,,";
what should I do to get
result[0].equals("abc");
result[1].equals(",,,");
The most elementary way is this:
final int pos = a.indexOf(',');
if( pos == -1 ) { // character not found, handle this case somehow }
String [] result = new String [2];
result[0]=a.substring(0, pos);
result[1]=a.substring(pos);