1

I am trying to assign an array in an object with an array that is in the main function. I am having trouble and the output I'm getting is [null,1]. Then the next time I try change it it prints out [(this Collection), 2]. What am I doing that is wrong in order to fix the problem. Also I'm sorry for any sloppy technique that I am using I've only had this class for about three weeks.

import java.util.ArrayList;
import java.util.Scanner;

//making a class for the baby sitting info 
public class bbySit 
{
    DecimalFormat df = new DecimalFormat("####");
    String yearString;
    String yearConvert;
    private int year;
    private int jobNum = 0001;
    private int numChild;
    private int hours;
    private ArrayList sitter;

    public bbySit()
    {
        year = 00;
        jobNum = 0001;
    }
    public bbySit(String yearString)    //going to make this also change the year later
    {
        yearConvert = yearString.substring(2,4);
        year = Integer.parseInt(yearConvert);
    }


    //This is what I need help with, trying to assign this then read assign it later in the code
    public ArrayList getSitter()
    {
        return sitter;
    }
    public void setSitter(ArrayList book)
    {
        this.sitter = book;
    }


    //this will be used later
    public int getNumChild()
    {
        return numChild;
    }
    public void setNumChild(int numChild)
    {
        this.numChild = numChild;
    }


    //this will be used later
    public int getHours()
    {
        return hours;
    }
    public void setHours(int hours)
    {
        this.hours = hours;
    }


    public int getYear()
    {
        return year;
    }
    public void setYear(String yearString)
    {
        yearConvert = yearString.substring(2,4);
        this.year = Integer.parseInt(yearConvert);
    }


    public int getJobNum() 
    {
        return jobNum;
    }
    public void setJobNum(int jobNum)
    {
        this.jobNum = jobNum;
    }


    public String toString()
    {
        String result = Integer.toString(year)+Integer.toString(jobNum);
        return result;
    }

    public static void main(String[] args)
    {
        DecimalFormat df = new DecimalFormat("0000.##");
        ArrayList book = new ArrayList();
        Scanner taco = new Scanner(System.in);

        int tempYear;
        int tempJobNum = 0001;
        int stopNum = 0;
//  These variables are going to be used later, just have them declared now
        int[] tempEmp;
        int[] addTempEnt;
        int loop = 0;

        bbySit job1 = new bbySit("2019");
        bbySit job2 = new bbySit("2020");
        bbySit job3 = new bbySit("2021");
        bbySit job4 = new bbySit("2022");
        bbySit job5 = new bbySit("2023");
        bbySit job6 = new bbySit("2024");
        bbySit job7 = new bbySit("2025");

        while(stopNum != 1)
        {
            System.out.println("What year is the job in (last two digits of the year i.e. 2019 = 19): ");
            tempYear = taco.nextInt();

            //The only two that I have changed trying to make work is case 19, and case 20
            switch(tempYear)
            {
            case 19:
                System.out.println("job is in year 20"+ job1.getYear());
                tempJobNum = job1.getJobNum();
                System.out.println("The job number is 19"+df.format(tempJobNum)+"\n");
                job1.setJobNum(tempJobNum + 1);
                System.out.println("Who is taking the job?");
                book.add(job1.getSitter());
                book.add(tempJobNum);
                job1.setSitter(book);
                System.out.println("taco taco "+job1.getSitter());
                book.clear();
                break;

            case 20:
                System.out.println("job is in year 20"+ job2.getYear());
                tempJobNum = job2.getJobNum();
                System.out.println("The job number is 20"+df.format(tempJobNum)+"\n");
                job2.setJobNum(tempJobNum + 1);
                System.out.println("Who is taking the job?");
                book.add(job1.getSitter());
                book.add(tempJobNum);
                job2.setSitter(book);
                System.out.println("taco taco "+job2.getSitter());  
                book.clear();
                break;

            case 21:
                System.out.println("job is in year 20"+ job3.getYear());
                tempJobNum = job3.getJobNum();
                System.out.println("The job number is 21"+df.format(tempJobNum)+"\n");
                job3.setJobNum(tempJobNum + 1);
                System.out.println("Who is taking the job?");
                book.add(job1.getSitter());
                break;

            case 22:
                System.out.println("job is in year 20"+ job4.getYear());
                tempJobNum = job4.getJobNum();
                System.out.println("The job number is 22"+df.format(tempJobNum)+"\n");
                job4.setJobNum(tempJobNum + 1);
                break;

            case 23:
                System.out.println("job is in year 20"+ job5.getYear());
                tempJobNum = job5.getJobNum();
                System.out.println("The job number is 23"+df.format(tempJobNum)+"\n");
                job5.setJobNum(tempJobNum + 1);
                break;

            case 24:
                System.out.println("job is in year 20"+ job6.getYear());
                tempJobNum = job6.getJobNum();
                System.out.println("The job number is 24"+df.format(tempJobNum)+"\n");
                job6.setJobNum(tempJobNum + 1);
                break;
            case 25:
                System.out.println("job is in year 20"+ job7.getYear());
                tempJobNum = job7.getJobNum();
                System.out.println("The job number is 25"+df.format(tempJobNum)+"\n");
                job7.setJobNum(tempJobNum + 1);
                break;

            default:
                System.out.println("Please print a valid responce (numbers 19-25).");   
            }
        }   
    }
}
steve john
  • 41
  • 1
  • 6
  • FYI, `0001` and `00` are in octal. In this case, the value in decimal is the same, but if you put something like `0010`, you will get 8, not 10. – Benjamin Urquhart Jun 05 '19 at 20:42
  • 1
    Which line is causing the problem? – 001 Jun 05 '19 at 20:43
  • Aside from main question: don't misuse terms array as replacement for arraylist. Array is `Type[]`. What you have here is `ArrayList` (which should not be raw and should provide type of elements it should hold via `ArrayList`). This is implementation of `List` interface and since internally it *uses* array to store its elements we call it array-list. – Pshemo Jun 05 '19 at 20:44
  • Anyway please *take your time* and create proper [MCVE] (a.k.a. [SSCCE](http://sscce.org)). There is a very high chance that while creating it you will find cause of your problem so it is worth it (especially for someone new to technology). Also maybe this will help a little: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and in case of exceptions [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788). – Pshemo Jun 05 '19 at 20:53
  • @BenjaminUrquhart I see what your taking about, but the assignment said that the output had to be a six digit number, first two being the year it is in and the last four is the number of jobs in that year, kinda forgot to mention that in the original post. That is why I put the the zeros in front. Is the zeros in front messing with the output? – steve john Jun 05 '19 at 21:04
  • @Pshemo I'm sorry if the question isn't formatted correctly. So going back to your first response, should I change the type in the to Type[] or should I keep the array list and make the Type[] in the first part an Array list – steve john Jun 05 '19 at 21:16
  • @stevejohn the number of zeros in front does not affect the number of digits in the number. – Benjamin Urquhart Jun 05 '19 at 22:43

0 Answers0