Java - Large String need to split with different field length - java

I'm new to Java and didn't find any exact solution for my scenario. I have a large string which is 1500 length.
For example :
String inData = "THISKALJSKAJFDSKJDKSJ KSJDLKJSFKD LSKJDFKSJ, ASA:..";
I have a fixed format for these 1500 length. I have to split the string into 200+ fields based on each field fixed length. I found something like below.
String firstFld = inData.substring(0, 2);
String secondFld = inData.substring(3, 10);
String thirdFld = inData.substring(11, 13);
can anyone please suggest with better way of doing the split instead of declaring 200 string variables and store them there or loading them to a String Array ? I plan to to build an xml with all these fields after getting them.
I appreciate all your help!

Well, if it's two of the below, then this would work:
There's a pattern for the field lengths: like (0,2), (3, 10), (11,13), (14, 21) ...
You have a list of field lengths
In both cases it is pretty simple to solve what you want:
First Case: Pattern is 2 chars -> 7 chars starting with 2
String[] fields = new String[getNumberOfFields(1500)];
int curr = 0;
for(int i = 0 ; i < fields.length; i++) {
if(i % 2 == 0) {
fields[i] = inData.substring(curr, curr+7);
curr+=8;
} else {
fields[i] = inData.substring(curr, curr+2);
curr+=3;
}
}
Second Case: You have a bunch of different field lenghts
int curr = 0;
String[] fields = new String[fieldLengths.length];
for(int i = 0; i < fieldLengths.length; i++) {
fields[i] = inData.substring(curr, curr+fieldLengths[i]);
}

Related

JAVA - Not able to properly fill multidimentional array from another array

I have a string of values separated by commas that I converted into an array, which I was then going to use to create a 2D array. When creating a loop to add the data from the first array to the 2D array it is repeating the data.
The output I'm getting is:
4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005,4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005
and the correct output should be:
4428,40,401,610,2016,3821,31,347,572,2015,4381,38,341,520,2014,2536,17,193,290,2013,4295,39,371,552,2012,4643,45,343,502,2011,3922,28,312,475,2010,4434,30,350,541,2009,4038,28,341,536,2008,218,1,20,28,2007,46,0,6,15,2006,65,0,9,16,2005
Expected results:
{{4428,40,401,610,2016}
{3821,31,347,572,2015}
{4381,38,341,520,2014}
...} and so on, every 5
My code for adding the array to the 2D array is below:
{String[] columns = {"Yards","Touchdowns","Attempts","Incompletions","Year"};
String[] data1 = results1.split(",");
Object [][] data11 = new Object[columns.length][data1.length];
for(int i = 0; i<columns.length;i++){
for(int j = 0; j<data1.length;j++){
data11[i][j] = data1[j];
//System.out.print(data11[i][j]+",");
}
}}
EDIT: Solution!
Object [][] data11 = new Object[data1.length/columns.length][columns.length];
int column = -1;
for(int j = 0; j<data1.length;j++){
if(j % columns.length == 0) column = column+1;
data11[column][j % 5] = data1[j];
}
Maybe this work for you:
Object [][] data11 = new Object[columns.length][data1.length / 5];
int column = -1;
for(int j = 0; j<data1.length;j++){
if(j % 5 == 0) column = column + 1
data11[j % 5][column] = data1[j];
}
Note the matrix size changed and the assignation too. Haven't tried, probably you can make a prettier version. Hope it helps!
You also loop the first array (i), so he will repeat it. Just write this:
data11[0][j] = data1[j];
Or even don't do the first loop at all, depending on what you need.
Assuming your output, you only want to fill in the first position (0) of the array.

Finding no. of occurences of String in another String in smart way

I am stuck on this question after doing a lot of research. I want to find the no. of occurrences of String in another String but needed very smart approach.
a = "ASAD" str = "ASADASAD" expect output:2
b = "AAA" str2 = "AAAAAAAAAA" expect output:8
For example, considers these two strings. On the first line, 'a' occurs twice in 'str' while on the second line if you see the whole combinations of b occurs 8 times in str2. How to address both these challenges at once in the same code. I have coded these two scenarios separately but I want to do it in a smart way so one code can deal with all possible combinations of input strings. Here is my code.
For Case 1
Type1 = (LongString.split(SmallStr, -1).length-1);
for Case 2
while (Index < SmallStr.length())
{
String tester = LongString.substring(Index);
Counter = (tester.split(SmallStr,-1).length-1);
ans= Counter + ans;
lastIndex ++;
}
System.out.println(ans);
You might try something like this:
String myString = "ASADASAD"; // or "AAAAAAAAAA"
String stringToFind = "ASAD"; // or "AAA"
int offset = 0;
int count = 0;
while (offset < myString.length())
{
int index = myString.indexOf(stringToFind, offset);
if (index < 0)
break;
count++;
offset = index + 1;
}

Spliting the string by number of chars [duplicate]

This question already has answers here:
Split string to equal length substrings in Java
(23 answers)
Closed 5 years ago.
String str = "abcdefghijklmnoprqstuvwxyz";
String[] array = new String[str.length()/4 +1];
Array should look like array = {"abcd","efgh","ijkl"...."yz"} after my work.
Here is what I have tried:
WORK1:
int strIndex = 0;
int arrayIndex=0;
for(strIndex=0; strIndex<str.length();strIndex++) {
array[arrayIndex] += Character.toString(str.charAt(strIndex));
if((strIndex % 4 == 0) && (strIndex != 0 ))
arrayIndex++;
}
========================================================================
WORK2:
String str = "abcdefghijklmnoprqstuvwxyz";
String[] array = new String[str.length()/4 +1];
int start = 0; // 0->4->8->12..
int end = 4; // 4->8->12->16...
System.out.println("arraylength:"+array.length);
for(int i=0;i<array.length;i++) {
array[i] = str.substring(start,end);
start+=4;
end+=4;
}
===========================================
WORK1: it gives me the output of abcde fghi jklm nopr qstu vwxy z, which is wrong
WORK2: Because substring() jumps by 4, it will be the cause of Exception when it access the index of 28. Last part should be: (str.substring(24,26));, I can't think of efficient way to handle this.
Any advice will be appreciated.
You Need to restrict the Substring end to the strings Maximum lenght:
// pseudocode - you did not supply a tag for the language you are using
str.Substring(start,Math.Min(str.Count,end)) // Math.Min == C#
WORK1 should work with a minor change.
Currently you're putting "abcde" into the first array element simply because you're adding the 0th, 1st, 2nd, 3rd and 4th elements. You want to seperate before the 4th element not after. Give this a try:
int strIndex = 0;
int arrayIndex=0;
for(strIndex=0; strIndex<str.length();strIndex++) {
if((strIndex % 4 == 0) && (strIndex != 0 ))
arrayIndex++;
array[arrayIndex] += Character.toString(str.charAt(strIndex));
}
Hopefully this helps. Let me know how you get on!
Check the below code sniplet, it works fine as you said.
Let me know if any issues. (Added a syso just to validate the answer :) )
String str = "abcdefghijklmnoprqstuvwxyz";
String[] array = new String[str.length()/4 +1];
int start = 0; // 0->4->8->12..
int end = 4; // 4->8->12->16...
int length = str.length();
System.out.println("arraylength:"+array.length);
for(int i=0;i<array.length;i++) {
array[i] = str.substring(start,end);
start+=4;
end+=4;
System.out.println(array[i]);
if(end>length)
end=length;
}

Converting String elements from an array to as values into an integer Array

I'm not allowed to use methods from any class except String and IO Class
So my code snippet is:
String line = reader.readLine();
while (line != null) {
String[] elements = line.split(",");
// Array could be too big if there are multiple occurances of
// the same number
// Array length + 1 because I can't use the 0 and with a input line of
// 1,2,3 for example would be the length 3 but I would have the
// numbers 0,1,2 in the Array as my index.
String[][] placeholderMatrix = new String[elements.length+1][elements.length+1];
for(int i = 0; i < elements.length-1; i++){
placeholderMatrix[(int)elements[i]][(int)elements[i+1]] = 1;
}
line = reader.readLine();
}
In the File I'm getting are only numbers like that: 1,2,3,4,5,8,7,4
So in my splitted String Array are only Numbers but now if I want to use them as my index for my Matrix(placeholderMatrix)
My problem is in my for loop where I want to use them as my Index I can't use them because it is a String Array. Normally I would use Integer.parseInt but I'm not allowed to :/
Any ideas on how I can implement them as my Index? and any Idea how I can get the perfect length of my Matrix? Because If I get the following numbers: 1,2,2,2,3 My Matrix should only have the numbers:
0 1 2 3
1
2
3
But if I'm using elements.length+1 for the length of my Matrix I would get the numbers 0 1 2 3 4 5
Hope you could understand my problem. Sorry for my bad english and Thanks in advance.
Edit: SO i got another problem with that. If I implement the method(parseInt) of Dici and am using it in the line "placeholderMatrix[parse(elements[i])][parse(elements[i+1])] = 1;" I'm getting the error ArrayOutOfBounce because my defined Array is just the length of my splitted String Array elements. But if I define it with Integer.MAX_VALUE as my length I get a memory error because it is too big. Any ideas?
Edit2: My Task:
I have to take a row of Numbers seperated by ",". (I will split it with the String split method to get only the numbers) Now I have to create a Matrix(2 dimensional Array) and look for the number at the index i of my new String Array and the number at the index i + 1 and have to take the first Number as my column and th second as my row (or vice versa) and implement at that point a 1. Now are my Numbers I will get from 1 to Integer.MAX_VALUE so I would have to create such a big Matrix but this isn't possible because I get the MemoryError.
Error: java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at Test.main(Test.java:29)
To understand what I have to do: http://de.wikipedia.org/wiki/Adjazenzmatrix the image at the right but for numbers from to Integer.MAX_VALUE so my 2D Array has to be defined with the length of Integer.MAX_VALUE?
Edit:
So Dici asked for an example:
My Sequence could be: 1,2,5,4
So my Matrix should be:
Hope this is what you wanted Dici
But the numbers I can get from the sequence are 1 to Integer.MAX_VALUE
For converting strings to integers, you can simply implement your own integer parser, it is not complicated. You can start with this and improve it if needed.
public int parseInt(String s) {
int n = 0;
int pow = 1;
for (int i=s.length() - 1 ; i>=0 ; i--) {
String si = String.valueOf(s.charAt(i);
if (si.matches("[0-9]")) {
n += pow*(s.charAt(i) - '0');
pow *= 10;
} else if (si.matches("+|-") && i == 0)
n *= s.charAt(i) == '+' ? 1 : -1;
else
throw new NumberFormatException();
}
return n;
}
Then, I'll handle the second part of your problem. If Integer.MAX_VALuE is one of your input values, you cannot possibly allocate an Integer.MAX_VALUE x Integer.MAX_VALUE matrix. What you need to do is assign contiguous ids to your input values and record the ids in a map so that you can access easily the index of the matrix corresponding to one node value. Here is an example to get you to understand :
public void someMethod() {
int id = 0;
Map<Integer,Integer> idMap = new HashMap<>();
String[] split = reader.readLine().split(",");
int [] nodes = new int[split.length];
for (int i=0 ; i<nodes.length ; i++) {
nodes[i] = parseInt(split[i]);
if (!idMap.containsKey(nodes[i]))
idMap.put(nodes[i],id++);
}
// the map is now constructed, it should probably be stored in an attribute
int[][] placeholderMatrix = new int[nodes.length][nodes.length];
for(int i = 0; i < nodes.length; i++){
if (i > 0) placeholderMatrix[idMap.get(nodes[i])][idMap.get(nodes[i-1])] = 1;
if (i < nodes.length-1) placeholderMatrix[idMap.get(nodes[i])][idMap.get(nodes[i+1])] = 1;
}
}
There are other ways to do it, let me know if this solution is ok
You could do something like:
String keyword = "1,2,3,4,5,8,7,4";//input line from file
String replacedKeyword = keyword.replaceAll("[^\\d]", "");//except numbers replace all. Assuming one digit numbers only.
String[][] placeholderMatrix = new String[replacedKeyword.length()+1][replacedKeyword.length()+1];
char keys[] = replacedKeyword.toCharArray();
for (int i = 0; i<keys.length - 1; i++) {
placeholderMatrix[keys[i] - '0'][keys[i + 1] -'0'] = "1";
}
I couldn't really understand what you want exactly. but, if that going to help a simple method to convert String number to int:
int toInt(String number) {
int num = 0;
for (int i=0; i<number.length(); i++) {
num = num*10 + (number.charAt(i)-'0');
}
return num;
}

java split unknown length of string in every 3 parts

public static void main(String[] args) {
String brandmodel="VolkswagenGolf";
String [] splitedstring=new String[13]
//how to insert every 3 letters in splitedstring array
}
What i want is to split the above string in every 3 letters.
For example
i want to save from the above string the next
Vol,ksw,age,nGo,lf
i have read here some crazy codes but i did not understand them,i want the simplest way.
I have not learned Regex yet
Calculate the number of parts you will have and create an array:
int parts = (string.length() + 2) / 3;
String splitted[] = new String[parts];
Fill the array, using String.substring(int, int):
for (int i = 0; i < parts; ++i)
{
int x = i * 3;
splitted[i] = string.substring(x, Math.min(string.length(), x + 3));
}
Substring takes a string out of another string, using indices.
The problem is that if you take a range that goes out of the string, an exception will be thrown. So what I do, is limiting the endIndex to the string length, by using Math.min(int, int). It will always return the smallest of the two passed values.
Example of this going wrong, without Math.min():
String str = "test";
String substr = str.substring(2, 9);
This fails (Exception) because, 9 is out of the range of str. str is only 4 characters long. So, valid startIndices are: {0, 1, 2, 3} and valid endIndices are in this case: {0, 1, 2, 3, 4}.
You could use regex look-behind matching the last match plus any 3 characters:
String[] splitString = brandmodel.split("(?<=\\G...)");
The regex (?<=\G...) matches an empty string that has the last match (\G) followed by three characters (...) before it ((?<= ))
Output:
[Vol, ksw, age, nGo, lf]
There's no "crazy code" required, it's a relatively straightforward:
String[] res = new String[(s.length()+2)/3];
for (int i = 0 ; i != res.length ; i++) {
res[i] = s.substring(3*i, Math.min(3*i+3, s.length()));
}
On ideone: link.
It works for all length of String
String brandmodel="VolkswagenGolf";
List <String> splitedstring = new ArrayList<String>();
int i = 0;
while(brandmodel.length() > 2 )
{
splitedstring.add(brandmodel.substring(0,3));
brandmodel = brandmodel.substring(3);
}
if(brandmodel.length() > 0)
splitedstring.add(brandmodel);

Categories