[JAVA HELP]can you help me do a code?
this is my case: this is a array type
:::::::::::::::::::::::::::::::::::::::::::::::::::::
enter name1: zoey
enter name2: mike
enter name3: jin
enter a name: jin
it is found in index: 2
::::::::::::::::::::::::::::::::::::::
that is the out put can u help me create a code using array only and statement plsssss..
Comments
import java.util.*;
class search_name
{
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
String str[]=new String[3];
for(int i=0; i<3; i++)
{System.out.print("enter name"+(i+1)+": ");
str[i]=ob.nextLine(); //taking input for names
}
System.out.print("enter a name: ");
String s=ob.nextLine();
for(int i=0; i<3; i++)
{
if(s.equalsIgnoreCase(str[i]))
System.out.println("it is found in index: "+i);
}
}
}
copy paste above code....you'll get yourjob done!
Arrays look like this:
Type[] variableName = { data; }
For example, those in an array would be this:
String[] names = { "zoey", "mike", "jin" }
It can also be displayed as this:
String[] names = new String[3]();
names[0] = "zoey";
names[1] = "mike";
names[2] = "jin";
What you want to do is this:
String[] names = new String[3](); // New String array that can only hold up to three values.
Scanner scanner = new Scanner(System.in); // This Scanner reads System.in, which is the input from the command line.
for (int a = 0; a < 3; a++) { // Goes 3 times getting input
System.out.print("Enter name" + a + ": ");
names[a] = Scanner.nextLine(); // Reads the next line the user types
}
System.out.print("Enter a name: ");
String name = Scanner.nextLine(); // Get the name they entered
for (int a = 0; a < 3; a++) { // For loop that goes through the whole array
if (name.toLower() == name1.toLower()) {
System.out.println("It is found in index: " + a);
}
}
Use the Scanner class to get the three names from the user:
http://docs.oracle.com/javase/7/docs/api/java/util...
You can store the three names in an array of type String:
http://docs.oracle.com/javase/tutorial/java/nutsan...
And you can compare the final input to the other values using the compareTo() method:
http://docs.oracle.com/javase/7/docs/api/java/lang...