Reputation: 343
new to java, and i'm making a student record system. I want to use the specified year stored in the arraylist in this code (i have used an array list as i plan on eventually making this loop to allow multiple inputs)
import java.util.ArrayList;
import java.util.Scanner;
public class StudentYear
{
public ArrayList<Integer> studentYear;
public void StudentYear()
{
studentYear = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the year the student has most recently completed: ");
studentYear.add(sc.nextInt());
System.out.println(studentYear);
}
public ArrayList<Integer> getStudentYear()
{
return studentYear;
}
}
This is the code for the class that i want the stored year value in the arraylist studentYear, from the above code to go into
import java.util.ArrayList;
import java.util.Scanner;
public class Results
{
public static void Results(StudentYear studentYear)
{
int year = studentYear.getStudentYear().get(0);
ArrayList<Integer> results = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the results for "+year+":");
results.add(sc.nextInt());
System.out.println(results);
}
}
this is my main code
StudentYear yearInputobject = new StudentYear();
yearInputobject.StudentYear();
Results resultsInputobject = new Results();
resultsInputobject.Results(); //error here < The method Results(StudentYear) in the type Results is not applicable for the arguments ()
Any help would be greatly appreciated, ive been stuck on this for hours.
Edit: error now
exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method Results(StudentYear) in the type Results is not applicable for the arguments ()
at Main.main(Main.java:21)
results code once again
import java.util.ArrayList;
import java.util.Scanner;
public class Results
{
public static void Results(StudentYear studentYear)
{
int year = studentYear.getStudentYear().get(0);
ArrayList<Integer> results = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the results for "+year+":");
results.add(sc.nextInt());
System.out.println(results);
}
}
Upvotes: 1
Views: 97
Reputation: 19189
You're calling get
on a StudentYear
object, not the ArrayList
.
You already have a method to fetch the ArrayList, so you simply need to use it, and then fetch the element from the ArrayList it returns:
int year = studentYear.getStudentYear().get(0);
As for your second problem:
(First off, the only methods that start with a capital letter is by convention constructors).
You should rework the Results
method to a constructor. And, you will then need to move some stuff to a new method.
Here's how I'd construct a Results object:
Change
public static void Results(StudentYear studentYear)...
to
public Results(int year) {
this.year = year;
}
Note that the rest of the method was removed, and should be in a separate method (to learn why simply look up constructors and their purpose). And the this.year = year
row simply means that you have a field called year
, just like your studentYear
array. It is assigned the value you use when constructing the object. Your other method will then be able to use this value.
When you construct this object you'd do something like:
StudentYear yearInputobject = new StudentYear();
int year = yearInputobject.getStudentYear();
Results resultsInputobject = new Results(year);
resultsInputobject.printResults();
Note that I reworked StudentYear as well, and that I have a method called getStudentYear()
which returns an int directly instead.
Upvotes: 1