-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;
}
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
Sherry
  • 1
  • 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 – fauzimh May 31 '22 at 02:27
  • 1
    @fauzimh the method is written in the question – Scary Wombat May 31 '22 at 02: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 ‍♂️ – MadProgrammer May 31 '22 at 02: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. – Dawood ibn Kareem May 31 '22 at 02:43

1 Answers1

1

You need to convert to a double first

checkValidNumber(Double.parseDouble(momentary_value))
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64