Write the inputAlphaString method . This method takes a Scanner as a parameter a
ID: 3691170 • Letter: W
Question
Write the inputAlphaString method. This method takes a Scanner as a parameter and enters a String consisting only of alphabetic characters (in the English language) from the user using a StringBuilder object. In particular, it should:
Create an empty StringBuilder object.
Repeatedly prompt the user to enter an alphabetic String. An alphabetic String s is one that causes the following expression to be true: s.matches("[a-zA-Z]+"). If the String entered is alphabetic, the method adds it to the StringBuilder object. As soon as a non-alphabetic String is entered, the loop stops. The function returns a String formed from the StringBuilder object. Check the Java API to see StringBuilder methods that are helpful for turning a StringBuilder object into a regular String.
The following is some output from my completed solution:
How many strings? -3
Please enter a positive (> 0) number.
How many strings? 0
Please enter a positive (> 0) number.
How many strings? 3
Enter string #1:
Enter an alphabetic string followed by enter: t
Enter an alphabetic string followed by enter: e
Enter an alphabetic string followed by enter: s
Enter an alphabetic string followed by enter: t
Enter an alphabetic string followed by enter: %
Enter string #2:
Enter an alphabetic string followed by enter: one
Enter an alphabetic string followed by enter: two
Enter an alphabetic string followed by enter: three
Enter an alphabetic string followed by enter: four
Enter an alphabetic string followed by enter: five
Enter an alphabetic string followed by enter: six
Enter an alphabetic string followed by enter: &
Enter string #3:
Enter an alphabetic string followed by enter: good
Enter an alphabetic string followed by enter: bye
Enter an alphabetic string followed by enter: @
The strings you entered are:
test onetwothreefourfivesix goodbye
Explanation / Answer
Hello there,
Please find below code and o/p as per your requirement.
import java.util.Scanner;
import java.util.regex.Pattern;
/**
*
* @author dipal.prajapati
*
*/
public class StringBuilderTutorial {
static String strPattern = "[a-zA-Z]+";
static Pattern pattern = Pattern.compile(strPattern);
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
inputAlphaString(scanner);
}
/**
* The method takes number of inputs for string and then continues taking
* string input until it gets non alphabetic string.
*
* @param scanner
*/
public static void inputAlphaString(Scanner scanner) {
StringBuilder builder = new StringBuilder();
int index = 0, numOfStrings;
String alphabatic;
System.out.println("How many strings?");
// Take no of string input until we get correct one which is >0
while (true) {
numOfStrings = scanner.nextInt();
if (numOfStrings <= 0) {
System.out.println("Please enter a positive (> 0) number.");
System.out.println("How many strings?");
} else {
break;
}
}
// Take input string until we find non alphabetic string hence match the
// given pattern.
while (index < numOfStrings) {
System.out.println("Enter string #" + (index + 1) + ":");
while (true) {
System.out.println("Enter an alphabetic string followed by enter:");
if (pattern.matcher(alphabatic = scanner.next()).matches()) {
builder.append(alphabatic);
} else
break;
}
builder.append(" ");
index++;
}
System.out.println("The strings you entered are: ");
System.out.println(builder.toString());
}
}
===O/P===
How many strings?
-3
Please enter a positive (> 0) number.
How many strings?
0
Please enter a positive (> 0) number.
How many strings?
3
Enter string #1:
Enter an alphabetic string followed by enter:
t
Enter an alphabetic string followed by enter:
e
Enter an alphabetic string followed by enter:
s
Enter an alphabetic string followed by enter:
t
Enter an alphabetic string followed by enter:
%
Enter string #2:
Enter an alphabetic string followed by enter:
one
Enter an alphabetic string followed by enter:
two
Enter an alphabetic string followed by enter:
three
Enter an alphabetic string followed by enter:
four
Enter an alphabetic string followed by enter:
five
Enter an alphabetic string followed by enter:
six
Enter an alphabetic string followed by enter:
&
Enter string #3:
Enter an alphabetic string followed by enter:
good
Enter an alphabetic string followed by enter:
bye
Enter an alphabetic string followed by enter:
@
The strings you entered are:
test onetwothreefourfivesix goodbye
Let me know if you have any queries .
Thanks.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.