0

I have no idea how to properly write line of code adding class field to an ArrayList

public class Main {
    public static void main(String[] args) throws IOException
    {
        zapisz("BazaDanych.txt");
        Scanner scanner = new Scanner(System.in);
        FilmExtended filmExtended = new FilmExtended();
        ArrayList<FilmExtended> bazaFilmow = new ArrayList<>();
        int i = 0;
        while(scanner.nextInt()!= 0)
        {
            boolean check = true;
            do
            {
                System.out.println("Podaj tytuł fimu: ");
                String temp = scanner.nextLine();
                if (temp.matches("[a-zA-Z]{2,}"));
                {
                    bazaFilmow.add(i,filmExtended.setTytul(temp));
                    check = false;
                }
            }while (check);
        }
    }
2

4 Answers 4

2

You don't increment your index i. It's always 0. You have to put i++; in your do while loop

Sign up to request clarification or add additional context in comments.

1 Comment

i did but it wasn't written in this part of code i pasted
1
public class Main {
public static void main(String[] args) throws IOException
{
    zapisz("BazaDanych.txt");
    Scanner scanner = new Scanner(System.in);
    List<FilmExtended> bazaFilmow = new ArrayList<>();
    //remove index
    while(scanner.nextInt() != 0)
    {
        boolean check = true;
        do
        {
            System.out.println("Podaj tytuł fimu: ");
            String temp = scanner.nextLine();
            if (temp.matches("[a-zA-Z]{2,}"));
            {
                FilmExtended filmExtended = new FilmExtended(); //create new instance
                filmExtended.setTytul(temp);
                bazaFilmow.add(filmExtended); //use add without index or else need to increment your index
                check = false;
            }
        } while (check);

} }

1 Comment

Could you explain what you changed? And why this solves the issue? Else your answer isnt that helpful, see How to Answer thanks.
1

You have to add i++; in the do while loop

Comments

0

Two ways to fix it:

  1. filmExtended.setTytul(temp) should return this.
  2. i++ or remove i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.