1

it's my first time asking something on here, be gentle on me if I fail to consider everything. I'll try my best.

To the actual problem. I just recently started programming Java, so this will probably be something basic, although I wasn't able to figure out what the problem is by checking out similar questions.

I'd wager this error is thrown due to a scoping issue, but I can't tell for certain, or fix it even.

public static void main(String[] args){

    Artikel a = new Artikel();

    Scanner scanner = new Scanner(System.in);

    if (args.length !=8){
        System.out.println("Bitte geben Sie die folgenden Werte, getrennt mit einem ',' ein: Artikelname, Artikelnummer, Mengeneinheit, Packungsgröße, Mindestbestand, Bestellmenge, Lagerbestand, Hersteller");        
        String eingabe = scanner.next();
        String parts[] = eingabe.split(",");
        String string1 = parts[0];
        String string2 = parts[1];
        String string3 = parts[2];
        int int1 = Integer.parseInt(parts[3]);
        int int2 = Integer.parseInt(parts[4]);
        int int3 = Integer.parseInt(parts[5]);
        int int4 = Integer.parseInt(parts[6]);
        String string4 = parts[7];

        a.Artikel(string1, string2, string3, int1, int2, int3, int4, string4);
    }
    else{
        a.Artikel(args[0], args[1], args[2], Integer.parseInt(args[3]), Integer.parseInt(args[4]), Integer.parseInt(args[5]), Integer.parseInt(args[6]), args[7]);
    }

That's the bit the error refers to, specifically the lines in which I try to access a.Artikel(...). Let me know if the code bit doesn't suffice.

edit:

public class Artikel{
private String Artikelname;
private String Artikelnr;
private String menge;
private int packgroesse;
private int mindestZahl;
private int bestell;
private int lager;
private String Hersteller;

//Defaultkonstruktor

public Artikel(){
    this.Artikelname = "Bez1";
    this.Artikelnr = "0";
    this.menge = "Beispiel";
    this.packgroesse = 0;
    this.mindestZahl = 0;
    this.bestell = 0;
    this.lager = 0;
    this.Hersteller = "Muster";
}

//Konstruktor

public Artikel(String Artikelname, String Artikelnr, String menge, int packgroesse, int mindestZahl, int bestell, int lager, String Hersteller){
    this.Artikelname = Artikelname;
    this.Artikelnr = Artikelnr;
    this.menge = menge;

    if(packgroesse < 0 ){
        System.out.println("Die Packungsgroesse kann nicht negativ sein! Der Wert wurde auf den Standardwert (10) gesetzt.");
    }
    else{
    this.packgroesse = packgroesse;
    }

    if(mindestZahl < 0 ){
        System.out.println("Der Mindestbestand kann nicht negativ sein! Der Wert wurde auf den Standardwert (30) gesetzt.");
    }
    else{
    this.mindestZahl = mindestZahl;
    }

    if(bestell < 0 ){
        System.out.println("Die Bestellmenge kann nicht negativ sein! Der Wert wurde auf den Standardwert (50) gesetzt.");
    }
    else{
    this.bestell = bestell;
    }

    if(lager < 0 ){
        System.out.println("Der Lagerbestand kann nicht negativ sein! Der Wert wurde auf den Standardwert (50) gesetzt.");
    }
    else{
    this.lager = lager;
    }

    this.Hersteller = Hersteller;
}
  • The variable `a` is an instantiated `Artikel`. What is the definition of `Artikel`? Generally either you have some setter method on the class `Artikel` that accepted the parameters or you would not instantiate in the line `Artikel a = new Articel();` (just declare it) and then in the other lines do `a = new Artikel(...)` assuming the parameters are passed to the constructor. – KevinO Apr 12 '18 at 16:20
  • That actually did the trick and it seems intelligible to do it that way. But would you mind explaining how that makes a difference to the compiler? Is it not possible to initialise an object to hard coded values (standard constructor) and reassign values through an overloaded constructor? –  Apr 12 '18 at 16:32
  • @plc constructor are not method in java. You cannot call them like `a.Artikel()` like this. Whenever you call constructor , it creates a fresh new object. – Number945 Apr 12 '18 at 16:36
  • Short answer: no. An object's constructor is only called once, at the time of instantiation [When does the Constructor get called in java](https://stackoverflow.com/questions/9567341/when-does-the-constructor-gets-called-in-java). If you wish to have default values assigned via a default constructor, you would then need to add so-called `setter` methods to update the values of the instance variables. – KevinO Apr 12 '18 at 16:38
  • Thanks a bunch. All the linked post and your explanation cleared it up. It's quite confusing coming from C. Especially to make all the proper connections from structures to objects. –  Apr 12 '18 at 16:46

3 Answers3

2

If the class Artikel is defined such that it has a constructor that accepts the various parameters such as:

public class Artikel {
  ...
  public Artikel (String s1, String s2, String s3, int i1, int i2, int i3, int i4, String s4) {
   // set the various values in the instance variables
   ...
  }
}

then the solution based upon the code above would be something like:

public static void main(String[] args){

// CHANGE: just define
Artikel a;

Scanner scanner = new Scanner(System.in);

if (args.length !=8){
    System.out.println("Bitte geben Sie die folgenden Werte, getrennt mit einem ',' ein: Artikelname, Artikelnummer, Mengeneinheit, Packungsgröße, Mindestbestand, Bestellmenge, Lagerbestand, Hersteller");        
    String eingabe = scanner.next();
    String parts[] = eingabe.split(",");
    String string1 = parts[0];
    String string2 = parts[1];
    String string3 = parts[2];
    int int1 = Integer.parseInt(parts[3]);
    int int2 = Integer.parseInt(parts[4]);
    int int3 = Integer.parseInt(parts[5]);
    int int4 = Integer.parseInt(parts[6]);
    String string4 = parts[7];

    // CHANGE: instantiate here
    a = new Artikel(string1, string2, string3, int1, int2, int3, int4, string4);
}
else{
    // CHANGE: instantiate here
    a = new Artikel(args[0], args[1], args[2], Integer.parseInt(args[3]), Integer.parseInt(args[4]), Integer.parseInt(args[5]), Integer.parseInt(args[6]), args[7]);
}

It depends on how the Artikel class is defined, of course.

KevinO
  • 4,303
  • 4
  • 27
  • 36
1
Artikel a;
if (args.length != 8 ) {
    ...
    String eingabe = scanner.nextLine();
    String[] parts = eingabe.split(", *", 8); // At most 8.


    a = new Artikel(string1, string2, string3, int1, int2, int3, int4, string4);
}
else{
    a = new Artikel(args[0], args[1], args[2], Integer.parseInt(args[3]),
        Integer.parseInt(args[4]), Integer.parseInt(args[5]), Integer.parseInt(args[6]),
        args[7]);
}

There is the variable declaration of a, and filling it with a value of new Artikel(...), a new object.

Also Scanner.next() reads one token, whereas nextLine an entire line.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

How about if we do like this :

public static void main(String[] args){

    Artikel a;

    Scanner scanner = new Scanner(System.in);

    if (args.length !=8){
        System.out.println("Bitte geben Sie die folgenden Werte, getrennt mit einem ',' ein: Artikelname, Artikelnummer, Mengeneinheit, Packungsgröße, Mindestbestand, Bestellmenge, Lagerbestand, Hersteller");        
        String eingabe = scanner.next();
        String parts[] = eingabe.split(",");
        String string1 = parts[0];
        String string2 = parts[1];
        String string3 = parts[2];
        int int1 = Integer.parseInt(parts[3]);
        int int2 = Integer.parseInt(parts[4]);
        int int3 = Integer.parseInt(parts[5]);
        int int4 = Integer.parseInt(parts[6]);
        String string4 = parts[7];

        a = new Artikel(string1, string2, string3, int1, int2, int3, int4, string4);
    }
    else{
        a = new Artikel(args[0], args[1], args[2], Integer.parseInt(args[3]), Integer.parseInt(args[4]), Integer.parseInt(args[5]), Integer.parseInt(args[6]), args[7]);
    } 

I think you should see how to use constructor to create object in java. Look here

See once you create an object using Artikel a = new Artikel() you cannot call constructor again and expect it to initialize the same object with new values. Also constructor is always called with new operator and it creates a fresh new object. Also constructor is not method in Java.Hence you cannot call like a.Artikel(...). Look Here

Number945
  • 4,631
  • 8
  • 45
  • 83