Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
Use user input for switch

For my Java class, we have to detect user input of a Pokemon ability and give an output of the description of said ability using a Switch. I have the scanner to detect an input, but when I type anything other than the case numbers then it just gives me an error. I don't know how to make it detect an ability like 'Chlorophyll,' and then have it use the output to the console.

Here is my code:

package pokemonAbility;

import java.util.Scanner;

public class PokemonAbility {

public static void main(String[] args) {
    System.out.println("Enter Pokemon ability:");
    Scanner scanner = new Scanner(System.in);
    int abilityPkmn = scanner.nextInt();
    String abilityString;
    switch (abilityPkmn) {
    case 1: abilityString = "Chlorophyll";
        System.out.println("Boosts the Pokémon's Speed stat in harsh sunlight.");
        break;
    case 2: abilityString = "Guts";
        System.out.println("Boosts the Pokemon's Attack stat when it has a status condition.");
        break;
    case 3: abilityString = "Cacophony";
        System.out.println("The Pokemon is immune to sound-based moves.");
        break;
    case 4: abilityString = "Damp";
        System.out.println("Prevents the use of Explosion and Self-Destruct.");
        break;
    case 5: abilityString = "Drought";
        System.out.println("Activates harsh sunlight when the Pokémon enters battle.");
        break;
    default: abilityString = "Invalid ability";
        break;

    }
    
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

public static void main(String[] args) {
    System.out.println("Enter Pokemon ability:");
    Scanner scanner = new Scanner(System.in);
    String abilityPkmn = scanner.next(); //here no int because u are taking string 
    String abilityString; //u do not need this anymore!
    switch (abilityPkmn) {
    case "Chlorophyll":
        System.out.println("Boosts the Pokémon's Speed stat in harsh sunlight.");
        break;
    case "Guts":
        System.out.println("Boosts the Pokemon's Attack stat when it has a status condition.");
        break;
    case "Cacophony":
        System.out.println("The Pokemon is immune to sound-based moves.");
        break;
    case "Damp":
        System.out.println("Prevents the use of Explosion and Self-Destruct.");
        break;
    case "Drought":
        System.out.println("Activates harsh sunlight when the Pokémon enters battle.");
        break;
    default:
        System.out.println("here comes any default text which is not included in all cases and there is no need for break;");

    }
}
over 4 years ago · Santiago Trujillo Report

0

I have noticed that you haven't used the print statement in the default case. Use the println statement to get the output on the standard output device.

vote my answer if this helps.

over 4 years ago · Santiago Trujillo Report

0

I think you will need to identify these variables and then write in the code or action performed in a new statement.

Try putting the statements into an array then call them in a method.

Perhaps set up a new class called abilities and create some values. When you call them in a new method you can super class those abilities.

IvonH on gitHub you can see what I have done for a simple army game.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!