Search code examples
javastaticargs

Why i cant use static method from my main?


i'm making a diary program.

  1. In Diary class i made method called "addStudent" But i cant figure out why it is invisible in my main?

EDIT i added code, sorry for just one block and not 3 but lombok imports are causing issues with formatting

EDIT2 i deleted remaining questions

public class Main {
    public static void main(String[] args) {

    }
}

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@Data

public class Diary {


        List<Student> listOfStudents = new ArrayList<>();



    public static void addStudent(Student... students){
        Student student = new Student(Student.askAboutGrades(), Student.askAboutIndexNumber(), Student.askAboutName(), Student.askAboutSurname());

    }
}



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

@AllArgsConstructor
@Data

public class Student {
    List<Double> grades = new ArrayList<>();
    String indexNumber;
    String name;
    String surname;

    public  static String askAboutName() {
        System.out.println("Type the name");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        while
        (!(name.matches("[a-zA-Z]+"))) {
            System.out.println("incorrect name, try again");
            name = scanner.nextLine();
        }
        System.out.println("Name seems good");
        return name;
    }

    public static String askAboutSurname() {
        System.out.println("Type the surname");
        Scanner scanner = new Scanner(System.in);
        String surname = scanner.nextLine();
        while (!(surname.matches("[a-zA-Z]+"))) {
            System.out.println("incorrect surname, try again");
            surname = scanner.nextLine();
        }
        System.out.println("surname seems good");
        return surname;
    }

    public static String askAboutIndexNumber() {
        System.out.println("Type the index number (6 digits!)");
        Scanner scanner = new Scanner(System.in);
        String indexNumber = scanner.nextLine();
        while (!indexNumber.matches("\\d{6}")) {
            System.out.println("incorrect index number, try again");
            indexNumber = scanner.nextLine();
        }
        System.out.println("index seems good");
        return indexNumber;
    }

    public static List<Double> askAboutGrades() {
        System.out.println("Type the grades,separated by comma (\",\")");//todo check if works and set limiter from 2 to 5.5
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();

        String[] arrayOfGrades = input.split(",");
        List<String> listOfGrades = new ArrayList<>(Arrays.asList(arrayOfGrades));
        List<Double> listOfGradesDouble = new ArrayList<>();
        String temp;
        for (int i = 0; i < listOfGrades.size(); i++) {
            temp = listOfGrades.get(i);
            listOfGradesDouble.add(Double.parseDouble(temp));
        }
        return listOfGradesDouble;
    }
}

Github: https://github.com/Karol011/SDA/tree/master/zadania/src/main/java/Kolekcje/ListyKolekcje/Diary


Solution

  • Whoah. I'll try: 1. How are you calling it? Should be somehing like

    Diary.addStudent();
    
    1. Ehmmm. How do you plan to access non-static list from static method? And why it's static?

    2. Sounds weird.

    3. Figure out what is static and when to use it. In most cases you don't need it. Also, I would remove all lombok and would try to sort thing out using pure java. IMHO.

    Powodzenia)