English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Please correct my method below

private boolean scanCapital (String s)
{
System.out.println(s);
boolean result = true;
String AZ = "AIUEO";
for (int i = 0; i<= AZ.length()-1 && result; i++)
{
if (AZ.indexOf(s.charAt(0)) != -1)
result = true;
}
return result;
}

2006-10-30 11:37:45 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

public boolean scanCapital (String s)
{
System.out.println(s);
boolean result = true;
String AZ = "AIUEO";
for (int i = 0; i<= AZ.length()-1 && result; i++)
{
if (AZ.indexOf(s.charAt(0)) != -1)
result = true;
}
return result;
}

2006-10-30 11:47:13 · answer #1 · answered by rose jone 1 · 0 0

First of all, your method name doesn't really convey what the method is for or why it returns a boolean. Perhaps a better name would be "beginsWithUppercaseVowel(String s)".

Anyway, here is my revision:
private boolean scanCapital(String s) {
System.out.println(s);
boolean result = false;//presumed until determined
String AZ = "AIUEO";
for (int i = 0; i <= AZ.length() - 1 && !result; i++) {
if (AZ.indexOf(s.charAt(0)) != -1){
result = true;
}
}
return result;
}

Of course, this could all be replaced with the existing String method matches():

String s = ...;//your string here
String regex = "[AIUEO]";//regular expression
boolean uppercaseVowel = s.substring(0,1).matches(regex);

Note: I leave it to you to verify that the String contains at least one character. Also, if your browser truncates the text or code, just hover your mouse over the ellipses (...) and a popup will show the full text.

2006-11-02 08:46:59 · answer #2 · answered by vincentgl 5 · 0 0

Inside the for, u have to put one more else part for ur if, because no where u are reinitialising result variable to false.
This is the mistake i found.

2006-10-31 05:14:02 · answer #3 · answered by Sudha P 2 · 0 0

setText public void setText(String t)gadgets the textual content it really is equipped by technique of this textual content area of be the wanted textual content. Parameters: t - the hot textual content; if this parameter is null then the textual content is wanting to the empty string "" See also: getText()

2016-12-05 09:28:01 · answer #4 · answered by harbert 4 · 0 0

fedest.com, questions and answers