if (room_num[a][b].equals(null))
运行时报错
为什么?讲的越详细越好
饥渴啊!第一次接触equals
运行时报错
为什么?讲的越详细越好
饥渴啊!第一次接触equals
解决方案 »
- 请教一个正则表达式,要求如下:
- 请问如何查看java文件时用的什么编码啊,
- 100求免费的手机号码所在地区查询web service,给个地址
- struts-hibernate 从数据库中删除数据--初学
- 一些关于Ioc的个人见解,希望网友来拍砖
- 开发servlet时出错,帮忙看一下!!
- 有没有做过LDAP研究的朋友,留个email或qq号交个朋友交流一下行吗?十分感激
- 给我一个EJB源码(SessionBean),要求与数据库交互!马上给分!SessionBean
- 看了有关EJB的资料,可还是弄不清其中的奥妙。能不能给讲一下,在实际例子中是个怎么样的概念?
- Jbpm6 spring struts2 mybaits整合问题
- 如何获取字符串所占用的宽度
- 冰天雪地跪求大虾拯救!
用==试下,因为equals是用来比较字符串的,更安全的写法
if (null==(room_num[a][b))
import java.util.Scanner;
public class Hotelmis {
int floor; //楼层
int number; //相同楼层房间数
String[][] room_num = new String[floor][number]; //房间号
public int getFloor() {
return floor;
}
public void setFloor(int floor) {
this.floor = floor; //
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Hotelmis(int floor,int number){
super();
this.floor = floor;
this.number = number;
}
public Hotelmis(){
floor = 5;
number = 23;
}
//每次写get set和构造方法,感觉很麻烦,怎么封装一个类,直接用 。错误的理解请老师纠正
//查找
public void find(){
for(int i = 0; i < floor; i++){
for(int j = 0;j < number; j++){
if (room_num[i][j] == null){
System.out.print(i);
System.out.println(j + "可以入住");
}else{
System.out.println("姓名为" + room_num[i][j] + "的客人入住" + i);
System.out.println( j + "房间");
}
}
}
}
public void inHotel(){
Scanner sc = new Scanner(System.in);
System.out.println("你想住几楼");
int a = sc.nextInt();
System.out.println("你想住几号房间");
int b = sc.nextInt();
for(int i = 0; i < floor; i++){
for(int j = 0;j < number; j++){
if (room_num[a][b] == null){
System.out.println("可以入住");
System.out.println("请输入客户姓名:");
String name = sc.next();
room_num[a][b] = name;
}else{
System.out.println("此房间有人");
}
}
}
}
public void outHotel(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要退的房间号:");
int c = sc.nextInt();
int d = sc.nextInt();
for(int i = 0; i < floor; i++){
for(int j = 0;j < number; j++){
room_num[c][d] = null;
}
}
}
public static void main(String args[]){
Hotelmis p = new Hotelmis(3,21);
Scanner sc =new Scanner(System.in);
System.out.println("请选择服务:");
String h=sc.next();
if(h.equals("入住")){
p.inHotel();
}
if(h.equals("退房")){
p.outHotel();
}
if(h.equals("查询")){
p.find();
}
}
}
你们看下怎么错了,谢谢!
* @param args
*/
public static void main(String[] args) { String str = null;
try {
if (str.equals(null)) {
System.out.println("ok!");
}
} catch (Exception e) {
System.out.println("NULLPointException!");
}
try {
if (str == null) {
System.out.println("ok!");
} } catch (Exception e) {
System.out.println("NULLPointException!");
}
}
}結果:
NULLPointException!
ok!
然后打个断点自己调试,如果还是报错说明是数组问题。然后自己看着办
数组越界,跟是否使用equals无关。看你代码
Hotelmis p = new Hotelmis(3,21);
也就是共3层,每层21个房间
String[][] room_num = new String[3][21];System.out.println("你想住几楼");
int a = sc.nextInt();
System.out.println("你想住几号房间");
int b = sc.nextInt();当你输入a>=3 b>=21的时候,当然就越界了。
比如取room_num[3][21]得时候