Reputation: 353
I want a user to input a string into an array. What is the fastest way to do this and check if the string they imputed carries any non-alphabet characters?(e.g. #, &, 8, [, 0) Thanks so much!
Upvotes: 2
Views: 82
Reputation: 9192
To begin with, your question is pretty vague. A statement like, "I want a user to input a string into an array." I think is most likely meant to be: "I would like a User's string input to be placed into an array.". Normally a User of your application doesn't input strings into an array, that is a task for your code to carry out. All your User is expected to do is supply one or more strings which of course manifests the question....How many strings is the User expected to supply? Okay, let's assume it's unlimited. Does the array already exist and does it already contain string elements? Let's assume, who cares.
I'm not even going to ask how or where the User is expected to input these desired strings. Whether it's from a console or a GUI of some sort really doesn't matter to me at this point because it obviously doesn't matter to you. We just want to get the job done and that's cool. This is where supplying code that you have already tried comes in helpful to those trying to help. You know, Help those to help you.
Let's start fresh and assume our array which is to hold User Input Strings has not been established yet. Let's name it inputStringArray and we have a variable which is to hold the string input from a User, let's name it inputString. The code below which is overly commented should take care of business.
Create a new java application project titled UserInputToArray then copy/paste the following code over the auto created class (in NetBeans anyways):
public class UserInputToArray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Where you declare or establish your input strings
// array is up to you as long as the scope of the
// varaible can reach our call to the addUserInputToArray
// method below.
String[] inputStringArray = {};
// How you acquire the User's input string is up to you...
String inputString = "Hello There";
// Pass our input string array and user string input to
// our addUserInputToArray() method and let it modify
// or rather append the User input string to our inputStringArray
// array variable.
inputStringArray = addUserInputToArray(inputStringArray, inputString);
// This is used just to test our input string array so that
// you can see that the User input string has been successfully
// appended to the inputStringArray array. you can omit it.
for (int i = 0; i < inputStringArray.length; i++){
System.out.println(inputStringArray[i]);
}
}
/**
* This method is used to append a User Input String to our
* inputStringArray[] variable.
* @param stringArray : (String Array) This is where you supply the
* input string array variable.
*
* @param inputString : The User's supplied input string is provided here.
*
* @return : A String array with the Users string input appended to it but
* only if it is found that the string only contains Alphabetic characters
* (a to z and A to Z and spaces).
*/
public static String[] addUserInputToArray(String[] stringArray, String inputString) {
// Get the length of our input string array and add 1.
//This is used so we don't have to type stringArray.length
//all the time.
int length = (stringArray.length + 1);
// Here we use the string matches method with a small regex
// expression string to make sure only alphabetic charaters are
// contained within the supplied User input string. Expression
// breakdown:
// (?i) Ignore letter case (we don't need to worry about that in this case).
// [ a-z] Match any characters that are either a to z or a space.
// * Any number of characters (strings can be any length).
// ( ) All enclosed in a set of brackets to create a group. Not
// really required in this case (just a habbit).
if (inputString.matches("((?i)[ a-z]*)")) {
// So, our acquired User input string has passed requirements and
// now it's time to append that string to our input string array.
// As you know there is no such thing as appending to an array so
// we need to simulate it and to do that we need to create a temporary
// array, increase its length to what is desired which in our case is
// once (1) every time this method is called, and then copy our passed
// original input string array into it while preserving the length of
// our temporary array and then finally forcing our original input
// string array to be our temporary array. Now we have a input string
// array which is one element size bigger than when we started and ready
// to have string data placed into it.
String[] tmp = new String[length];
if (stringArray.length != 0) {
System.arraycopy(stringArray, 0, tmp, 0, Math.min(stringArray.length, tmp.length));
}
stringArray = tmp;
// Append our User input string to the array.
stringArray[length-1] = inputString;
}
return stringArray;
}
}
I hope this helps you somewhat.
Upvotes: 1
Reputation: 819
You can use Scanner
to take in user input:
String alphabet = "abcdefghijklmnopqrstuvxyz";
int arraySize = 10;
String[] charArray = new String[arraySize];
Scanner sc = new Scanner(System.in);
for (int i=0; i<arraySize; i++) {
String input = sc.next();
if (!alphabet.contains(input) {
charArray[i] = input;
} else {
charArray[i] = "";
}
}
Upvotes: 1
Reputation: 1261
You could do it this way:
int size = 1024;
String[] strs = new String[size];
Scanner sc = new Scanner(System.in);
for (int i=0; i<size; i++) {
String input = sc.next();
if (input.matches("[A-Za-z]")) {
strs[i] = input;
} else {
strs[i] = "";
}
}
sc.close();
I hope it helps.
Upvotes: 1