Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have created an ArrayList which contains some infos .
Every line has this pattern : name age sex job .
I'm searching for a way to take for example the every value separately and to assign them in different variables. I've search the web , but I came out to nothing ! So if there is someone that could help me I would appreciate it !
I have no idea how to do it so I can't provide code ! Soory I'm newbie in java .
Create a class Person that will hold your data:
class Person {
private String name;
private int age;
private String sex;
private String job;
//class constructor...
//getters and setters...
}
Then, read the file. For every line in the file, create an instance of Person class and store it in a List. I'll do this in pseudocode, it's up to you the concrete implementation (otherwise it would be me doing your homework =\):
List<Person> people <- new ArrayList<Person>()
open_file(theFile)
while not_end_of_file
String name <- read_text
String age <- read_int
String sex <- read_text
String job <- read_text
Person person <- new Person()
person->setName(name)
//similar for other fields...
people->add(person)
end while
Sounds like you have a List of Strings. If so, you could call the String split(String regex) method to get a String[] back.
I think what you need to do is create a Person object which would contain a name, age, sex and job.
Then you could create an Arraylist of type Person.
List<Person> people = new ArrayList<Person>();
The person class would look something like this:
public class Person {
String name, sex, jobTitle;
int age;
public Person(String name){
this.name = name;
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
List
Person
name: String
age: Integer
address: List
Address
addLine1: String
addLine2: String
state: String
pincode: Integer
I don't know how you designed your models but you can follow this way:
class Person {
String name;
Integer age;
List<Address> addressList;
// getter, setter, const...
}
class Address {
// some fields...
// getter, setter, const...
}
And then you can add your address to your person via add method:
Person p = new Person();
Address a = new Address();
// modiy person and address...
p.getAddressList().add(a);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have class Library
public class Books extends Library {
String regNumber
String author;
String name;
int yearOfPublishing;
String publishingHouse;
int numberOfPages;
public Books(String regNumber, String author, String name, int yearOfPublishing,
String publishingHouse, int numberOfPages) {
this.regNumber = regNumber;
this.author = author;
this.name = name;
this.yearOfPublishing = yearOfPublishing;
this.publishingHouse = publishingHouse;
this.numberOfPages = numberOfPages;
}
How to list books with authors' last names in alphabetical order?
First, you should have a Book class for individual books. Assuming your Library is a list of books you can then do it like this.
List<Book> sortedLibrary = library.stream()
.sorted(Comparator.comparing(book -> book.author))
.collect(Collectors.toList());
Since no details were provided about the author name field it sorts on the entire field whether its first, last or both names.
If you want to sort them in place, a cleaner approach would be.
library.sort(Comparator.comparing(book->book.author));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hello so I'm having a bit of difficulties with a setter method on objects.
public class Company {
private String companyName;
public static int numberOfEmployees;
public Employees employees[];
public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
Employees employee1 = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());
employees[numberOfEmployees] = employee1;
numberOfEmployees++;
}
So basicly this is a method defined in the 'company class' while making an Employees object who's using the parameters for making a 'Programmer'.
But that's not the deal, what I want tot do is by calling this setter method, automaticly create an object. So each time it's used, kind of increment the name of the object it's going to make.
So for example the first time I use it it makes an object called Employee1 and stores it in Employee[0].. second time I want it to store Employee2 into Employee[1].
Maybe I'm making this way too difficult but I'm just trying things out, and can't seem to find a way to make this work.
I suppose that Programmer object is subclass of Employees, or else it will not work. More or less it should look like the following:
public class Company {
private String companyName;
public static int numberOfEmployees;
public static Employees employees[];
public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
numberOfEmployees++;
employees[numberOfEmployees] = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a model: fase.java with Integers and Strings + getters and setters:
public class Fase implements Serializable {
private Integer age;
private String name;
}
I want to store both the Integer and String in a Array or ArrayList. I now use this:
public String[] getAllValues(){
String[] values = {age.toString(), name};
return values;
Then in dataServiceImpl.java I retrieve the data with:
user.getFase().getAllValues()[0];
and retrieve the age.
This works, but I have a lot more than age and name, and was thinking if I could put everything in Fase.java in one Array/ArrayList, because they are Integer and String, and then retrieve it in dataServiceImpl.java?
Something like this in Fase.java: ArrayList <Objects> f3Values = new ArrayList <Objects>();
or Fase [] f3Array = new Fase[34];
and then retrieve that in dataServiceImpl.java with: ArrayList<Fase3.Fase3Array> f3List = new ArrayList<Fase3.Fase3Array>();
and use something like: user.f3List[0]; ?
First, you should learn how Java works.
Is Java "pass-by-reference" or "pass-by-value"?
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Then, you should learn how to properly create an encapsulated class, by defining both constructor(s) and getters, methods, setters (if needed; note that setters in general break encapsulation) etc.
Then, you should understand that to aggregate data you:
create a class, i.e. definition object that holds all the necessary fields,
create a storage aggregate (array, ArrayList, Map, whatever),
3a. create an object of a given class, setting the values of the fields,
3b. add the object to the aggregate,
3c. goto 3a until the aggregate is filled with the data needed.
Explaining that on the code provided, you should first have
public class Fase implements Serializable {
private int age;
private String name;
public Fase( int age, String name ) {
this.age = age;
this.name = name;
}
public int getAge() { return age; }
public String getName() { return name; }
}
then you can create the aggregate, e.g.
int FASE_MAX = 34;
Fase[] fArray = new Fase[FASE_MAX];
ArrayList<Fase> fArrayList = new ArrayList<Fase>(FASE_MAX);
then you create the objects and add them to the aggregate, e.g.
for( int i = 0; i < FASE_MAX; i++ ) {
Fase newFase = new Fase( i, "John Doe" );
fArrayList.add( newFase );
fArray[i] = newFase;
}
then, and only then, you can access the aggregate:
Fase someFase = fArrayList.get( n );
Fase someOtherFase = fArray[n];
Your Fase class can have whatever members and however many members you like and you can access them all. If you want an array of Fase then create one and each element of the array will contain all the Fase members.
Fase[] myArray = new Fase[34];
You have an array of 34 "Fase's" just add whatever members you want to your Fase class.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im a new programmer and I want to know when its best practice to use overloaded constructors and what makes it different from single primary constructor.
The short answer is: you should use overloading whenever you need it.
As a real-life example, take a look at the JLabel API: https://docs.oracle.com/javase/8/docs/api/javax/swing/JLabel.html
JLabel has quite a few constructors: one that just takes a String, one that takes a String and an icon, one that only takes an icon, and one that doesn't take any arguments at all.
You would use each constructor when you want to construct that kind of JLabel: one that displays a String, one that displays a String and an icon, one that only displays an icon, or one that doesn't display anything yet (until you call one of its setter functions).
Constructor overloading is useful when you want to allow user to create objects in multiple different ways.For example to be able to create a simple Student class object in following different ways:
new Student(45); // given student id
new Student("name"); // given student name
new Student(45,"name"); // given student registration id and name
This helps ease the task of creating objects according to our requirements. You can link this concept with various java API's as the provide a no of different ways to initialize an object of a class.
Also you can combine the Construstor Overloading with Constructor chaining.
Here is an examlple:
public Student (int id){
this(id,"ANY-DEFAULT-NAME"); // calls the constructor of same class with 2 params
}
public Student (String name){
this(ANY-DEFAULT-ID,name);// calls the constructor of same class with 2 params
}
public Student (int id,String name){
// here you can initialize the instance variables of the class.
}
You can overload a constructor based in your needs. For example,let's say that you have a simple class called Dog, that have some attributes like: Name,Breed, Birthday, Owner and skin color.
public class Dog {
private String name;
private String breed;
private Date birthday;
private String owner;
private String skinColor;
/*Getters and Setters*/
...
}
If you instance a object of type Dog and want to set a all or some of the attributes' values, you'll have to call all the setters methods of the object, but with the constructor, you can save that step passing the values directly every moment you instance the object.
Example:
public class Dog {
private String name;
private String breed;
private Date birthday;
private String owner;
private String skinColor;
public Dog(String name, String breed,Date birthday,String owner,String skinColor){
this.name = name;
this.breed = breed;
this.birthday = birthday;
this.owner = owner;
this.skinColor = skinColor;
}
/*Getters and Setters*/
...
}
Dog myDog = new Dog("Jose", "Chiguagua",new Date(),"Jhon","Brown");
If you want only instance the object with the name only, you can do it too. A good practice is, if you have an object with attributes that is necessary to fill in some point, provide the default constructor, if you do not provide it, you will always need to pass some values for instance a object. This give flexibility to the programmer.