-1

I am new to Java and I am trying to print the student numbers and numbers (cijfer in this case) on 1 line. But for some reason I get weird signs etc. Also when I'm trying something else I get a non-static context error. What does this mean and how does this exactly work?

Down here is my code:

import java.text.DecimalFormat;
import java.util.Arrays;


public class Student {
public static final int AANTAL_STUDENTEN = 50;
public  int[] studentNummer = new int[AANTAL_STUDENTEN];
public String[] cijfer;


public int[] StudentNummers() {

    for (int i = 0; i < AANTAL_STUDENTEN; i++) {
        studentNummer[i] = (50060001 + i);

    }
    return studentNummer;
}

 public  String[] cijfers(){

    for (int i = 0; i < AANTAL_STUDENTEN; i++) {
        DecimalFormat df = new DecimalFormat("#.#");

        String cijferformat = df.format(Math.random() * ( 10 - 1 ) + 1);
        cijfer[i++] = cijferformat;
    }
    return cijfer;
}

public static void main(String[] Args) {
    System.out.println("I cant call the cijfer and studentnummer.");
}
}

Also I'm aware my cijfer array is giving a nullpointer exception. I still have to fix this.

Assafs
  • 3,257
  • 4
  • 26
  • 39
Axel
  • 81
  • 2
  • 9
  • I don't see where you are trying to print anything. Is this the right code? – 001 Oct 09 '17 at 11:33
  • To add to @JohnnyMopp what kind of weird signs do you get? – Dinh Oct 09 '17 at 11:34
  • As for the `static` errors, see: [Instantiating object from inside the main of that class in Java](https://stackoverflow.com/q/7892622/669576) – 001 Oct 09 '17 at 11:37
  • Especially with a main that only print "_I cant call the cijfer and studentnummer._" – AxelH Oct 09 '17 at 11:41
  • @JohnnyMopp I am trying to print all the studentnummer and cijfer variables that are stored into the array. But if i make them static like the solution of oleg.cherednik down here then i get these signs: [Ljava.lang.String;330bedb4 [I2503dbd3 – Axel Oct 09 '17 at 12:52
  • 1
    @Axel [What's the simplest way to print a Java array?](https://stackoverflow.com/q/409784/669576) – 001 Oct 09 '17 at 13:01
  • @Axel in Java all types (except of simple ones) are extends from `Object` class. This class contains `toString()` method. And when you have an array `cijfer` and you try to print it with `System.out.println(cijfer)`, this equals to `System.out.println(cijfer.toString())` which pring string for **object reference cijfer to array instance**. You have to use `Arrays.toString(cijfer)` to pring array content. See my example below, I have added couple linse about it. – Oleg Cherednik Oct 10 '17 at 06:46

4 Answers4

0

I am not java developer but try

System.out.print
0

Just make all your methods and class variables as static. And then you have access to them from main method. Moreover you have got some errors in code:

public class Student {
    public static final int AANTAL_STUDENTEN = 50;
    // NOTE: static variables can be moved to local ones

    // NOTE: only static method are available from static context
    public static int[] StudentNummers() {
        int[] studentNummer = new int[AANTAL_STUDENTEN];

        for (int i = 0; i < AANTAL_STUDENTEN; i++)
            studentNummer[i] = 50060001 + i;

        return studentNummer;
    }

    // NOTE: only static method are available from static context
    public static String[] cijfers() {
        // NOTE: it is better to use same `df` instance
        DecimalFormat df = new DecimalFormat("#.#");
        String[] cijfer = new String[AANTAL_STUDENTEN];

        for (int i = 0; i < AANTAL_STUDENTEN; i++)
            // NOTE: remove `i++`, because we have it in the loop
            cijfer[i] = df.format(Math.random() * (10 - 1) + 1);

        return cijfer;
    }

    // NOTE: this is `static` method, therefore it has access only to static methods and variables
    public static void main(String[] Args) {
        String[] cijfer = cijfers();
        int[] studentNummer = StudentNummers();

        // TODO you can pring two arrays one element per line
        for(int i = 0; i < AANTAL_STUDENTEN; i++)
            Sytem.out.println(cijfer[i] + '-' + studentNummer[i]);

       // TODO as alternative, you can print whole array
       System.out.println(Arrays.toString(cijfer));
       System.out.println(Arrays.toString(studentNummer));
    }
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

You could loop around System.out.print. Otherwise make your functions static to access them from main. Also initialize your cijfer array.

Asgeirr
  • 82
  • 10
0

Besides the things I noted in the comments, your design needs work. You have a class Student which contains 50 studentNummer and cijfer members. Presumably, a Student would only have one studentNummer and and one cijfer. You need 2 classes: 1 for a single Student and one to hold all the Student objects (StudentBody for example).

public class StudentBody {
    // An inner class (doesn't have to be)
    public class Student {
        // Just one of these
        public  int studentNummer;
        public String cijfer;

        // A constructor. Pass the student #
        public Student(int id) {
            studentNummer = id;
            DecimalFormat df = new DecimalFormat("#.#");
            cijfer = df.format(Math.random() * ( 10 - 1 ) + 1);
        }

        // Override toString
        @Override
        public String toString() {
            return studentNummer + " " + cijfer;
        }
    }

    public static final int AANTAL_STUDENTEN = 50;
    public Student students[] = new Student[AANTAL_STUDENTEN];

    // StudentBody constructor
    public StudentBody() {
        // Create all Students
        for (int i = 0; i < AANTAL_STUDENTEN; i++) {
            students[i] = new Student(50060001 + i);
        }
    }

    // Function to print all Students
    public void printStudents(){
        for (int i = 0; i < AANTAL_STUDENTEN; i++) {
            System.out.println(students[i]);
       }
    }

    public static void main(String[] Args) {
        // Create a StudentBody object
        StudentBody allStudents = new StudentBody();
        // Print
        allStudents.printStudents();
    }
}
001
  • 13,291
  • 5
  • 35
  • 66