0
public class Eksamensøving2 {
    private static String Marcus;
    private static String Peter;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

     Konto kontoer [] = new Konto[2];
     kontoer [0] = new Konto(100,"Marcus");
     kontoer [1] = new Konto(200,"Peter");
         System.out.println("Saldo er: " + kontoer[1].getSaldo()+"kr på kontoen til " + kontoer[0].getEier());

         Konto minKonto = new Konto(); 
         minKonto.SettInnPenger();    

           }
    }

This is the main class

 public class Konto {
    double saldo;
    String eier;
    private Scanner input;

   public Konto (double innsaldo, String inneier) 
    {
        input = new Scanner(System.in);
        saldo = innsaldo;
        eier = inneier;
    }

    public double getSaldo () {
        return saldo;
    }
    public String getEier () {
        return eier;
    }
            public void SettInnPenger (){
        int settInn;
        String kontoSettInn;
        System.out.println("Skriv in navnet på kontoen;");
        kontoSettInn = input.nextLine();

        if (kontoSettInn == eier) {
            System.out.println("Skriv in beløpet du vil sette inn:");
            settInn = input.nextInt();
            saldo = saldo + settInn;
            System.out.println("beløpet på kontoen er:"+ saldo);
        }
        else 
            System.out.println("denne kontoen finnes ikke.");

            }
}

I'm having problems with creating and assigning a object to the konto class. minKonto should call the settInnPenger method, but for some reason it gets an error. Will appriciate all the help i get.

1 Answers1

0

You cannot access that array because it is valid only in main method which is in another class. Read this: http://www.dummies.com/how-to/content/local-variables-in-java.html

Konto minKonto = new Konto(); line does not compile because you don't have a default constructor in Konto class. You need innsaldo and inneier parameters to create an object. Read this: http://www.dummies.com/how-to/content/how-to-use-a-constructor-in-java.html

In SettInnPenger method you compare two strings with == operator which is a bad thing. Instead use equals method. Read this: How do I compare strings in Java?

At the beginning of Eksamensøving2 class you define Marcus and Peter strings but you do not assign them any values. I think you should do this:

private static String Marcus = "Marcus";
private static String Peter = "Peter";

And now you can create konto objects like this:

 kontoer [0] = new Konto(100,Marcus);
 kontoer [1] = new Konto(200,Peter);

Finally giving your best friends name to a variable is not a good thing. Read this: http://www.oracle.com/technetwork/java/codeconventions-135099.html

Community
  • 1
  • 1
b4da
  • 3,010
  • 4
  • 27
  • 34