我正在尝试创建一个列表并输出我的书籍数组,让用户可以点击其中一本书来租用它。列表工作,它输出所有的书,但当我点击一本书并选择确定它给我错误"线程中的例外"主" java.lang.ClassCastException:无法将书籍强制转换为java.lang.String"。有人可以向我解释这个错误是什么以及我如何解决它?
public static void CustomerBooks() throws IOException {
Object[] Menu = {
"Option1",
"Option2",
"Option3",
"Option4"
};
String userChoice = (String) JOptionPane.showInputDialog(null, "Welcome", "Book Menu", JOptionPane.QUESTION_MESSAGE, null, Menu, null);
if (userChoice.equals("Option1")) {} else if (userChoice.equals("Option2")) {
File myFile = new File("books.txt");
Scanner inputFile = new Scanner(myFile);
Books[] bookArray = new Books[200];
String str;
while (inputFile.hasNext()) {
str = inputFile.nextLine();
String[] myData = str.split("#");
Books.addBook(bookArray, Books.getBookCount(), myData[0], myData[1], myData[2]);
}
inputFile.close();
String userChoice1 = (String) JOptionPane.showInputDialog(null, "Books List", "Books", JOptionPane.QUESTION_MESSAGE, null, bookArray, null);
JOptionPane.showMessageDialog(null, "You chose: " + userChoice1); {
System.exit(0);
}
}
答案 0 :(得分:1)
不确定问题是什么,您没有发布Book
课程。
一般来说,每个班级都有toString()
个内置功能,但这可能不一定适合你的班级。
我建议覆盖toString()
课程中的Book
函数。
这样的事情。
@override
public String toString(){
// assign result to your intended string
return result;
}
答案 1 :(得分:1)
java中有一个'$'
方法可以在任何对象上调用。但是如果你想根据toString()
的特征得到这种方法的适当行为,你应该像在Ibukun的回答中那样覆盖它。
强制转换概念用于获取更具体的Object类型。因此,由于本书不是Book
instanceof
,因此您在运行时获得String
。
答案 2 :(得分:1)
在这里,我重现了同样的行为
// check for respin
var respinCount = 0;
var respinCheck = randItem(1, 1000);
if((+respinCheck) <= 899){respinCount = 0}
else if((+respinCheck) >= 900 && (+respinCheck) <= 950){respinCount = 1;}
else if((+respinCheck) >= 951 && (+respinCheck) <= 970){respinCount = 2;}
else if((+respinCheck) >= 971 && (+respinCheck) <= 980){respinCount = 3;}
else if((+respinCheck) >= 981 && (+respinCheck) <= 990){respinCount = 4;}
else if((+respinCheck) >= 991 && (+respinCheck) <= 999){respinCount = 5;}
else if((+respinCheck) == 1000){respinCount = 10;}
// adjust stampede delay if win is >= 100
if(win >= 100){var stampede_delay = 7000;}else if (win <= 99){var stampede_delay = 10;}
setTimeout(function() {// stampede delay
if(respinCount == 0){respinCount = 3;} // for testing stampede
if(respinCount >= 1){
// turn off auto spin if active
if(auto_set == 1){ auto_set = 0; $("#autoData").val(auto_set);
if($("#auto_spin").hasClass("active")){$("#auto_spin").removeClass("active");}}
// stampede()
(function stampede(i) {
setTimeout(function () {
// announce stampede
$("#stampede_wrap").slideDown().delay(700).slideUp();
// make reel stop transparent
$("#reel_1").html('<img src="images/stop-images/transparent.png">');
$("#reel_2").html('<img src="images/stop-images/transparent.png">');
$("#reel_3").html('<img src="images/stop-images/transparent.png">');
// add reel sprite class to reels
if (!$('#reel_1').hasClass("reel-1-sprite")){$('#reel_1').addClass('reel-1-sprite')};
if (!$('#reel_2').hasClass("reel-2-sprite")){$('#reel_2').addClass('reel-2-sprite')};
if (!$('#reel_3').hasClass("reel-3-sprite")){$('#reel_3').addClass('reel-3-sprite')};
// annimate reels
var x = 0; var xx = 0; var xxx = 0; // reel increment variables
var r1a = setInterval(function(){ // annimate/stop reel_1
if(ar.length == x){x = 0;}
else {$('.reel-1-sprite').css("background-position", aru[x]); x++;}
},0);
var r2a = setInterval(function(){ // annimate/stop reel_2
if(ar.length == xx){xx = 0;}
else {$('.reel-2-sprite').css("background-position", ar[xx]); xx++;}
},0);
var r3a = setInterval(function(){ // annimate/stop reel_3
if(ar.length == xxx){xxx = 0;}
else {$('.reel-3-sprite').css("background-position", aru[xxx]); xxx++;}
},0);
// stop reels
setTimeout(function() { // stop reel_1
$("#reel_1").removeClass("reel-1-sprite");
$("#reel_1").html('<img src="images/stop-images/'+rn1sc+'.png">');
}, 800);
setTimeout(function() { // stop reel_2
$("#reel_2").removeClass("reel-2-sprite");
$("#reel_2").html('<img src="images/stop-images/'+rn2sc+'.png">');
}, 1000);
setTimeout(function() { // stop reel_3
$("#reel_3").removeClass("reel-3-sprite");
$("#reel_3").html('<img src="images/stop-images/'+rn3sc+'.png">');
}, 1200);
// adjust bank for this stampede spin
pBank = ((+bank) + (+win_value)); updateBank(uid,pBank); bank = (+pBank);
$("#creditData,#bankData").val((+bank)); $("#winData").val((+win_value));
// show win and bank add on screen
var comma_separator = $.animateNumber.numberStepFactories.separator(',');
$('#thisWin').animateNumber({ number: win_value, numberStep: comma_separator },500);
$('#credit_count_text').prop('number', (+now)).animateNumber({ number:(+bank), numberStep:comma_separator},500);
$("#creditData").val((+bank));
if (--i) stampede(i);
}, 2000)
})(respinCount); // end stampede
}; // end if(respinCount >= 1)
}, stampede_delay); // end stampede delay
不要忘记一件事,public static void main(String[] args) {
Book[] ani = new Book[]{new Book(), new Book()};
String usi = (String) JOptionPane.showInputDialog(null, "Books", "Books", JOptionPane.QUESTION_MESSAGE, null, ani, null);
}
的{{3}} Overload
会从参数中传递的数组返回选择的对象(此处为showInputDialog
)。
您当然希望从返回的Book
分配toString()
或getName()
Object