[英]When adding elements to string array from user replace it by null! java
我有一个包含整数的2D字符串数组。
当我要求用户添加包含字符串和整数的元素时,程序将显示用户添加的内容-但将其替换为null
。
这是代码:
case 3:
count++;
System.out.println("Enter the type of the car: ");
car[count][0] = ky.next();
System.out.println("Enter the model of the car: ");
String addM = ky.next(); // integer
car[count][1] = addM;
System.out.println("Enter the price of the car: ");
String addP = ky.next(); // integer
car[count][1] = addP;
System.out.println("Enter the condition of the car: ");
String addC = ky.next();
car[count][3] = addC;
System.out.println("Enter the odemeter of the car (if it is new put it 0): ");
String addO = ky.next(); // integer
car[count][1] = addO1;
income -= Integer.parseInt(addP);
break;
该程序显示:
Ford 2012 55000 new 0
Toyota 2012 60000 new 0
Honda 2011 25000 Used 6000
null null null null null
最后一行是用户应添加的数据!
非常感谢您的帮助。
干:=不要重复自己:
String [] attribute = new String [] {"type", "model", "price", "condition", "odemeter"};
for (int i = 0; i < attribute.length; ++i)
{
System.out.println ("Enter the "+attribute[i] +" of the car: ");
car[count][i] = ky.nextInt ();
}
如果该属性确实是一个int,而ky是键盘的缩写,则为System.in上的Scanner,请使用ky.nextInt();。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.