Java - split and parse | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Java - split and parse

Im reading in 3 inputs from a file that are on the same line and splitting it -i can add the 3 split pieces into an array - but what if i want to send each piece to my object as parameters for the constructor? My instructions are to use parse and split to save the data from my file and up to this point ive only saved each as its own variable using the different variations of next()

8th Oct 2017, 4:13 AM
Emily Pauls
Emily Pauls - avatar
2 Answers
+ 13
A code example would be quite helpful for us ;) Did you already save your parsed string to an array? Then pass the values to the constructor like this: MyClass myClass = new MyClass(arr); You can handle the array in the constructor like this: public MyClass(String[] arr) { s1=arr[0]; s2=arr [1]; // and so the on }
8th Oct 2017, 6:35 AM
Tashi N
Tashi N - avatar
+ 1
Easiest thing: Give your object an additional constructor and let it handle the array.
8th Oct 2017, 7:34 AM
1of3
1of3 - avatar