How to resolve error: illegal Static declaration

Asked

Viewed 165 times

0

I have 2 classes that work well independently of each other. I joined the 2 in one and got 3 build errors, the error message says:

 ilegal static declaration.

how to solve error on lines 42, 58 6 69.

The code to follow:

import java.util.*;

public class nesTudo
{
       class UserInfo 
       {
           String user; String pass; String secretCode;

   ArrayList <UserInfo> InfoList = new ArrayList<UserInfo> ();  

        public void userInternalDatabase (UserInfo info) 
        {
        this.user = info.user;
        this.pass = info.pass;
        this.secretCode = info.secretCode;
        }
    public void addUser(String i, String j, String k) 
        {
           UserInfo newUser = new UserInfo();
           newUser.user = i;
       newUser.pass = j;
           newUser.secretCode = k;
           InfoList.add(newUser);
        }

         public Object findUsername(String a)  
         {    
              for (int i=0; i <InfoList.size(); i++) 
              {
                if (InfoList.get(i).user.equals(a))
                {
                    return "This user already exists in our database.";
                    }
                  }
                    return "NÃO EXISTE ESSE UTILIZADOR."; 
              } 
   }

   class DadosGuardados
 {

        private static int lerNumero(Scanner kb, String mensagem, String mensagemErro) 
    {
        while (true) 
        {
               System.out.println(mensagem);
                try 
                {
                return Integer.parseInt(kb.nextLine());
                } 
        catch (NumberFormatException e) 
            {
               System.out.println(mensagemErro);
            }
       }
    }

      private static boolean lerSimNao(Scanner kb, String mensagem, String mensagemErro) 
      {
          while (true) 
          {
             System.out.println(mensagem);
             String x = kb.nextLine();
             if (x.equalsIgnoreCase("S")) return true;
             if (x.equalsIgnoreCase("N")) return false;
             System.out.println(mensagemErro);
           } 
      }
            public static void main(String[] args) 
            {
                Scanner kb = new Scanner(System.in);

                System.out.println("Bem-vindo, utilizador.");
                boolean maisRecarga = true;
                   while (maisRecarga) 
               {
                          int recarga = lerNumero(kb, "Introduza o número da recarga: ", "Isso que você digitou não era um número. Por favor, tente novamente.");
                        System.out.println("Você digitou " + recarga + ".");
                        maisRecarga = lerSimNao(kb, "Tem mais recarga para registar?\nResponda `S´ para continuar ou `N´ para terminar: ", "Era para você responder S ou N! Por favor, tente novamente.");  

                   }
                System.out.println("Obrigado, até o próximo registro.");
            }
 }
} 
  • 2

    We do not have the numbers, have to inform which are the lines. but already put forward that should be to take the static how the error is reporting on them. The problem may be further down. (Putting things together) programming is not usually just copying and pasting codes without understanding what they do. A class that has Tudo in the name seems suspicious tb. The code seems very confusing.

  • @bigown mentioned lines 42, 58 and 69.

  • 1

    Where? I don’t see it. There’s only numbers that mean nothing to us.

  • @bigown I accept that the comic is confusing except to be guessed.

  • So it’s proven to be a class that does more than it should even :P

1 answer

1

For a Sub-Class to have static methods, it needs to be static as well!

Its Sub-class DadosGuardados has 2 static methods (in addition to main) and she’s not static!

Try to state it as follows :

static class DadosGuardados

I emphasize what was said in the comments !

programming is not usually just copying and pasting codes without understanding what they do

At first this amendment should work !

But I suggest you remove static methods, and create instances of Sub-classes!

Leave your method public static void main(String[] args) in the main class.

  • the change functioned.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.