I am trying to create a function that returns the location of specific items within an array. The array that I am dealing with is populated with Strings, I want the function to be given a String array with a String and then find the index of that String within the array. However, when I pass the array to the function it interprets the array as [Ljava.lang.String;@4dd8dc3
.
Code:
import java.util.Scanner;
public class findIndex {
public static int findIndex(String arr[], String username) {
System.out.println(arr); // This prints [Ljava.lang.String;@4dd8dc3
int len = arr.length;
int i = 0;
if (len == 0) {
return -1;
}
while (i < len) {
if (arr[i] == username) {
return i;
} else {
i = i 1;
}
}
return -1;
}
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.print("Enter username: ");
String userName = obj.nextLine();
String[] userNameList = {"username1", "username2", "NA"};
String[] passwordList = {"password1", "password2", "NA"};
System.out.println("Index of username in list = " findIndex(userNameList, userName));
}
}
For example, the program above should print 0 if the user inputs "username1"
, but it prints -1
every time due to the array being read as [Ljava.lang.String;@4dd8dc3
. I was just wondering if there is a better way to find the index of a specific string within an array. Thanks in advance.
CodePudding user response:
There are several problems with your code
- You use
.equals()
to compare strings and objects and not==
:
string1.equals(string2); // == does not work since it compares references
- You cannot print an array like that, you should use a
toString()
function:
System.out.println(Arrays.toString(arr));
.equals()
and toString()
are both well known in the world of Java. .equals()
is always accompanied by another function called hashCode()
.
Google them.
There are a lot of ways to find the index of an array item.
My favorite way would be using java 8 streams:
OptionalInt indexOfUser = IntStream.range(0, arr.length)
.filter(i -> username.equals(arr[i]))
.findFirst();
if(indexOfUser.isEmpty())
{
return -1; // NOT FOUND
}
else
{
return indexOfUser.getAsInt();
}
CodePudding user response:
Some example code implemented:
import java.util.Scanner;
public class FindIndex {
public static int findIndex(String arr[], String username) {
System.out.println(arr); // This prints [Ljava.lang.String;@4dd8dc3
// It's bacause you try to print one array.
// If you call some index of it, ir prints
System.out.println(arr[0]); // This prints the element of index=0 (first element)
int len = arr.length;
int i = 0;
if (len == 0) {
return -1;
}
while (i < len) {
if (arr[i].equals(username)) {
return i;
} else {
i = i 1;
}
}
return -1;
}
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.print("Enter username: ");
String userName = obj.nextLine();
String[] userNameList = {"user1","user2","user3", "username2", "NA"};
String[] passwordList = {"password1", "password2", "NA"};
System.out.println("Index of username in list = " findIndex(userNameList, userName));
}
}```
CodePudding user response:
public static int findIndex(String[] arr, String username) {
for (int i = 0; i < arr.length; i )
if (arr[i].equals(username))
return i;
return -1;
}