0

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.

Makri
  • 331
  • 2
  • 4
  • 15

2 Answers2

3

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);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • But it depends on the order on which its declared so the compiler wont know anything about it... – Reimeus Sep 24 '13 at 18:24
  • 2
    Also the variable `input` must be `final` in order to be used inside of `Meny`. – SamYonnou Sep 24 '13 at 18:24
  • @SamYonnou Please explain – Makri Sep 24 '13 at 18:28
  • @Makri a non-final variable cannot be referenced inside the method of an inner class – Reimeus Sep 24 '13 at 18:31
  • @Makri Reimus edited his answer to explain it. Basically for a variable to be accessible inside of an anonymous class it must be marked final – SamYonnou Sep 24 '13 at 18:34
  • Thanks alot. I moved the declaration down after the class itself and now it compiled atleast. Just got to define the switch-sequence and try calling for the class and be definite that everything works and my day is saved. Thank you! Kinda weird tho, when the book we use declares the class infront of the class itself.. – Makri Sep 24 '13 at 18:36
-1

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.

musical_coder
  • 3,886
  • 3
  • 15
  • 18