-1

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;
}
4
  • 1) what is the content of your method checkValidNumber(momentary_value) ? 2) could you also print the value of momentary_value? easier for you to debug from there Commented May 31, 2022 at 2:27
  • 1
    @fauzimh the method is written in the question Commented May 31, 2022 at 2:33
  • 4
    checkValidNumber is a String, public boolean checkValidNumber(double momentary_value){ declares itself has requiring a double parameter, yet you try and call it using checkValidNumber(momentary_value). You've already shown you know how to convert a String to a double using Double.parseDouble(momentary_value), so now I'm trying to figure out what the actual problem is 🤷‍♂️ Commented May 31, 2022 at 2:34
  • Just a side note - it's not a good idea to use non-ASCII characters in identifiers such as method names, class names and so on. It makes it more difficult for people to work on your code, if they don't have the right kind of keyboard. Commented May 31, 2022 at 2:43

1 Answer 1

1

You need to convert to a double first

checkValidNumber(Double.parseDouble(momentary_value))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.