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.
Related
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 last year.
Improve this question
I have a string wherein I need to split it and store in an ArrayList as seen below; All these using Java7.
The below is the input for my program;
String inputStr = "ABC:DONE:07JAN90:1234:00001111:U:1234567:9632587:4561230:EC:IN:4815263:3621547:0314783)";
What I am trying to achieve here is an ArrayList as seen below after its grouped.
"List" : [
{
1234567,
4815263
},
{
9632587,
3621547
},
{
4561230,
0314783
}
]
I have a sample code written but that doesn't seem to group the elementB into the list. How can we do the grouping of both the elements into a List.
Sample Code:
String splitStringNeeded = "ABC:DONE:07JAN90:1352:00009279:U:1234567:9632587:4561230:EC:GB:4815263:3621547:0314783)";
String[] vElements = splitStringNeeded.split(":");
String[] grpV = Arrays.copyOfRange(vElements, 6,10 );
String[] grpT = Arrays.copyOfRange(vElements, 12, vElements.length);
List<String> groupedV = Arrays.asList(grpV);
List<String> groupedT = Arrays.asList(grpT);
for(String splitV : groupedV){
for(String splitT : groupedT){
System.out.println(splitV);
System.out.println(splitT);
break;
}
}
I am getting the below output where elementB does not change but repeats itself. Can someone please guide
"List" : [
{
1234567,
4815263
},
{
9632587,
4815263
},
{
4561230,
4815263
}
]
We can consider even the below inputs:
ABC:DONE:07JAN90:1352:00009279:U:1234567:9632587:0:EC:GB:4815263:3621547
ABC:DONE:07JAN90:1352:00009279:U:1234567:0:0:EC:GB:4815263
I have just modified the nested for loop in your sample code to a single for loop
Of course there are few un-knows such as Does your Source string always follow same pattern as you have displayed The length of the lists groupedV and groupedT are they always of same length etc. which is not considered
String splitStringNeeded = "ABC:DONE:07JAN90:1352:00009279:U:1234567:9632587:4561230:EC:GB:4815263:3621547:0314783";
/* Additional code for dynamic allocation of indexes
int indexU = splitStringNeeded.indexOf("U:");
int indexOfGb = splitStringNeeded.indexOf("EC:GB:");
System.out.println("Index of U "+indexU);
System.out.println("Index of GB "+indexOfGb);
String sub1 = splitStringNeeded.substring(indexU+2, indexOfGb-1);
System.out.println("Substring 1 = "+sub1);
String sub2 = splitStringNeeded.substring(indexOfGb+6, splitStringNeeded.length());
System.out.println("Substring 2 = "+sub2);
//Additional code for dynamic allocation of indexes
*/
String[] vElements = splitStringNeeded.split(":");
String[] grpV = Arrays.copyOfRange(vElements, 6, 9);
String[] grpT = Arrays.copyOfRange(vElements, 11, vElements.length);
List<String> groupedV = Arrays.asList(grpV);
List<String> groupedT = Arrays.asList(grpT);
if(groupedV.size() == groupedT.size()) {
for(int i=0; i<groupedV.size(); i++){
System.out.println(groupedV.get(i));
System.out.println(groupedT.get(i));
}
}
/*for (String splitV : groupedV) {
for (String splitT : groupedT) {
if (!splitV.equals("0")) {
System.out.println(splitV);
System.out.println(splitT);
break;
}
}
}*/
I have added the commented code to dynamically pick the index values instead of hard coding like 6,10,12
if you un-comment the first commented block the output will be like below
Index of U 31
Index of GB 57
Substring 1 = 1234567:9632587:4561230
Substring 2 = 4815263:3621547:0314783
1234567
4815263
9632587
3621547
4561230
0314783
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);
}
I have a dataset with so many columns and I want to cast all columns to the string using Java.
I tried below steps, I want to know if there is any better way to achieve this?
Dataset<Row> ds = ...;
JavaRDD<String[]> stringArrRDD = ds.javaRDD().map(row->{
int length = row.length();
String[] columns = new String[length];
for(int i=0; i<length;i++){
columns[i] = row.get(i) !=null? row.get(i).toString():"";
}
return columns;});
You can iterate over columns:
for (String c: ds.columns()) {
ds = ds.withColumn(c, ds.col(c).cast("string"));
}
If you want to use objects only:
import org.apache.spark.sql.types.*;
...
for (String c: ds.columns()) {
ds = ds.withColumn(c, ds.col(c).cast(DataTypes.StringType));
}
i have 2d array in this form:
GrupaArtikala [] grupaArtikala = parser.getObjektiArtikli();
Object[][] data = {
{grupaArtikala[1].getId(), grupaArtikala[1].getSifra(), grupaArtikala[1].getNaziv(), grupaArtikala[1].getIkonaID()},
{grupaArtikala[2].getId(), grupaArtikala[2].getSifra(), grupaArtikala[2].getNaziv(), grupaArtikala[2].getIkonaID()},
{grupaArtikala[3].getId(), grupaArtikala[3].getSifra(), grupaArtikala[3].getNaziv(), grupaArtikala[3].getIkonaID()},
{grupaArtikala[4].getId(), grupaArtikala[4].getSifra(), grupaArtikala[4].getNaziv(), grupaArtikala[4].getIkonaID()},
{grupaArtikala[5].getId(), grupaArtikala[5].getSifra(), grupaArtikala[5].getNaziv(), grupaArtikala[5].getIkonaID(),}
};
But i wat to create it with nested for loop, Any help please?
Here is one way you could incorporate a loop into this:
GrupaArtikala[]grupaArtikala=parser.getObjektiArtikli();
int length = grupaArtikala.length;
Object[][] data = new Object[length][4];
for(int i=0;i<length;i++){
GrupaArtikala temp = grupaAtrikala[i];
data[i][0] = temp.getId();
data[i][1] = temp.getSifra();
//add the rest of your attributes
}
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)
}