This is what I need to do. I'm not really sure how though.
addGrade() method: Accepts a String parameter representing a new grade (including category prefix) to add to the GradeBook object; and returns true if added or false if not added (e.g., if category was not in the category array).
private String[] categories;
public Gradebook(String[] categoriesIn) {
categories = categoriesIn;
}
public boolean addGrade(String newGrade) {
categories[] = new Gradebook(newGrade);
}
I tried that above for starters but that's not correct
you cannot instantiate Gradebook Class in to variable categories, because variable categories is Array of String
try to instance new array, then assign to variable categories
Related
I need to transform an array of one type to an array of another type.
More specifically, I need to pull just a couple fields from each object in the starting array to create the resulting array, which will contain only those 2 fields, though named differently.
For example, let's say I have an array of Thing objects:
public class Thing {
private String id;
private String description;
... // other fields
}
I need to create from that an array of Item objects:
public class Item {
private String code;
private String data;
...
}
... where the id from each Thing becomes code in each Item; and description becomes data.
I've seen examples of using the Stream api to transform an array of objects to an array of Strings. But it's unclear to me thus far how to transform an object to another object.
Try this.
record Thing(String id, String description) {}
record Item(String coded, String data) {}
public static void main(String[] args) {
Thing[] array = {new Thing("1", "one"), new Thing("2", "two")};
Item[] transformed = Arrays.stream(array)
.map(thing -> new Item(thing.id(), thing.description()))
.toArray(Item[]::new);
System.out.println(Arrays.toString(transformed));
}
output:
[Item[coded=1, data=one], Item[coded=2, data=two]]
I have a Course class which has a method to add Items, which can be a note, an assignment, a URL, or just a generic item. All Items are kept in an ArrayList which the Course that created the list keeps up with. My question is from an item inside this ArrayList, how do I get the printLogger that I have attached to the course object in order to attach it to an item when the Item is created?
this from my Course:
public class Course {
private ArrayList<Item> items;
public PrintLogger p1 = null;
public Course(String code, String name) {
this.code = code;
this.name = name;
items = new ArrayList<>();
}
void add(Item item) {
items.add(item);
if (hasPrintLogger() == true) {
log(PrintLogger.INFORMATIONAL, "Adding " + item.toString());
}
}
And Im trying to have in the code that the assignment constructor runs a way to attach the same printLogger that is already on the course.
You could store a reference to the proper Course object in each of the items in the List.
You could also wrap the ArrayList class in your own class and add a field that points to the Course it belongs to if you need it to be a relationship between the List and the Course instead of between the items in the List and the Course.
You could also search each Course object in your system and check if it contains the List in question. However, this solution would scale badly.
Heyho everyone!
My project is a bit bigger so I decided to short it a bit and showing only the problem code, which i have currently. On the first, im programming on a console. Reading six strings from a Scanner, before I save them in a variable i'm doing a validity check (length, special signs, etc...). So I decided to make this in an extra method check_newCustomer(). I used an ArrayList to return more as one value. So now is the point that I need the captured inputs in the main() function or any other method which writes the new Customer in the database. Problem is now i don't know how I can reference to userID, firstname, secondname... in other method. I just can refer with an index. But I would prefer it when i can use the variable names to refer to it. So its much easier on the other methods to handle with strings. Possible?
public static void main(String[] args) {
ArrayList<String> newCustomer = new ArrayList<String>();
check_newCustomer (newCustomer);
}
public static void check_newCustomer(ArrayList<String> newCustomer) throws IOException {
String userID = null;
String firstname = null;
String secondname = null;
String street = null;
String zipcode = null;
String city = null;
// validity check before I fill the input fields
...
// fill arraylist
newCustomer.add(userID);
newCustomer.add(firstname);
newCustomer.add(secondname);
newCustomer.add(street);
newCustomer.add(zipcode);
newCustomer.add(village);
}
Thanks!
No, the value in the ArrayList is just a reference. The fact that you originally referred to it using a different variable is irrelevant.
You could use a Map<String, String> instead... but it would be much cleaner to have a Customer class which had fields for the various pieces of information. If you want multiple customers, you can then have a List<Customer>.
One needs to make a class Customer, just a group of fields. Instead of a list of Strings.
public class Customer {
String userID;
String firstname;
String secondname;
String street;
String zipcode;
String city;
}
And in the code:
Customer newCustomer = new Customer();
newCustomer.userID = ....
System.out.println(newCustomer.userID);
When you pass check_newCustomer (newCustomer); you are only passing a copy of the array list. In this case, the original array list newcustomer is left intact while a new copy whose scope is within the method check_newCustomer is where all the strings are stored. You can either create a new class for Customers or you can create a class and have your array list as a class variable and check_newCustomer as a class method. In the latter case, it is much simple.
class customer
ArrayList<String> newCustomer;
public static void main(String[] args) {
newCustomer = new ArrayList<String>();
check_newCustomer ();
}
public static void check_newCustomer() throws IOException {
String userID = null;
String firstname = null;
String secondname = null;
String street = null;
String zipcode = null;
String city = null;
// validity check before I fill the input fields
...
// fill arraylist
newCustomer.add(userID);
newCustomer.add(firstname);
newCustomer.add(secondname);
newCustomer.add(street);
newCustomer.add(zipcode);
newCustomer.add(village);
}
}
This must work.
I've been playing around with arrays for some time and this problem has been troubling me.
I created a user defined object and declared it in an array like this: `Property regesteredAssets[] = new Property[200];
And here's my constructor: `
public Property(String newPropertyName,String newPropertyAddress,String newPropertyType, String newPropertyDescription)
{
propertyName[arraySequence] = newPropertyName;
propertyFullAddress[arraySequence] = newPropertyAddress;
propertyType[arraySequence] = newPropertyType;
propertyDescription[arraySequence] = newPropertyDescription;
arraySequence++;
}
I want to initialize each array regesteredAsssets[] according to my desire. How can I do it?
Do I have to use arrays in my attributes in the Property class too?
You do not need your attributes to be arrays, unless a particular asset has multiple of something. In this case, I don't think it does. You can greatly simplify your code as follows:
public class Property {
private String name, address, type, description;
public Property(String name, String address, String type, String description) {
this.name = name;
this.address = address;
this.type = type;
this.description = description;
}
public static void main(String[] args) {
Property[] registeredAssets = new Property[200];
registeredAssets[0] = new Property("Joe Bloggs", "555 Fake St.", "IMPORTANT", "Lorem Ipsum Dolor");
// etc.
}
}
If you have an array of type Property, you can set each of the elements up using the following code:
regesteredAssets[0] = new Property( enterYourParametersHere );
I assume the fields in your Property constructor are single fields, and therefore you do not need to set them using the array notation field[index] = value, and indeed, if the Property class is of the consistency I think it is, then this will produce a compilation error.
If you wanted to set up multiple entries in your array, you could perform the initialisation step inside a loop, providing a loop index to the index of the array as below:
for( int i = 0; i < 10; i++ )
{
regesteredAssets[i] = new Property( enterYourParametersHere );
}
I hope this helps...
I'm new to Java so I'm probably doing something wrong here,
I want to create an array of Sets and I get an error (from Eclipse).
I have a class:
public class Recipient
{
String name;
String phoneNumber;
public Recipient(String nameToSet, String phoneNumberToSet)
{
name = nameToSet;
phoneNumber = phoneNumberToSet;
}
void setName(String nameToSet)
{
name = nameToSet;
}
void setPhoneNumber(String phoneNumberToSet)
{
phoneNumber = phoneNumberToSet;
}
String getName()
{
return name;
}
String getPhoneNumber()
{
return phoneNumber;
}
}
and I'm trying to create an array:
Set<Recipient>[] groupMembers = new TreeSet<Recipient>[100];
The error I get is "Cannot create a generic array of TreeSet"
What is wrong ?
From http://www.ibm.com/developerworks/java/library/j-jtp01255/index.html:
you cannot instantiate an array of a generic type (new List<String>[3] is illegal), unless the type argument is an unbounded wildcard (new List<?>[3] is legal).
Rather than using an array, you can use an ArrayList:
List<Set<Recipient>> groupMembers = new ArrayList<Set<Recipient>>();
The code above creates an empty ArrayList of Set<Recipient> objects. You would still have to instantiate every Set<Recipient> object that you put into the ArrayList.
Arrays don't support Generics. Use an ArrayList:
ArrayList<Set<Recipient>> groupMembers = new ArrayList<Set<Recipient>>();
You might want to consider using Guava's Multimap where the key is the index. This will handle creating the Sets for each index as you need them.
SetMultimap
SetMultimap<Integer, Recipient> groupMembers;