-3

I am trying to get the persons position in the array by typing the persons name in the console.

public static void main(String[] args) {
    Students[] student = new Students[50];

    student[0] = new Students("Helen", "Jones", 10);
    student[1] = new Students("Fred ", "Smith", 15);
    student[2] = new Students("George", "Evans", 25);
    student[3] = new Students("Malcolm", "Evans", 30);


    Scanner userInput = new Scanner(System.in);
    System.out.println("Enter Forename");
    String name = userInput.nextLine();
    Arrays.asList(student).indexOf(name);

I expect to be able to input a name into the console and then it'll show the persons position in the array. Please, any form of help is appreciated. Thank you

Andreas
  • 138,167
  • 8
  • 112
  • 195
ray
  • 29
  • 5
  • You read the new line from the `System.out.println("Enter Forename");`. You can add a `userInput.nextLine();` after the `println`. [Scanner is skipping nextLine](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Butiri Dan Jul 16 '19 at 21:30
  • a hashmap would be a better collection to store your student objects instead of an array. Use the name of the student as the key and the student as the value. This will allow you to look the person up by name – RAZ_Muh_Taz Jul 16 '19 at 21:32
  • Iterate the array yourself and compare to first name of student object. – Andreas Jul 16 '19 at 21:33

3 Answers3

0

You either need to forEach your list and check the name against the objects name or filter the list and get the object that matches.

int index= -1
for(int i =0;i<students.length();i++){
    if(students[i].getFirstName().compareToIgnoreCase(name)){
         index = i;
    }
} 
if(index<>-1){
    //name was found do something
 }
mavriksc
  • 1,086
  • 1
  • 7
  • 10
0

If you aren't constrained to using an array, you could use a map instead to store your objects instead of an array. Using the name as the key and the student object as the key. This would be a faster lookup than if you stored the objects in the array and looped it, searching for a matching name.

HashMap<String, Students> map = new HashMap<>();
map.put("Helen", new Students("Helen", "Jones", 10));
map.put("Fred", new Students("Fred", "Smith", 15));
...

Scanner userInput = new Scanner(System.in);
System.out.println("Enter Forename");
String name = userInput.nextLine();

Students studentToFind;
if(map.contains(name))
{
   studentToFind = map.get(name);
   //do stuff with the student
}
else
{
   System.out.println("No student with name " + name + " was found");
}
RAZ_Muh_Taz
  • 3,964
  • 1
  • 10
  • 22
0

I think I MIGHT know how to solve your problem

this is your code

...
System.out.println("Enter Forename");
String name = userInput.nextLine();
println(Arrays.asList(student).indexOf(name));

you should change it to

...
HashMap<Name, Student> studentz = new HashMap<>();
System.out.println("Enter Firstname");
String name = userInput.nextLine();
Arrays.asList(student).indexOf(studentz.get(codeIsIn));

the problem is you were trying to find the index of a string NAME instead of the index of the object string is in

if you are trying to get the object it is in at the end, just use

...
println(studentz.get(codeIsIn))

by the way, replace Name with whatever the property name is of "Helen" also, you weren't printing it to the console