I have been searching here for some time but haven't been able to find the answer to it.
I am basically required to use an array for this assignment from college. And then I am supposed to check that the input (which is also a String) matches whatever's stored within the String array.
I know one can easily compare Strings by using the .equals() method. However, the same method is not working with the String array.
I created the following example of code for the purpose of StackOverflow so you can use it to explain it to me, if you'd like.
What am I doing wrong?
import java.util.Scanner;
class IdiocyCentral {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
/*Prints out the welcome message at the top of the screen*/
System.out.printf("%55s", "**WELCOME TO IDIOCY CENTRAL**\n");
System.out.printf("%55s", "=================================\n");
String [] codes = {"G22", "K13", "I30", "S20"};
System.out.printf("%5s%5s%5s%5s\n", codes[0], codes[1], codes[2], codes[3]);
System.out.printf("Enter one of the above!\n");
String usercode = in.nextLine();
if (codes.equals(usercode)) {
System.out.printf("What's the matter with you?\n");
}
else {
System.out.printf("Youda man!");
}
}
}
I apologize if this has been asked before and I just missed it, if its a double question, I will remove it.
-
1You'll need to loop over the array and check each string individually.Dave Newton– Dave Newton2012年01月20日 02:40:29 +00:00Commented Jan 20, 2012 at 2:40
6 Answers 6
I presume you are wanting to check if the array contains a certain value, yes? If so, use the contains
method.
if(Arrays.asList(codes).contains(userCode))
-
@PeterOlsen Ah finally I could accept your answer. Thanks a million!Nico– Nico2012年01月20日 02:53:48 +00:00Commented Jan 20, 2012 at 2:53
-
3@PeterOlsen - that's probably the cleanest solution, but I expect that the OP's instructor is expecting him/her to code this using a loop ... and in the process, practice using arrays and loops.Stephen C– Stephen C2012年01月20日 03:36:18 +00:00Commented Jan 20, 2012 at 3:36
-
@StephenC This is only part of the code and it is perfect the way PeterOlsen has suggested. I have to work with loops but it's separate from this part of the code :)Nico– Nico2012年01月20日 05:03:20 +00:00Commented Jan 20, 2012 at 5:03
Right now you seem to be saying 'does this array of strings equal this string', which of course it never would.
Perhaps you should think about iterating through your array of strings with a loop, and checking each to see if they are equals() with the inputted string?
...or do I misunderstand your question?
-
What @PeterOlsen suggested works. I am just waiting to be able to accept his answer. Sorry if my question isn't clear enough.Nico– Nico2012年01月20日 02:50:47 +00:00Commented Jan 20, 2012 at 2:50
-
No worries. As it's a college question though I figured your instructor was looking for you to learn control flows like loops etc. That said, you may be past that in your college course anyway :)f1dave– f1dave2012年01月20日 03:55:22 +00:00Commented Jan 20, 2012 at 3:55
Iterate over the codes
array using a loop, asking for each of the elements if it's equals()
to usercode
. If one element is equal, you can stop and handle that case. If none of the elements is equal to usercode
, then do the appropriate to handle that case. In pseudocode:
found = false
foreach element in array:
if element.equals(usercode):
found = true
break
if found:
print "I found it!"
else:
print "I didn't find it"
-
When I glanced at the pseudo code I almost immediately assumed it was Python.Peter Olson– Peter Olson2012年01月20日 03:00:44 +00:00Commented Jan 20, 2012 at 3:00
If I understand your question correctly, it appears you want to know the following:
How do I check if my
String
array containsusercode
, theString
that was just inputted?
See here for a similar question. It quotes solutions that have been pointed out by previous answers. I hope this helps.
Instead of using array you can use the ArrayList
directly and can use the contains method to check the value which u have passes with the ArrayList
.
import java.util.Scanner;
import java.util.*;
public class Main
{
public static void main (String[]args) throws Exception
{
Scanner in = new Scanner (System.in);
/*Prints out the welcome message at the top of the screen */
System.out.printf ("%55s", "**WELCOME TO IDIOCY CENTRAL**\n");
System.out.printf ("%55s", "=================================\n");
String[] codes =
{
"G22", "K13", "I30", "S20"};
System.out.printf ("%5s%5s%5s%5s\n", codes[0], codes[1], codes[2],
codes[3]);
System.out.printf ("Enter one of the above!\n");
String usercode = in.nextLine ();
for (int i = 0; i < codes.length; i++)
{
if (codes[i].equals (usercode))
{
System.out.printf ("What's the matter with you?\n");
}
else
{
System.out.printf ("Youda man!");
}
}
}
}