如何将字符串输入转换为java中的双重?
我的代码不起作用,“ checkvalidnumber(Momentary_Value)”中存在错误,说字符串无法转换为双倍。
/**
* Add a pokémonCard to collection
*/
public void addPokémonCard()
{
final int INCREMENT = 1;
boolean repeat = true; // a boolean variable return true
// Ask user for details
String name = UI.askString("Name of PokémonCard: ");
if (name.equals("")) {
UI.println("I don't recognise that command");
addPokémonCard();
} else if (name == null ) {
UI.println("Please type a PokémonCard's name: ");
addPokémonCard();
} else {
// Check boundaries for the number of the momentary value of PokémonCard added
do {
String momentary_value = UI.askString("Momentary value: ");
// Check a suitable value of a PokémonCard
if (checkValidNumber(momentary_value)){
double momentary_value1 = Double.parseDouble(momentary_value);
// Increment the PokémonCard ID count and add to hashmap
pokémonCards.setPokémonCardId(); // Increment the id by 1
//add a PokémonCard image to display in the GUI
String imgFileName = UIFileChooser.open("Choose Image File: ");
pokémonCards.addPokémonCard(name, momentary_value1, imgFileName);
UI.println("Added");
repeat = false;
} else if (checkValidNumber(momentary_value) == false) { // Check for invalid value
UI.println("Please input a valid price!");
} else { // Check for invalid value
UI.println("Must be a number!");
}
}while (repeat); //repeat the method again if there's null input
}
}
/**
* Check a valid number for momentary value
* @return boolean false if it's an invalid number
* @param momentary_value for the price of PokemonCard
*/
public boolean checkValidNumber(double momentary_value){
boolean validNumber = false;
if (momentary_value > 0) {
validNumber = true;
}
return validNumber;
}
My code is not working and there's errors in "checkValidNumber(momentary_value)" saying that String can not convert to double.
/**
* Add a pokémonCard to collection
*/
public void addPokémonCard()
{
final int INCREMENT = 1;
boolean repeat = true; // a boolean variable return true
// Ask user for details
String name = UI.askString("Name of PokémonCard: ");
if (name.equals("")) {
UI.println("I don't recognise that command");
addPokémonCard();
} else if (name == null ) {
UI.println("Please type a PokémonCard's name: ");
addPokémonCard();
} else {
// Check boundaries for the number of the momentary value of PokémonCard added
do {
String momentary_value = UI.askString("Momentary value: ");
// Check a suitable value of a PokémonCard
if (checkValidNumber(momentary_value)){
double momentary_value1 = Double.parseDouble(momentary_value);
// Increment the PokémonCard ID count and add to hashmap
pokémonCards.setPokémonCardId(); // Increment the id by 1
//add a PokémonCard image to display in the GUI
String imgFileName = UIFileChooser.open("Choose Image File: ");
pokémonCards.addPokémonCard(name, momentary_value1, imgFileName);
UI.println("Added");
repeat = false;
} else if (checkValidNumber(momentary_value) == false) { // Check for invalid value
UI.println("Please input a valid price!");
} else { // Check for invalid value
UI.println("Must be a number!");
}
}while (repeat); //repeat the method again if there's null input
}
}
/**
* Check a valid number for momentary value
* @return boolean false if it's an invalid number
* @param momentary_value for the price of PokemonCard
*/
public boolean checkValidNumber(double momentary_value){
boolean validNumber = false;
if (momentary_value > 0) {
validNumber = true;
}
return validNumber;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要先转换为双人
You need to convert to a double first