简体   繁体   中英

It is possible to use a Scanner in this manner?

I am programming something, that creates multiple Teacher objects:

public class Teacher {

// 1) Define instance variables
String teacherName;
String catchPhrase;
public static int roomNum;
// 2) Write the constructor method

public Teacher() {
    teacherName = "unknown";
    catchPhrase = "unknown";
    roomNum = 0;
}//end no-arg constructor

public Teacher(String newTeacher, String newCatch, int newRoom) {
    teacherName = newTeacher;
    catchPhrase = newCatch;
    roomNum = newRoom;
}



// 3) Write the proccessing methods (getters and setters)

public void setName(String newName) {
    teacherName = newName;
}

public String getName() {
    return teacherName;
}

public static int getRoom() {
    return roomNum;

}

// 4) Write the out method (eg toString() method)

public String toString() {

    String str = "Name: " + teacherName + ". \nCatch phrase: " + catchPhrase + " \nRoom number: " + roomNum + ".";

    return str;

}//end toString

public static void main(String args[]) {

}
}

It is executed like this:

Teacher teacherName("First Last", "Catch Phrase", 123); 

I have multiple Teacher objects. I am trying to make a scanner that checks input from the user to see if the number entered is a room number from one of those objects:

 while (input != -1) {
        Scanner scan = new Scanner(System.in);
        input = scan.nextInt();
            if(input == Teacher.getRoom()) {
                System.out.println("Yes");
            } else if(input != Teacher.getRoom()) {
                System.out.println("Nope");
            }

        }

But I'm not sure how to do it. Or if it's possible.

Any help would be highly appreciated.

Thank you!

EDIT:

I tried a different way. I tried to use an array with the room numbers, and compare it with the input, but it hasn't worked.

int[] rooms = {220, 226, 204, 234, 236, 242, 243, 129, 125, 136, 101, 104, 107, 113, 103, 105, 102, 108, 117, 111, 111, 313, 310, 132, 127, 129, 125, 
            + 124, 122, 126, 130, 137, 114, 138, 136, 123, 135, 128, 139, 134, 220, 215, 211, 222, 253, 213, 252, 231, 255, 224, 254, 
            + 218, 235, 233, 000, 212, 223, 257, 217, 259, 214, 240, 258, 221, 210, 219, 256, 216, 110, 133, 115, 423, 253, 230, 115, 106, 1062, 418, 415};

 if (rooms.equals(input)) {
            System.out.println("Yes");
        } else {
            System.out.println("Nope");
        }

That didn't work. Nor did:

 if (Arrays.asList(rooms).contains(input)) {
            System.out.println("Yes");
        } else {
            System.out.println("Nope");
        }

Any help with getting this to work with in integer array, (or a better method) would be appreciated.

Thank you.

EDIT2:

I got it working like this:

if (rooms.contains(input)){
            System.out.println("That teacher is in our database!");
            //System.out.println(new int[(rooms).indexOf(1)]);
        } else {
        System.out.println("Sorry, that teachner was not found in our database!");
    }

Thank you very much!

easiest way to do that would be to create an array of Teacher and then inside your while loop, put a for loop that loops through all your Teacher objects and checks the room number. or even better, make an array of just room numbers and loop through that.

also, you probably want to instantiate the scanner outside the while loop, then do while(scan.hasNextInt()){...

so it would be something like

Scanner scan = new Scanner(System.in);
Teacher[] teachears...
while (scan.hasNextInt()) {
    input = scan.nextInt();
    boolean isARoom = false;
    for(Teacher teach : teachers){
        if(input == teach.getRoom()) {
            isARoom = true;
        } 
    }
    if(isARoom)
        System,out.println("Yes");
    else
        System,out.println("No");  
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM