Pregunta

I got this part of code:

import java.util.*;
import java.io.*;
public class Oblig2 {
Meny menyen = new Meny();
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
int menyvalg=0;


//Lager filen ved navn Fugleobservasjoner
try{
PrintWriter fil=new PrintWriter(new FileWriter("Fugleobservasjoner.txt"));
} catch (IOException e) {
    System.out.println("Filen finnes ikke");
    } 

//Selve menyen til programmet i en egen klasse.
class Meny {
    int menyvalg=0;
    void Meny() {
System.out.println("====== Meny for registrering av fugleobservasjoner =====");

System.out.println("\n1. Registrer en fugleobservasjon");
    System.out.println("2. Skriv ut alle fugleobservasjoner av en type");
System.out.println("3. Skriv ut alle fugleobservasjoner på ett bestemt sted");
System.out.println("4. Avslutt systemet");

System.out.println("\nVennligst velg et tall: ");
menyvalg = input.nextInt();
    }
}



//Dette er kommandoene for valget som gjøres i menyen.


    }
}

But I keep getting errors when compiling as well as a pointer to the part where I declare the Meny-class. It says Cannot find Symbol, which suggest that the variable isn't declared, but how should I do so then? (I am fully aware that there might be better ways of coding this, but this is what I got so far.) EDIT: Edited the code since I just saw that it didn't include everything... Sorry about that.

¿Fue útil?

Solución

Define the class Meny before attempting to use it, i.e move

class Meny {
  ...
}

ahead of

Meny menyen = new Meny();

The order in which classes are defined matters to the compiler. In addition declare the Scanner instance final so it can be referenced in the inner class

final Scanner input = new Scanner(System.in);

Otros consejos

If that's your complete code, then you're missing } to close void Meny(), another } to close class Meny, and another } to close main(). Also, do you have main() placed inside a larger class? Java doesn't allow functions to be outside classes.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top