Spliting the string by number of chars [duplicate] - java

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;
}

Related

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;
}

Reverse Char Array [duplicate]

This question already has answers here:
How do I reverse an int array in Java?
(47 answers)
Closed 5 years ago.
I am working on a code for homework where we have to use a char array holding a sentence and reverse the order of array so that the words are in opposite order in java for example "I am a house" should put out "house a am I" I am stuck on how to actually step through and order the array so the words go in this order any tips will help.
The code i have reverses the whole array but it does not put reverse it word by word
if(sentence.length%2 == 0)
{
int middleR = sentence.length/2;
int middleL = middleR - 1;
for(int i = middleR; i < sentence.length; i++)
{
char temp = sentence[i];
sentence[i] = sentence[middleL];
sentence[middleL] = temp;
middleL--;
}
}
else
{
int middle = sentence.length/2;
int end = sentence.length -1;
for(int i = 0; i < middle;i++)
{
char temp = sentence[i];
sentence[i] = sentence[end];
sentence[end] = temp;
end --;
}
}
Split the text into an array of Strings (String.split), put the array into a List (Arrays.asList), revers the list (Collections.reverse), get String array from the list (List.toArray)

Java - Large String need to split with different field length

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]);
}

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;
}

how to compare numbers in string with string? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
i have array which is:
String[] array= {azem, 1 , soaib, 3}; // a[0]= azem , a[1]= 1, a[2]=soaib, a[3]= 3
My code is not printing any thing. I also saw ascii table and get the values of numbers in string that is from 48 to 57 but no printing.
Mycode:
String input = "1"; // integers as input
//String takeinput;
// String input = "48" + takeinput;
for(int i=0; i<array.length; i++){
if(array[i]== input){
System.out.print(array[i-1]);
System.out.print(array[i]);
}
In Java, the == compares if the two references point to the same object in memory, not whether they actually point to equivalent strings.
Use array[i].equals(input) instead.
This,
String[] array= {"azem", "1" , "soaib", "3"}; // a[0]= azem , a[1]= 1, a[2]=soaib, a[3]= 3
String input = "1"; // integers as input
//String takeinput;
// String input = "48" + takeinput;
for(int i=0; i < array.length; i++){
if(array[i].equals(input)) { // Modified
System.out.print(array[i-1]);
System.out.print(array[i]);
}
Or better, if you know every odd position (indexed 0) would be number, you can rewrite the code as,
int input = 1; // Modified : integers as input
for(int i=1; i < array.length; i += 2){
if(Integer.valueOf(array[i]) == input) { // Modified
System.out.println(array[i-1] + ":" + array[i]);
}
This would be a cleaner and a faster solution.
Did you try Integer.parseInt(...) while comparing strings with numeric values?
Integer.parseInt("26") == Integer.parseInt("56")
Update
You can also try comparing strings with .equals() method, as below.
array[i].equals(input);
Also, use println() instead of print() to be sure of what is printing.
Shishir

Categories