variable type question java
So here is my problem im trying to search an array for a value and then return the index as well as the string name.
But my problem is that when i return the index it sets it as a string because thats what the method called for so when i try to type cast the index to be changed to a string my compiler throws errors. Here are the trouble some methods, i can post my whole code if you guys would like to see it.
public static String studentGrade(Scanner stdIn)
{
System.out.print("What student do you want to enter a grade for?");
String searchName=stdIn.next();
String index = findStudent(students, searchName开发者_如何学Python);
int studentId =(int) currentStudentId(students, searchName);
if (index == searchName)
{
System.out.println("Student was found, " + index + studentId);
System.out.println("What is the grade that you want to input for this student?");
String input = stdIn.next();
String studentGrade = input;
return studentGrade;
}
else
{
System.out.println("not found");
}
}
public static String findStudent(String[] students, String searchName)
{
for (int i = 0; i < students.length; i++)
{
if (students[i].equals(searchName))
{
String currentStudentId = i;
return currentStudentId;
return searchName;
}
} // end for
String fail = "fail";
return fail;
} // end findStudent
I don't believe returning both String and index as int is a good idea. Why don't you just return index and get the student by looking up in array that contains all students. If the student wasn't found you can return for example -1.
This is what I mean:
String[] students = new String[] { ... };
//...
int indexFound = findStudent(students, "John Doe");
if (indexFound >= 0) {
String studentFound = students[indexFound];
}
PS. Your code contains errors (like doubled return command)
Why would you want to return searchName
from findStudent()
to a method that passes it through an argument.
Of course the caller method already has the value. Just return the index:
Example:
public static int findStudent(String[] students, String searchName)
{
for (int i = 0; i < students.length; i++)
{
if (students[i].equals(searchName))
{
return i;
}
} // end for
int fail = -1;
return fail;
} // end findStudent
An index is naturally an int, not a String. You should return an int.
Assuming this is homework, and you have to return a String, you can convert a number into a String using the following.
return ""+currentStudentId;
However, the problem you have is that you are trying to return two values.
I suspect you have mis-understood the requirements, I suggest you read them again.
A shorter example, using varargs
public static int findString(String string, String... strings) {
for (int i = 0; i < strings.length; i++)
if (strings[i].equals(string))
return i;
return -1; // for not found.
}
Or even the following works for any type.
public static <T> int indexOf(T t, T... ts) {
for (int i = 0; i < ts.length; i++)
if (ts[i].equals(t))
return i;
return -1; // for not found.
}
e.g.
int found = indexOf(5, 1,3,5,7); // found = 2;
精彩评论