-1

So I have an object which I import in another object

First object:

package test;
import java.util.*;

public class Eveniment {
    int ziua;
    String luna = new String();

    public void setZiua(int param){
        ziua = param;
    }
    public void setLuna(String param){
        luna = param;
    }
    public int getZiua(){
        return ziua; 
    }
    public String getLuna(){
        return luna;
    }
}

Second object:

package test;
import test.*;
import java.util.*;
public class EventPlanner {

    public static void main(String[] args){
        ArrayList<Eveniment> myAr = new ArrayList();
        Scanner sc = new Scanner(System.in);

        System.out.println("Introduceti ziua urmata de luna evenimentului: ");

        int zi = 0;
        String luna;
        zi = sc.nextInt();
        luna = sc.nextLine();

        Eveniment first = new Eveniment();
        first.setZiua(zi);
        first.setLuna(luna);

        myAr.add(first);

        while(luna!=null && zi!=0)
        {
            zi = sc.nextInt();
            luna = sc.nextLine();

            if(zi!=0)
            {
                Eveniment ev = new Eveniment();
                ev.setZiua(zi);
                ev.setLuna(luna);

                myAr.add(ev);
            }

        }

        String l = new String();
        l = "Ianuarie";

        System.out.println(myAr.size());*/

        for(int i = 0; i < myAr.size(); i++){
            if(myAr.get(i).getLuna().equals(l))
                System.out.println(1);
            else
                System.out.println(0);
        }
    }

    public static void afisare(ArrayList<Eveniment> myAr){
        System.out.println("---------Array------------");
        for(Eveniment i : myAr){
            System.out.println(i.getLuna() +" "+i.getZiua());
        }
    }
}

The thing that bugs me is that inside the for I do a check if a current object has it's luna string equal to l string then I print out 1 else I print 0, but the algorithm prints out 0 even if the strings are equal, what am I doing wrong ? I'm a newbie with Java so please don't judge too harshly.

Input given to the program:

1 Decembrie
2 Ianuarie
3 Februarie
4 Martie
0//to end the input

The program should write

`Decembrie` 0
`Ianuarie` 1
`Februarie` 0
`Martie` 0

because l is equal to Ianuarie.

southpaw93
  • 1,658
  • 4
  • 21
  • 37

1 Answers1

4

this is because

l = "Ianuarie"

and

 myAr.get(i).getLuna() =" Ianuarie"
                        ^--->there are space before the Ianuarie

so you will never get 1

Alya'a Gamal
  • 5,464
  • 17
  • 32