I need to check the array to see if the user input is already present, and display a message as to whether it is or isn't there. The first part is working, but I tried to create a method for the word check, and I'm not sure if I'm on the right path or not, cheers.
import java.util.Scanner;
public class InputLoop {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String array[] = new String[10];
int num = array.length, i = 0;
System.out.println("Enter a word");
for (i = 0; i < num; i++) {
while (scan.hasNextInt()) // while non-integers are present...
{
scan.next(); // ...read and discard input, then prompt again
System.out.println("Bad input. Enter a word");
}
array[i] = scan.next();
WordCheck();
}
}
public void WordCheck(String[] i) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter another word");
if (scan.next().equals(array[i])) {
System.out.println("The word has been found");
} else {
System.out.println("The word has not been found");
}
}
}
4 Answers 4
Right. You've clearly gone down a bad thought process, so let's just clear the slate and have a re-think.
- Step one: You want to take some user input
- Step two: Compare it with all previous user inputs to see if it's present.
- If it is present, return a message indicating that value has been inputted.
- otherwise ignore the input and continue execution
- Repeat step one.
The solution
So, let's review what you've got, and how you need to change it.
public static void main(String[] args)
If I were you, I would avoid calling methods directly from here. If you do, every method will need to be static, which is a pointless adjustment in scope for the functionality of your class. Create a new instance of your class, inside the main method, and move this code to the class' constructor. This will remove the need to make every single method static.
Scanner scan = new Scanner(System.in);
String array[] = new String[10];
Okay, so you've created a scanner object that takes input from the System.in
stream. That's a reasonable thing to do when taking input from the keyboard. You've also created an array to contain each item. If you only want the user to be able to type in 10 values, then this is fine. Personally, I would use an ArrayList, because it means you can take in as many user inputs as the user desires.
Secondly, you want a function to compare the input, with all other inputs. What you have at the moment clearly isn't working, so let's have another go at it.
You will need some input, userInput
, and a collection to compare it against, allInputs
.
allInputs
needs to be accessible from any point in the program, so it's probably wise to make it into a field, rather than a local variable.
Then, because you're comparing userInput against all values, you're going to need a foreach loop:
for(String s : allInputs)
{
if(s.equals(userInput))
{
// Output message code.
}
}
Now the trick is fitting this inside a loop that works with this program. That is up to you.
-
Thanks for taking the time to help, much appreciated. Few questions here sorry! So the userInput is the word the user inputs to be checked against the array right? allInputs is equal to the values held in the array? What is 'String s'?user1554786– user155478603/07/2013 22:42:46Commented Mar 7, 2013 at 22:42
-
You should read the documentation about a For each loop in Java. Everything else you said is correct though. Yes, userInput is what the user typed in, and allInputs is all the values the user typed in. I would also STRONGLY recommend looking at ArrayLists, via my link in the answer.christopher– christopher03/07/2013 22:44:27Commented Mar 7, 2013 at 22:44
One simple solution is to use a Set
:
Set<String> words = new HashSet<String>();
Add words with the add()
method and check if a word is already added with contains(word)
method.
EDIT
If you must use Arrays
you can keep the array sorted and do a binary search:
Arrays.sort(words);
boolean isAlreadyAdded = Arrays.binarySearch(words, newWord) >= 0;
-
This question looks a lot like an early HW assignment. They might be forced to implement the Array[] object / not know about Setstyh– tyh03/07/2013 22:51:02Commented Mar 7, 2013 at 22:51
You're going to have to loop through the entire array and check if scan.next() equals any of them - if so return true - as such:
String toCheck = scan.next();
for (String string : i) { //For each String (string) in i
if (toCheck.equals(i)) {
System.out.println("The word has been found");
return;
}
}
System.out.println("The word has not been found");
This supposes you call WordCheck()
, passing the array to it - this method also has to be static for you to call it from the main()
method.
You can use the arraylist.contains("name") method to check if there is a duplicate user entry.
WordCheck
with no parameters even though it expects aString[]
. Also, withinWordCheck
array
isn't referencable.