如何在Array中搜索字符串

时间:2015-02-16 12:47:45

标签: java arrays

我正在尝试创建一个具有搜索功能的酒店数据库程序,用户可以输入住在酒店的名称,程序将显示该人的房间号。下面的代码确实识别输入的名称与程序中的现有名称相同,但每次都会出现错误:

Exception in thread "main" java.lang.NullPointerException
at hotel.Hotel.findroom(Hotel.java:113)
at hotel.Hotel.main(Hotel.java:51)
Java Result: 1 

我还留下了问号'???'在底部的代码中我不知道如何让程序显示匹配名称的房间号。

public class Hotel {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String command;
    Scanner input = new Scanner(System.in);
    String roomName;
    int roomNum = 0;
    String[] hotel = new String[12];

    initialise(hotel);   

    while ( roomNum  < 11 )

    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter command : ");
        command = input.next();

        if (command.charAt(0) == 'a') {
            addcustomer(hotel);
        }

        if (command.charAt(0) == 'v') {
            viewoccupants(hotel);
        }

        if (command.charAt(0) == 'e') {
            emptyrooms(hotel);

        }

        if (command.charAt(0) == 'd') {
            deleteroom(hotel);  
        }    

        if (command.charAt(0) == 'f') {
            findroom(hotel);  
        }    
    }
}

 private static void initialise( String hotelRef[] ) {
     for (int x = 0; x < 11; x++ ) hotelRef[x] = "e";
     System.out.println( "initilise ");
  }

private static void viewoccupants(String[] hotel) {
         for (int x = 0; x < 11; x++ )
          {
           System.out.println("room " + x + " occupied by " + hotel[x]);
         }
}

private static void addcustomer(String[] hotel) {
    String roomName;
    int roomNum;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter room number (0-10) or 11 to stop:" ) ;
    roomNum = input.nextInt();
    if (roomNum<11) {
        System.out.println("Enter name for room " + roomNum +" :" ) ;
        roomName = input.next();
        hotel[roomNum] = roomName ;   
    }
    else {
        System.exit(0);
    }
}

private static void emptyrooms(String[] hotel) {
    for (int x = 0; x < 11; x++ )
          {
           if (hotel[x].equals("e"))System.out.println("room " + x + " is empty");
         }
}

private static void deleteroom(String[] hotel) {
    String x = "e";
    int roomNum;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter room to be vacated: " );
    roomNum = input.nextInt();
    if (roomNum<11) {
        hotel[roomNum] = x;   
    }
    else {
        System.exit(0);
    }
}

private static void findroom(String[] hotel) {
    String roomName;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter name: " ) ;
    roomName = input.next();


    for(int i = 0; i < hotel.length; i++){
        if(hotel[i].equals(roomName)){
            System.out.println(roomName + " is located in room " + i);
        }
     }
   }
 }

3 个答案:

答案 0 :(得分:0)

你也可以在java中使用lambda。

int i = 0;
hotel.asList().forEach(i++ -> s==roomName -> System.out.println(n " is located in room " + i));

答案 1 :(得分:0)

我遇到了你的问题。您需要声明String [] hotel或String [] rooms作为全局成员。如果你在另一个函数中使用或者在main中使用它将是一个不同的String数组,那么当函数执行完成时,String [] hotel的范围就会消失。所以只有你才能获得 NPE 。所以你的代码应该如下所示,

public class Hotel {

/**
 * @param args the command line arguments
 */
private static String[] hotel= new String[11];
public static void main(String[] args) {
    String command;
    Scanner input = new Scanner(System.in);
    String roomName;
    int roomNum = 0;
    initialise();   

    while ( roomNum  < 11 )

    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter command : ");
        command = input.next();

        if (command.charAt(0) == 'a') {
            addcustomer();
        }

        if (command.charAt(0) == 'v') {
            viewoccupants();
        }

        if (command.charAt(0) == 'e') {
            emptyrooms();

        }

        if (command.charAt(0) == 'd') {
            deleteroom();  
        }    

        if (command.charAt(0) == 'f') {
            findroom();  
        }    
    }
}

 private static void initialise( ) {
     for (int x = 0; x < 11; x++ ) hotel[x] = "e";
     System.out.println( "initilise ");
  }

private static void viewoccupants() {
         for (int x = 0; x < 11; x++ )
          {
           System.out.println("room " + x + " occupied by " + hotel[x]);
         }
}

private static void addcustomer() {
    String roomName;
    int roomNum;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter room number (0-10) or 11 to stop:" ) ;
    roomNum = input.nextInt();
    if (roomNum<11) {
        System.out.println("Enter name for room " + roomNum +" :" ) ;
        roomName = input.next();
        hotel[roomNum] = roomName ;   
    }
    else {
        System.exit(0);
    }
}

private static void emptyrooms() {
    for (int x = 0; x < 11; x++ )
          {
           if (hotel[x].equals("e"))System.out.println("room " + x + " is empty");
         }
}

private static void deleteroom() {
    String x = "e";
    int roomNum;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter room to be vacated: " );
    roomNum = input.nextInt();
    if (roomNum<11) {
        hotel[roomNum] = x;   
    }
    else {
        System.exit(0);
    }
}

private static void findroom() {
    String roomName;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter name: " ) ;
    roomName = input.next();


    for(int i = 0; i < hotel.length; i++){
        if(hotel[i].equals(roomName)){
            System.out.println(roomName + " is located in room " + i);
        }
     }
   }

}

输出:

initilise 
Enter command : 1
Enter command : a
Enter room number (0-10) or 11 to stop:
1
Enter name for room 1 :
kalai
Enter command : f
Enter name: 
kalai
kalai is located in room 1
Enter command : 

答案 2 :(得分:0)

你得到一个空指针,因为你没有初始化hotel []里面的最后一个字符串。要么这样做,要么在findroom函数中交换函数中的对象和参数等于循环内部。 (roomname.equals(酒店[I]))。

问题的起源是因为你创建了一个包含12个元素的数组,但是你总是将索引从0到10,即11个元素,除了在你通过数组长度的查找函数中,这是12个元素。

始终使用&#34; for(int i = 0; i&lt; hotel.length; i ++)&#34;穿过你的阵列。