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