Map array to object - java

I have an array defined as follows:
String [] source = {"26", "Tom", "foo", ...};
And a Person class:
public class Person{
private String age;
private String name;
private String print;
private String ......;//the same type and order and number of source
public Person() {
}
//full construtors
public Person(String age, String name, String print,String ....) {
this.age = age;
this.name = name;
this.print = print;
//....
}
/* setters & getters */
}
How can I map these values to a Personinstance?
this is my real coding
public static List<BasicalVo> readObject(String path) throws IOException, NoSuchMethodException {
InputStreamReader fReader = new InputStreamReader(new FileInputStream(path),"gb2312");
BufferedReader bufferedReader = new BufferedReader(fReader);
String currentLine;
String[] temp;
List<BasicalVo> basicalVoList= new ArrayList<BasicalVo>();
while ((currentLine = bufferedReader.readLine()) != null) {
temp = currentLine.split(",");//I get the Array
for (int i = 0; i < temp.length; i++) {
//I don't know hot to translate to BasicalVo .
BasicalVo vo = new BasicalVo();
basicalVoList.add(vo);
}
}
return basicalVoList;
}

If the source just contains one person's data then you can do this:
Person p = new Person(source[0], source[1], source[2] ...);
If the array is too short an ArrayIndexOutOfBoundsException will be thrown.

I.
If the array contains only one Person you only need to create the instance like this :
String[] source = new String[]{"26", "tom", "xx", "....."};
Person p = new Person(source[0], source[1], source[2], source[3],...);
Because you'll know how many parameters there is in the constructor and so you won't have an ArrayIndexOutOfBoundsException if the array is well-build
II.
Assuming you have only 3 attributes, you'll be able to do like this if the array is like this :
String[] source = new String[]{"26", "tom", "xx", "22", "john", "yy"};
ArrayList<Person> list = new ArrayList<>()
for (int i = 0; i < source.length; i += 3) {
list.add(new Person(source[i], source[i + 1], source[i + 2]));
}
III.
If you have multiple fields, you would better do like this :
public Person(String[]source) {
this.age = source[0];
this.name = source[1];
this.print = source[2];
//....
}
Because it wouldn't not surcharge the code you have in your loop which read from the data, and make it easier to do your stuff, and in fact this is not hard, because in every case if you have like 20 fields, you'll have to assignate these 20 attributs
IV.
Or last proposition with a factory method :
public static Person createPersoneFromArray(String[] array) {
Person p = new Person();
p.setAge(array[0]);
p.setName(array[1]);
//...
return p;
}
And in the main method :
Person p = Person.createPersoneFromArray(source);

you can also add another constructor to your BasicalVo class which takes a String[] as input :
public BasicalVo(String [] input) {
this.age = input[0];
this.name = input[1];
this.print = input[2];
//....
}
which you then can call in your main as follows without additional for loop
....
temp = currentLine.split(",");
BasicalVo vo = new BasicalVo(temp);
basicalVoList.add(vo);
....

You can use OpenCSV
CSVReader csvReader = new CSVReader(new FileReader("people.csv"),',');
ColumnPositionMappingStrategy mappingStrategy = new ColumnPositionMappingStrategy();
mappingStrategy.setType(Person.class);
String[] columns = new String[]{"age","name","print"};
mappingStrategy.setColumnMapping(columns);
CsvToBean ctb = new CsvToBean();
List personList = ctb.parse(mappingStrategy, csvReader);

In this specific case I think that your best option is to use reflection. Reflection are a set of classes and interfaces that allow you to call different methods at execution time. For instance:
String [] source = { "26", "tom", "xx", ... };
Constructor constructor = Person.class.getConstructors()[0]
constructor.newInstance(source)
Take into account that this example only works because you have only one constructor, and so Person.class.getConstructors()[0] returns the constructor you want. YOu can try to get the specific constructor with Person.class.getConstructors(Class<?>...), in that case you would need to pass as a parameter an array with the type of the arguments.

Related

Sorting multi-type string ArrayList in Java based on Integers

I have a data Set like this:
1,JOHN,1934
2,TERENCE,1914
3,JOHN,1964
4,JOHN,1904
5,JOHN,1924
6,JOHN,1954
7,JOHN,1944
8,JOHN,1984
9,JOHN,1974
10,JOHN,1994
Which I've loaded in ArrayList of String[] from Text file like this:
ArrayList<String[]> records = new ArrayList<>();
String fileLocation = System.getProperty("user.dir");
String dataPath = fileLocation + File.separator + "boys-names.txt";
try {
try (BufferedReader br = new BufferedReader(new FileReader(dataPath))) {
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
records.add(values);
}
}
} catch (IOException e) {
e.printStackTrace();
}
I wanted to sort data set in increasing year like this:
4,JOHN,1904
2,TERENCE,1914
5,JOHN,1924
1,JOHN,1934
7,JOHN,1944
6,JOHN,1954
3,JOHN,1964
9,JOHN,1974
8,JOHN,1984
10,JOHN,1994
Problem: The built-in sorting method Collections.sort(list); of ArrayList only works on single type of data. But, in my case I have string with multi-type (string-integer) and sorting should base in Integers. So, is there any way to solve this problem?
Consider defining a model class for your data - let's call it MyData :
public class MyData {
private long id;
private String name;
private Year year;
//getters setters constructor
}
If the last integer value represents a year, why not use java.time.Year directly?
And then you could sort this list using List::sort method and passing a comparator :
Comparator<MyData> comparator = Comparator.comparing(MyData::getYear);
myDataList.sort(comparator);
By using java-8 lambda expression, write custom Comparator that compares Integer values and use Integer.valueOf for converting String to Integer
List<String[]> list = new ArrayList<String[]>();
String[] ar1 = {"1","JOHN","1934"};
String[] ar2 = {"2","TERENCE","1914"};
list.add(ar1);
list.add(ar2);
list.sort((c1,c2)->Integer.valueOf(c1[2]).compareTo(Integer.valueOf(c2[2])));
list.forEach(i->System.out.println(Arrays.toString(i)));
Output
[2, TERENCE, 1914]
[1, JOHN, 1934]
This would work:
public static void main(String[] args) {
List<String[]> records = new ArrayList<>();
records.add(new String[] {"1", "John", "1934"});
records.add(new String[] {"2", "Terence", "1914"});
Collections.sort(
records,
(o1, o2) -> {
Integer year_o1 = Integer.parseInt(o1[2]);
Integer year_o2 = Integer.parseInt(o2[2]);
if (year_o1 < year_o2) {
return -1;
} else if (year_o1 > year_o2) {
return 1;
} else {
return 0;
}
});
}

Implementing 2 Java Classes

I'm learning java and trying to implement two java classes.
Student: firstName, lastName, departmentIn, yearGraduation, an array of UAClass this student is taking, an array of integers corresponding to the grades received for these classes
UAClass: teacherFirstName, teacherLastName, semesterOffered, numCredits
In the Student class, implement a method that calculates GPA. In the Student’s main() method, initiate one Student object and print out her GPA.
In my student.java class I have:
import java.util.*;
public class Student {
private String firstName;
private String lastName;
private String departmentIn;
private String yearGraduation;
private float [] grade;
private int counter = 0;
private String Student;
public Student(String my_firstName, String my_lastName, String my_deptIn, String my_yearGrad) {
firstName = my_firstName;
lastName = my_lastName;
departmentIn = my_deptIn;
yearGraduation = my_yearGrad;
grade = new float[5];
}
public String toString(){
String value;
value = "First Name: " + firstName + "\n";
value += "Last Name : " + lastName + "\n";
value += "Department: " + departmentIn + "\n";
value += "Grad. Year: " + yearGraduation + "\n";
return value;
}
public static void main(String[] args) {
Student my1 = new Student("Bob", "Hope", "MBA", "2018");
Student my2 = new Student("John", "Smith", "MBA", "2020");
Student my3 = new Student("Jane", "Doe", "MBA", "2021");
UAClass cy1 = new UAClass[4];
String[] secondArray = cy1.getarrayClass();
System.out.println(my1);
System.out.println(my2);
System.out.println(my3);
System.out.println(Arrays.toString(cy1));
}
}
And in my UAClass.java class I have:
import java.util.*;
public class UAClass {
private String teacherFirstName;
private String teacherLastName;
private String semesterOffered;
private String numCredits;
private String[] arrayClass = {"MBA 501","MBA 505","MBA 513","MBA 545"};
public UAClass(String teacherF, String teacherL, String semesterO, String numC) {
teacherFirstName = teacherF;
teacherLastName = teacherL;
semesterOffered = semesterO;
numCredits = numC;
}
public String[] getarrayClass(){
return arrayClass.clone();
}
}
What I am trying to do is to create an Array in 'UAClass' and having it printed into 'Student' but I can't seem to get it working.
I've modified the code as Amit suggested. When I run it, I get this error.
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Array.getarrayClass at Homework2.Student.main(Student.java:66)
It seems to be having an issue with String[] secondArray = cy1.getarrayClass();
I took out the line String[] secondArray = cy1.getarrayClass() and it seems to run fine but now all I get is [null, null, null, null]
First of all, your UAClass has one constructor that takes String teacherF, String teacherL, String semesterO, String numC as parameters.
So you need to call this constructor like this:
UAClass cy1 = new UAClass("Teacher F", "Teacher L", "Semester", "NumC");
Secondly, you use an String[] type. This is a low-level array type. You can do this in Java, but normally people rather use a List type, and then not the raw type, but better like List<String>. List is actually an interface, but you can reference it as the Arrays class returns an implementation of the List class.
You should then use:
private List<String> arrayClass = Arrays.asList("MBA 501","MBA 505","MBA 513","MBA 545");
And you return a clone of the array. I presume you do this because you don't want the array to be changed. I would just return a concatenated String with values. Here is a nice example with a stream.
public String getClasses() {
return arrayClass.stream().collect(Collectors.joining(","));
}
Now in the Student class you can just print the list of classes like this:
System.out.println(cy1.getClasses());
When you change your code like that it will work but I couldn't understand what you are trying to do in your code.
UAClass cy1 = new UAClass("Bob", "", "", "");
String[] secondArray = cy1.getarrayClass();
System.out.println(my1);
System.out.println(my2);
System.out.println(my3);
System.out.println(cy1.getarrayClass());

Java Convert List<String> to List<Object>

I've two classess CsvRead and MyOwnClass.
In CsvRead I've a method public static List getDataFromCsv(); It returns list of all data. And this data I want to take in another method in class MyOwnClass and return there as list of objects of My OwnClass
It looks like this:
List<String> dataFromCsv = new ArrayList<String>();
And in another class, I want to convert it to List<Object> of my class.
private static List<String> getDataFromCsvClass = new ArrayList<String>();
getDataFromCsvClass = CsvReader.getAllCsvData(filename);
String name = dataFromCsv[0];
String surname = dataFromCsv[1];
String birth = dataFromCsv[2];
I want to return new MyOwnClass(name, surname, birth);
MY ERROR: array required but List found: String name = allData[0]; etc
You can create a method to convert a String to MyOwnClass and use stream to map the elements, e.g.:
public static MyOwnClass convertToObject(String element){
String[] tokens = element.split(",");
return new MyOwnClass(tokens[0], tokens[1], tokens[2]);
}
//code to convert
List<String> dataFromCsv = new ArrayList<String>();
List<MyOwnClass> list = dataFromCsv.stream()
.map(e -> convertToObject(e))
.collect(Collectors.toList());
However, this may not work if let's say name or surname contains comma. In which case, I would recommend having a look at OpenCSV library and this example of how to read csv into objects.
Supposing that the list contains the name, surname and birth in every group of 3 strings (i.e., the elements on index 0, 3, 6, 9 etc. contain the name), you might try the following:
public List<MyOwnClass> convertCsvData(List<String> csv_data)
{
// Initialize result
List<MyOwnClass> result;
result = new ArrayList<MyOwnClass>();
// Parse data
int counter;
String name;
String surname;
String birth;
for (counter = 0; counter < csv_data.size(); counter += 3)
{
name = csv_data.get(counter);
surname = csv_data.get(counter + 1);
birth = csv_data.get(counter + 2);
result.add(new MyOwnClass(name, surname, birth));
}
// Done
return (result);
} // convertCsvData
Somthing like this :
private static MyOwnClass toMyOwnClass(String str){
String[] object= str.split(",");
return new MyOwnClass(object[0], object[1], object[2]);
}
List<String> dataFromCsv = new ArrayList<String>();
List<MyOwnClass> list = new ArrayList<>();
for(String string : dataFromCsv ){
if(StringUtils.isNoneEmpty(string)){
list.add(toMyOwnClass(string));
}
}
And then you return your list

manipulate and sort text file

I am working on a project where I have been given a text file and I have to add up the points for each team and printout the top 5 teams.
The text file looks like this:
FRAMae Berenice MEITE 455.455<br>
CHNKexin ZHANG 454.584<br>
UKRNatalia POPOVA 453.443<br>
GERNathalie WEINZIERL 452.162<br>
RUSEvgeny PLYUSHCHENKO 191.399<br>
CANPatrick CHAN 189.718<br>
CHNHan YAN 185.527<br>
CHNCheng & Hao 271.018<br>
ITAStefania & Ondrej 270.317<br>
USAMarissa & Simon 264.256<br>
GERMaylin & Daniel 260.825<br>
FRAFlorent AMODIO 179.936<br>
GERPeter LIEBERS 179.615<br>
JPNYuzuru HANYU 197.9810<br>
USAJeremy ABBOTT 165.654<br>
UKRYakov GODOROZHA 160.513<br>
GBRMatthew PARR 157.402<br>
ITAPaul Bonifacio PARKINSON 153.941<br>
RUSTatiana & Maxim 283.7910<br>
CANMeagan & Eric 273.109<br>
FRAVanessa & Morgan 257.454<br>
JPNNarumi & Ryuichi 246.563<br>
JPNCathy & Chris 352.003<br>
UKRSiobhan & Dmitri 349.192<br>
CHNXintong &Xun 347.881<br>
RUSYulia LIPNITSKAYA 472.9010<br>
ITACarolina KOSTNER 470.849<br>
JPNMao ASADA 464.078<br>
UKRJulia & Yuri 246.342<br>
GBRStacey & David 244.701<br>
USAMeryl &Charlie 375.9810<br>
CANTessa & Scott 372.989<br>
RUSEkaterina & Dmitri 370.278<br>
FRANathalie & Fabian 369.157<br>
ITAAnna & Luca 364.926<br>
GERNelli & Alexander 358.045<br>
GBRPenny & Nicholas 352.934<br>
USAAshley WAGNER 463.107<br>
CANKaetlyn OSMOND 462.546<br>
GBRJenna MCCORKELL 450.091<br>
The first three letters represent the team.
the rest of the text is the the competitors name.
The last digit is the score the competitor recived.
Code so far:
import java.util.Arrays;
public class project2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] array = new String[41];
String[] info = new String[41];
String[] stats = new String[41];
String[] team = new String[41];
//.txt file location
FileInput fileIn = new FileInput();
fileIn.openFile("C:\\Users\\O\\Desktop\\turn in\\team.txt");
// txt file to array
int i = 0;
String line = fileIn.readLine();
array[i] = line;
i++;
while (line != null) {
line = fileIn.readLine();
array[i] = line;
i++;
}
//Splitting up Info/team/score into seprate arrays
for (int j = 0; j < 40; j++) {
team[j] = array[j].substring(0, 3).trim();
info[j] = array[j].substring(3, 30).trim();
stats[j] = array[j].substring(36).trim();
}
// Random stuff i have been trying
System.out.println(team[1]);
System.out.println(info[1]);
System.out.println(stats[1]);
MyObject ob = new MyObject();
ob.setText(info[0]);
ob.setNumber(7, 23);
ob.setNumber(3, 456);
System.out.println("Text is " + ob.getText() + " and number 3 is " + ob.getNumber(7));
}
}
I'm pretty much stuck at this point because I am not sure how to add each teams score together.
This looks like homework... First of all you need to examine how you are parsing the strings in the file.
You're saying: the first 3 characters are the country, which looks correct, but then you set the info to the 4th through the 30th characters, which isn't correct. You need to dynamically figure out where that ends and the score begins. There is a space between the "info" and the "stats," knowing that you could use String's indexOf function. (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int))
Have a look at Maps.
A map is a collection that allows you to get data associated with a key in a very short time.
You can create a Map where the key is a country name, with value being the total points.
example:
Map<String,Integer> totalScore = new HashMap<>();
if (totalScore.containsKey("COUNTRYNAME"))
totalScore.put("COUNTRYNAME", totalScore.get("COUNTRYNAME") + playerScore)
else
totalScore.put("COUNTRYNAME",0)
This will add to the country score if the score exists, otherwise it will create a new totalScore for a country initialized to 0.
Not tested, but should give you some ideas:
public static void main(String... args)
throws Exception {
class Structure implements Comparable<Structure> {
private String team;
private String name;
private Double score;
public Structure(String team, String name, Double score) {
this.team = team;
this.name = name;
this.score = score;
}
public String getTeam() {
return team;
}
public String getName() {
return name;
}
public Double getScore() {
return score;
}
#Override
public int compareTo(Structure o) {
return this.score.compareTo(o.score);
}
}
File file = new File("path to your file");
List<String> lines = Files.readAllLines(Paths.get(file.toURI()), StandardCharsets.UTF_8);
Pattern p = Pattern.compile("(\\d+(?:\\.\\d+))");
List<Structure> structures = new ArrayList<Structure>();
for (String line : lines) {
Matcher m = p.matcher(line);
while (m.find()) {
String number = m.group(1);
String text = line.substring(0, line.indexOf(number) - 1);
double d = Double.parseDouble(number);
String team = text.substring(0, 3);
String name = text.substring(3, text.length());
structures.add(new Structure(team, name, d));
}
}
Collections.sort(structures);
List<Structure> topFive = structures.subList(0, 5);
for (Structure structure : topFive) {
System.out.println("Team: " + structure.getTeam());
System.out.println("Name: " + structure.getName());
System.out.println("Score: " + structure.getScore());
}
}
Just remove <br> from your file.
Loading file into memory
Your string splitting logic looks fine.
Create a class like PlayerData. Create one instance of that class for each row and set all the three fields into that using setters.
Keep adding the PlayerData objects into an array list.
Accumulating
Loop through the arraylist and accumulate the team scores into a hashmap. Create a Map to accumulate the team scores by mapping teamCode to totalScore.
Always store row data in a custom object for each row. String[] for each column is not a good way of holding data in general.
Take a look in File Utils. After that you can extract the content from last space character using String Utils e removing the <br> using it as a key for a TreeMap. Than you can have your itens ordered.
List<String> lines = FileUtils.readLines(yourFile);
Map<String, String> ordered = new TreeMap<>();
for (String s : lines) {
String[] split = s.split(" ");
String name = split[0].trim();
String rate = splt[1].trim().substring(0, key.length - 4);
ordered.put(rate, name);
}
Collection<String> rates = ordered.values(); //names ordered by rate
Of course that you need to adjust the snippet.

incompatible type of double array and properties string.split()

public static void main(String[] args)
{
String input="jack=susan,kathy,bryan;david=stephen,jack;murphy=bruce,simon,mary";
String[][] family = new String[50][50];
//assign family and children to data by ;
StringTokenizer p = new StringTokenizer (input,";");
int no_of_family = input.replaceAll("[^;]","").length();
no_of_family++;
System.out.println("family= "+no_of_family);
String[] data = new String[no_of_family];
int i=0;
while(p.hasMoreTokens())
{
data[i] = p.nextToken();
i++;
}
for (int j=0;j<no_of_family;j++)
{
family[j][0] = data[j].split("=")[0];
//assign child to data by commas
StringTokenizer v = new StringTokenizer (data[j],",");
int no_of_child = data[j].replaceAll("[^,]","").length();
no_of_child++;
System.out.println("data from input = "+data[j]);
for (int k=1;k<=no_of_child;k++)
{
family[j][k]= data[j].split("=")[1].split(",");
System.out.println(family[j][k]);
}
}
}
i have a list of family in input string and i seperate into a family and i wanna do it in double array family[i][j].
my goal is:
family[0][0]=1st father's name
family[0][1]=1st child name
family[0][2]=2nd child name and so on...
family[0][0]=jack
family[0][1]=susan
family[0][2]=kathy
family[0][3]=bryan
family[1][0]=david
family[1][1]=stephen
family[1][2]=jack
family[2][0]=murphy
family[2][1]=bruce
family[2][2]=simon
family[2][3]=mary
but i got the error as title: in compatible types
found:java.lang.String[]
required:java.lang.String
family[j][k]= data[j].split("=")[1].split(",");
what can i do?i need help
nyone know how to use StringTokenizer for this input?
Trying to understand why you can't just use split for your nested operation as well.
For example, something like this should work just fine
for (int j=0;j<no_of_family;j++)
{
String[] familySplit = data[j].split("=");
family[j][0] = familySplit[0];
String[] childrenSplit = familySplit[1].split(",");
for (int k=0;k<childrenSplit.length;k++)
{
family[j][k+1]= childrenSplit[k];
}
}
You are trying to assign an array of strings to a string. Maybe this will make it more clear?
String[] array = data.split("=")[1].split(",");
Now, if you want the first element of that array you can then do:
family[j][k] = array[0];
I always avoid to use arrays directly. They are hard to manipulate versus dynamic list. I implemented the solution using a Map of parent to a list of childrens Map<String, List<String>> (read Map<Parent, List<Children>>).
public static void main(String[] args) {
String input = "jack=susan,kathy,bryan;david=stephen,jack;murphy=bruce,simon,mary";
Map<String, List<String>> parents = new Hashtable<String, List<String>>();
for ( String family : input.split(";")) {
final String parent = family.split("=")[0];
final String allChildrens = family.split("=")[1];
List<String> childrens = new Vector<String>();
for (String children : allChildrens.split(",")) {
childrens.add(children);
}
parents.put(parent, childrens);
}
System.out.println(parents);
}
The output is this:
{jack=[susan, kathy, bryan], murphy=[bruce, simon, mary], david=[stephen, jack]}
With this method you can directory access to a parent using the map:
System.out.println(parents.get("jack"));
and this output:
[susan, kathy, bryan]

Categories

Resources