How I transfer file details to Vector? - java

I splited the file to String lines and put them in array indexes.
String fileString = readfromfile(fileEvents);
String[] fileLines = fileString.split("\n");
assuming this is the first index in fileLines:
14 57 9 2 www.buytickets.com
this is the constructor:
Orders (int EventID, int SoldtoCustomerID, int SoldtoEmployeesID, int NumberOfTickets, String URL)
the vector instance:
Vector<Orders> myOrders = new Vector<Orders>();
how do I transfer this line to first index in the vecotr so it would be like this:
(14,57,9,2,www.buytickets.com)
thank you :)

If we assume that there won't be any invalid entries the following should do the job:
Vector<Orders> myOrders = new Vector<Orders>();
for (String line : fileLines) {
String[] tokens = line.split("\\s+")
Orders o = new Orders(
Integer.valueOf(tokens[0]),
Integer.valueOf(tokens[1]),
Integer.valueOf(tokens[2]),
Integer.valueOf(tokens[3]),
tokens[4])
myOrders.add(o)
}

Related

I want to data type Conversion in a row in android

Hi.
I'm making an app that receives data from bluetooth by using stringbuilder
And makes it slice for using another activity.
The image shows what i want to make.
Q1. What should i use c->d, d->e ?
Q2. There will be a lot of data, I want to know the way to simplify this sequence
******************** edited ********************
I have practiced by adding value to Arraylist.
But in String Array, there is no .get(), so i couldn't access to element's length.
public static ArrayList<String> randomValue = new ArrayList<>();
public static int iDistance=0, xIAngle=0, yIAngle=0, zIAngle=0;
public static String distance, xAngle, yAngle, zAngle;
randomValue.add("12345090080070");
randomValue.add("15640080085071");
randomValue.add("16542070084074");
randomValue.add("12645080087078");
randomValue.add("21345084081060");
randomValue.add("14785078075065");
randomValue.add("13155079077077");
randomValue.add("14623080078078");
randomValue.add("14918086080078");
randomValue.add("15684085082080");
for (int i=0; i<randomValue.size(); i++){
String a = randomValue.get(i);
String distance = a.substring(0,5);
String xAngle = a.substring(5,8);
String yAngle = a.substring(8,11);
String zAngle = a.substring(11,14);
//String to int
iDistance = Integer.parseInt(distance);
xIAngle = Integer.parseInt(xAngle);
yIAngle = Integer.parseInt(yAngle);
zIAngle = Integer.parseInt(zAngle);
}
It seems like you are just stuck on finding the equivalent of get for a string array. To access an element in an array, the syntax is array[I], so if you were using a string array, this line:
String a = randomValue.get(i);
would have been:
String a = randomValue[i];
The code for your sequence of transformations can be shortened with Streams:
// this is the sequence of transformation starting with the sting builder "a"
List<String> randomValueWithLength14 =
Arrays.stream(a.toString().split(";")).filter(x -> x.length() == 14)
.collect(Collectors.toList());
// this is the for loop shown in your code
for (int i=0; i<randomValueWithLength14.size(); i++){
String s = randomValueWithLength14.get(i);
String distance = a.substring(0,5);
String xAngle = s.substring(5,8);
String yAngle = s.substring(8,11);
String zAngle = s.substring(11,14);
//String to int
iDistance = Integer.parseInt(distance);
xIAngle = Integer.parseInt(xAngle);
yIAngle = Integer.parseInt(yAngle);
zIAngle = Integer.parseInt(zAngle);
}

how to group 59 elements into one element in arraylist?

so, i have a program that read value and its year from a textfile that starts from 1960 till 2018. each indicator code have different values but the year stays the same, 1960-2018. i have successfully read the textfile and store it in the arraylist.
however, values and its years from one indicator is not grouped in one element. but instead, it groups them one value(and its year), one element. AND let's say i have 2 indicator code, 59 values for each indicator code. they're all mixed up in 118 elements. i wanted them in 2 elements.
how to solve this problem so that each indicator code and its data(years and values) is in one element?
myLinkedList bigdata = new myLinkedList();
ArrayList <MyData> data = new ArrayList();
MyData d1;
File inFile2 = new File ("indicator2.txt");
FileReader fr2 = new FileReader (inFile2);
BufferedReader br2 = new BufferedReader(fr2);
String str2 = br2.readLine();
while(str2 != null ){
StringTokenizer st = new StringTokenizer(str2,";");
String cCode = st.nextToken();
String iName = st.nextToken();
String iCode = st.nextToken();
for (int j = 0; j < 59; j++){
String v = st.nextToken();
int year = 1960 + j;
d1 = new MyData (year,v);
data.add(d1);
}
Indicator idct1 = new Indicator (cCode,iName,iCode,data);
bigdata.insertAtBack(idct1);
str2 = br2.readLine();
}
example of indicator2.txt:
MYS; Employment in industry (% of total employment) (modeled ILO estimate); SL.IND.EMPL.ZS; null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;null;31,72100067;31,62000084;32,01699829;32,22900009;32,34000015;32,23300171;33,6629982;31,76000023;31,70000076;32,18299866;33,11000061;31,97999954;32,02000046;30,12700081;29,72299957;30,26000023;28,45999908;28,68000031;27,01300049;27,73699951;29,08600044;28,56900024;28,36300087;28,02300072;27,51600075;27,48699951;27,39800072;27,30500031
(From left: cCode; iName; iCode; the values starting from 1960)
I believe the problem is that you're writing the data into the same ArrayList over and over again for all indicator codes. What if you moved the following statement:
ArrayList <MyData> data = new ArrayList();
Inside your while loop. Then you would be creating a new ArrayList for each indicator code when your save it to an Indicator object.
Generic types can be nested, so one solution would be to use an ArrayList of ArrayLists of MyData objects instead:
ArrayList<ArrayList<MyData>>

How to retrieve word after specific word in Java?

I've this kind of String:
{aa=bbbb, cc=blabla1, ee=ffff, cc=blabla2, gg=hhhh, cc=blabla3,.......}
and i want to get a list of all words after cc=.
How can i do it? I'm not very confident with regex stuff.
public static void main(String[] args) {
String input = "aa=bbbb, cc=blabla1, ee=ffff, cc=blabla2, gg=hhhh, cc=blabla3";
String[] splitValues = input.split(", ");
Map<String,List<String>> results = new Hashtable<>();
List<String> valueList = null;
// iterate through each key=value adding to the results
for (String a : splitValues) {
// a = "aa=bbbb" etc
String[] keyValues = a.split("=");
// you can check if values exist. This assumes they do.
String key = keyValues[0];
String value = keyValues[1];
// if it is already in map, add to its value list
if (results.containsKey(key)) {
valueList = results.get(key);
valueList.add(value);
} else {
valueList = new ArrayList<>();
valueList.add(value);
results.put(key, valueList);
}
}
System.out.println("cc= values");
valueList = results.get("cc");
// assumes value is in results
for (String a : valueList)
System.out.println(a);
}
Your question is very vague but I am guessing the String is provided as is, like:
String toSearch = "{aa=bbbb, cc=blabla1, ee=ffff, cc=blabla2, gg=hhhh, cc=blabla3,.......}";
By list I am guessing you are referring to the abstract List object and not to an array. Here is a solution:
String toSearch = "{aa=bbbb, cc=blabla1, ee=ffff, cc=blabla2, gg=hhhh, cc=blabla3,.......}";
List<String> result = new ArrayList<String>();
int prevMatch = 0;
while (toSearch.indexOf("cc=", prevMatch+1) != -1) {
result.add(toSearch.substring( // Substring method.
toSearch.indexOf("cc=",prevMatch+1)+3,toSearch.indexOf(",") //Getting correct indexes.
));
prevMatch = toSearch.indexOf("cc=",prevMatch+1);
}
The prevMatch variable ensures that the indexOf("cc=") that will be returned will be the next one occurring in the String. For the above String the returning ArrayList will contain the words "blabla1","blabla2", "blabla3" and whatever else is encountered.

How i could save an array[] to an array[][] with two dimensional

I had create an Array[] and i would like to save it in firsti column! So i had wrote this code:
String[][] SundaySounds = {{"0","0"}};
String[] SundayArray = {"0"};
........
for (int i=0;i<SundayArray.length;i++)
{
SundaySounds[0][i] = SundayArray[i]; <--line 39
System.out.println(SundayArray[0][i]);
}
And i get this error output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Test.main(Test.java:39)
Arrays in Java start at position 0, not position 1.
String[][] SundaySounds = {{"0","0"}};
String[] SundayArray = {"0"};
for (int i=0;i<SundayArray.length;i++)
{
SundaySounds[0][i] = SundayArray[i];//changed the index
System.out.println(SundayArray[i]);
}
This doesnt work, because in the second iterations you do the following:
SundaySounds[0][i] = SundayArray[i];
Here is i equal to 1. This is not possible becausw SundayArray doenst go further than 0
The following code works just fine.....
String[][] SundaySounds = {{"0","0"}};
String[] SundayArray = {"0"};
for (int i=0;i<SundayArray.length;i++)
{
SundaySounds[0][i] = SundayArray[i];
}
System.out.println(SundaySounds[0][0]);
System.out.println(SundaySounds[0][1]);
Output comes as 0 0
Try working it again.

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