S
S
Sawayadi2020-09-26 19:54:42
Java
Sawayadi, 2020-09-26 19:54:42

How to perform code array selection with a class?

I need to fulfill the array selection condition in the Main.java code: -
Select people with a certain name (the second word in the first value);
-Output an array with today's month;
Main.java code:

package com.company;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

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

        Znak men = Znak.input();

        Date date = new SimpleDateFormat("dd.MM.yyyy").parse(men.getDateBirthday());
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int monthBirthday = cal.get(Calendar.MONTH) + 1;
        int dayBirthday = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println("Знак зодиака: " + men.getFIO());
        Znak.checkZnak(monthBirthday, dayBirthday);

        Znak [] String = new Znak [4];
        String[0] = new Znak("Тягай Максим", "12.06.1990");
        String[1] = new Znak("Титов Олексей", "26.09.1985");
        String[2] = new Znak("Рымар Анастасия", "04.06.1998");
        String[3] = new Znak("Мирна Маша", "22.04.2002");
        for (int i = 0; i < 4; i++) {
            Scanner in = null;
            System.out.print("Ведите какое имя вы хотите вывести: ");
            String goal = in.nextLine();
            if (String[i] == goal)
            {
                System.out.println(String[i]);
            }
        }
    }
}

Class code:
package com.company;

import java.util.Scanner;

public class Znak {
    private String FIO;
    private String dateBirthday;

    public Znak(String FIO, String dateBirthday) {
        this.FIO = FIO;
        this.dateBirthday = dateBirthday;
    }

    public Znak() {
    }

    public String getFIO() {

        return FIO;
    }

    public void setFIO(String FIO) {

        this.FIO = FIO;
    }

    public String getDateBirthday() {

        return dateBirthday;
    }

    public void setDateBirthday(String dateBirthday) {

        this.dateBirthday = dateBirthday;
    }

    public static Znak input() {
        Scanner in = new Scanner(System.in);
        System.out.println("Введите фамилию и имя: ");
        String FIO = in.nextLine();
        System.out.println("Введите дату рождения (дд.мм.гггг): ");
        String date = in.nextLine();

        return new Znak(FIO, date);
    }

    public static void checkZnak(int monthBirthday, int dayBirthday) {
        switch (monthBirthday) {
            case 1:
                if (dayBirthday >= 21)
                    System.out.println("Водолей");
                System.out.println("Козерог");
                break;
            case 2:
                if (dayBirthday >= 21)
                    System.out.println("Рыбы");
                System.out.println("Водолей");
                break;
            case 3:
                if (dayBirthday >= 21)
                    System.out.println("Овен");
                System.out.println("Рыбы");
                break;
            case 4:
                if (dayBirthday >= 21)
                    System.out.println("Телец");
                System.out.println("Овен");
                break;
            case 5:
                if (dayBirthday >= 21)
                    System.out.println("Близнецы");
                System.out.println("Телец");
                break;
            case 6:
                if (dayBirthday >= 22)
                    System.out.println("Рак");
                System.out.println("Близнецы");
                break;
            case 7:
                if (dayBirthday >= 23)
                    System.out.println("Лев");
                System.out.println("Рак");
                break;
            case 8:
                if (dayBirthday >= 24)
                    System.out.println("Дева");
                System.out.println("Лев");
                break;
            case 9:
                if (dayBirthday >= 24)
                    System.out.println("Весы");
                System.out.println("Дева");
                break;
            case 10:
                if (dayBirthday >= 24)
                    System.out.println("Скорпион");
                System.out.println("Весы");
                break;
            case 11:
                if (dayBirthday >= 23)
                    System.out.println("Стрелец");
                System.out.println("Скорпион");
                break;
            case 12:
                if (dayBirthday >= 22)
                    System.out.println("Козерог");
                System.out.println("Стрелец");
                break;
            default:
                System.out.println("Такого месяца не существует!!!");
                break;
        }
    }
}

I started writing
for (int i = 0; i < 4; i++) {
            Scanner in = null;
            System.out.print("Ведите какое имя вы хотите вывести: ");
            String goal = in.nextLine();
            if (String[i] == goal)
            {
                System.out.println(String[i]);
            }
        }
, but throws an error: Incompatible types. Found: 'java.lang.String', required: 'com.company.Znak'.
Who knows how to fix this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan., 2020-09-26
@LaRN

Your array is declared as Znak [] String, but then you do this:
String goal = in.nextLine();
if (String[i] == goal
) the variable goal of type String compares with the array element String[i], which is of type Znak.
You confused yourself by naming the array String.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question