Home > Back-end >  How to return variable in the end of static method
How to return variable in the end of static method

Time:11-12

I'm trying to make a method that read from keyboard a,d compare with an enum collection. The problem is that variable a that contains the match from the comparison I can't return it from the end of the function after the while statement the error is like the variable is not declared.

 private static Planet choosePlanet(Scanner scan) {

        System.out.println("Voici les planetes Disponible"   Arrays.toString(Planet.values()));
        List<Planet> PlanetValues = Arrays.asList(Planet.values());
        boolean exist = false;


        do {
            System.out.println("Choisis une planet");
            String choix = scanner.next();


            for (int i = 0; i < PlanetValues.size(); i  ) {
               Planet a = PlanetValues.get(i);
                if (a.toString().matches("(?i).*"   choix   ".*") == true) {

                    exist = true ;



                }
                return a;
            }


            } while (exist==false);

        return a;

        } 

CodePudding user response:

You declared a in the scope of the do, it is not visible at the point you are trying to return it.

private static Planet choosePlanet(Scanner scan) {

   System.out.println("Voici les planetes Disponible"   Arrays.toString(Planet.values()));
   List<Planet> PlanetValues = Arrays.asList(Planet.values());
   boolean exist = false;

   Planet a;
   do {
       System.out.println("Choisis une planet");
       String choix = scanner.next();


       for (int i = 0; i < PlanetValues.size(); i  ) {
          a = PlanetValues.get(i);
           if (a.toString().matches("(?i).*"   choix   ".*") == true) {

               exist = true ;



           }
           return a;
       }


       } while (exist==false);

   return a;

   }

CodePudding user response:

You would be best served by using the method for validation and doing the I/O elsewhere.

  • declare the scanner
  • print the values
  • then enter into a loop to prompt and validate the planet
Scanner scanner = new Scanner(System.in);
System.out.println("Voici les planetes Disponible"   Arrays.toString(Planet.values()));
Planet planet = null;
while (planet == null) {
     System.out.println("Choisis une planet");
     String choix = scanner.next();
     planet = choosePlanet(choix);
}
// planet found, so do something with it.

Use this method to simply search for the supplied argument and return either planet or null if not found.

private static Planet choosePlanet(String choix) {
      for (Planet planet : Planet.values()) {
         if (planet.toString().matches("(?i).*"   choix   ".*")) {
              return planet;
         }
      }
      return null;
 }

Note: Your Planet.values() were referenced in different manners in your example.

Also when testing booleans, you don't need to used ==. You can do

boolean flag = true;
if (flag) {
   // flag is true
}

if (!flag) {
   // flag is false.
}

CodePudding user response:

He says that the variable is not declared because you declared it in the for loop.
When the for loop ends, the variable 'a' does not longer exists.
You have to declare the variable 'Planet a' outside the loop and then assign it.

Also if you are new to Java I suggest you to check out the scope rules: https://www.w3schools.com/java/java_scope.asp
https://www.codecademy.com/articles/variable-scope-in-java

  •  Tags:  
  • java
  • Related