Error . class expected

Asked

Viewed 418 times

0

The program is supposed to ask for a number between 1 and 12. (I haven’t gotten to the part of certifying that it’s between 1 and 12 but this is the easiest.However I’m supposed to write the code through functions (not that I see much sense being for functions in such an exercise). But by my reasoning does not work. I leave it there. Besides someone knows some page or can explain to me how the types of function works? of the kind I only know public Static void and the public Static final and public Static String(if there really is).

public static void main(String[] args)
{
    System.out.println("Idique um número entre 1 e 12");
    int num = scanner.nextInt();
    System . out . println(num + "Correponde a :" + mesesdoano);
}
public static String mesesdoano()
{
    String meses = ("janeiro  fevereiromarço    abril    maio     junho    julho    agosto   setembro outubro  novembro dezembro "); //vai relacionar o número introduzido com a formula //
    Substring=(int (numero*9)-9,int numero*9);// vai buscar á string meses a palavra correspondente de acordo com o número.Cada mês ocupa sempre nove espaços para através dessa "formula" ir buscar a palavra.
    return substring;
  • Your doubt is about what it is public static void main(String[] args) or what the code problem (which alias, has many)?

  • This line Substring=(int (numero*9)-9,int numero*9); is not valid. I was trying to do what specifically ?

  • @Article is about the classes of the functions and also about the code.

  • I’m sorry @Isac vo add some comments to the code

  • Your doubt is unclear, please be more specific.

  • @The first is that the code does not accomplish what is intended. The second is that I don’t know what to put after public Static ____ according to what I want to do in this function and if there is any "list" that helps.

  • I believe the mistake .class expected, be why you are assigning a int It’s a class, on the line Substring=(int (numero*9)-9,int numero*9);

  • @Guilhermecostamilam then what should I assign? is because without the int also does not give

  • I can make an answer with a method that does what you want, but in a different way

  • If you think you’re going to help me I really appreciate it. It’s because I’m very new to this and I’m still learning the basics.

  • How you are compiling and running, @Phil ?

Show 6 more comments

2 answers

0

I created a function that returns a String (month of the year), works as follows:

public String mesesDoAno(int i) {
    //Crio uma variável ArrayList
    ArrayList<String> meses = new ArrayList<>();
    //Adiciono os meses do ano à variável 
    meses.add("janeiro");
    meses.add("fevereiro");
    meses.add("março");
    meses.add("abril");
    meses.add("maio");
    meses.add("junho");
    meses.add("julho");
    meses.add("agosto");
    meses.add("setembro");
    meses.add("outubro");
    meses.add("novembro");
    meses.add("dezembro");
    //Retorno a String (mes) correspondente ao número que o usuário escolheu - 1, porque o array começa no índice 0, ou seja, 0 = janeiro, 1 = fevereiro...
    return meses.get(i-1);
}

To call just send one int:

mesesDoAno(mesQueOUsuarioDigitou);

The way you did, I’m not sure what you wanted but I think if you change that line:

Substring=(int (numero*9)-9,int numero*9);

For that reason:

String substring = meses.Substring(int (numero*9)-9,int numero*9);

Assuming, of course, the rest of the code is correct, it will work

  • I would have used a vector itself, a map, or even switch case.

  • Ah, to use in main, the method mesesDoAno would need to be static

  • actually do not even need to create the method just put the content inside the main and use meses.get(i-1), being i the value the user entered, are also other possibilities

0

I ended up being able to solve in a very simple way. But it was based on the first problem I posed. Ended up like this and runs without problems:

    System.out.println("Indique um número entre 1 e 12");
    int num = scanner.nextInt();        
    final String meses = ("janeiro  fevereiromarço    abril    maio     junho    julho    agosto   setembro outubro  novembro dezembro ");
    String mesesdoano =meses.substring ((num*9)-9,num*9);
    System . out . println("Correponde a :" + mesesdoano);

Browser other questions tagged

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