How can I convert a Jsoup Document[] array to a String[]? - java

My question is the same as this one except that instead of a single Document I have an array (Document[]).
I normally use R, not Java, so I apologize if it should be apparent how to change the solution from the linked thread for the case of an array.
The solution for the case of a single Document object was:
String htmlString = doc.html();
My code to create the object was:
Document[] target = new Document[20];
for(int n=0; n < strvec.length;n++){
target[n] = Jsoup.connect(strvec[n]).get();
}
I tried a few things like creating the original target object as String[], putting .toString() on the end of Jsoup.connect(strvec[n]).get() and elsewhere, but these attempts were unsucessful.

it is assumed that serves is an array of String containing the URL to connect, you do not need to create another array of Document
String[] result = new String[strvec.length];
for(int n=0; n < strvec.length;n++)
result[n]=Jsoup.connect(strvec[n]).get().html();

String[] htmlList = new String[target.length];
for(int i = 0; i < target.length; i++)
htmlList[i] = target[i].html();
This loop should do what you want.

Related

How to join a part of an array

I'm working on a small side project and need to join an array from one element to another - so if my array is:
{"1","2","3","4","5"}
I would like my output to be in the form of
startingElement|elementsInBetween|endingElement
my code for this section looks like this:
int lastTwo = Integer.parseInt(String.join("",numberArray[numberArray.length-2],
numberArray[numberArray.length-1]))
This works for when I want the last two digits (in this case Strings needed to be converted to int) but I now need to join all elements except the last one.
I wrote up a solution which involved making a new array comprised of the elements of the original array minus the last element, and then using the String.join method on the new array. But I feel like this is horribly inefficient.
Edit:
Here is the testing solution I came up with:
String[] test = {"a", "b", "c","d","e","f"};
String[] test1 = new String[test.length-2];
for(int i =0; i< test1.length; i++){
test1[i] = test[i];
}
as I stated, this does technically do what I want, but I feel like a cleaner solution is available...
Try this
StringBuffer get(String[] arr){
StringBuffer buffer = new StringBuffer();
for(int i = 0; i < arr.length - 2; i++) buffer.append(arr[i]);
return buffer;
}

Create a char array for every string inside a string array

I'm trying to implement in java this little project: I want to rename the episodes of an entire season in a series using a text file that has the names of all the episodes in that season.
To that end I wrote a code that reads the text file and stores every line of text (the name of an episode) as a string array, where every element of the array stores the name of one episode. Also, I wrote a code that takes the FIRST element of that array (an array called arrayLines[]) and renames a given file.
This code works like a charm.
What I want to do next is to create a char array for every element in the string array arrLines[].
The pseudo-code i'm thinking to implement is something like this:
for(int i=0; i<arrLines.length; i++){
char line_i+1[] = arrLines[i];
}
and thus getting many arrays with the names line_1, line_2,..., line_arrLines.length, with the name of every episode stored as a char array.
How can I implement something like this?
Just use a 2-dimensional char array:
char[][] lines = new char[arrLines.length][];
for (int i = 0; i < arrLines.length; i++) {
lines[i] = arrLines[i].toCharArray();
}
If you have Java 8, you can use Streams:
char[][] lines = Arrays.stream(arrLines)
.map(String::toCharArray)
.toArray(char[][]::new);
You can use the String toCharArra method.
for(int i = 0; i < strArray.length; i++) {
char[] charArray = strArray[i].toCharArray();
}

ArrayIndexOutOfBoundsException error in parsing JSON data

I am getting ArrayIndexOutOfBoundsException at id[i] = c.getString(TAG_ID);
Here's the code:
for (int i = 0; i < 25; i++) {
JSONObject c = contacts.getJSONObject(i);
id[i] = c.getString(TAG_ID);
}
I have checked that JSON file contains 25 objects. I have also tried using i<10 but it still gives same error.
id should be an array with at least 25 elements to avoid index out-of-bound.
String [] id = new String[25];
Initialize id[] array equivalent to loop condition before entering the for loop.
Or
Add null check to array id[] size inside the for loop and Initialize array equivalent to loop condition.
You declared your id array as -
String id[] = {null};
That is size of your id array is 1. When you are trying to access 25th or 10th array you are getting the ArrayIndexOutOfBoundException.
Redefining your id array may help you -
String[] id = new String[25];
Or better you may use ArrayList then you don't have to think about the size of the array -
List<String> id = new ArrayList<String>(); //declaration of id ArrayList
Then in your for loop you may do this -
for (int i = 0; i < 25; i++) {
JSONObject c = contacts.getJSONObject(i);
id.add(c.getString(TAG_ID));
}
Hope it will Help.
Thanks a lot.

How can I store a string Matrix inside an array?

I have to store a String matrix(3x20) inside an array whose length may vary.
I am trying the following code but I am getting an incompatible types error.
How could I fix this error?
My code is:
int x=0;
String[] arrayF=new String[10];
arrayF[x]= new String[3][20];
You can't assign array this way. You should eventually assign each element of the first 2-array to the 1-d array.
Something like:
String[][] array2D =new String[M][N];
String[] array1D = new String[M * N];
for (int i = 0 ; i < M ; i++)
{
for (int j = 0 ; i < N ; i++)
{
array1D[(j * N) + i] = array2D[i][j];
}
}
arrayF is an array of strings, so each element in arrayF must be a string (by definition of the array).
What you are trying to do is is put an array (new String[3][20]), instead of a string, in each element of arrayF, which obviously contradicts it's definition (hence the incompatible types error).
One solution for what you want might be using a 3-d array of strings:
String[][][] arr = new String[10][3][20];
arrayF is one dimensional array with String type.
You cannot add two dimensional array to arrayF. For dynamic array size, you should use ArrayList.
List<String[][]> main = new ArrayList<String[][]>();
String[][] child1 = new String[3][20];
String[][] child2 = new String[3][20];
main.add(child1);
main.add(child2);
Refer to
Variable length (Dynamic) Arrays in Java
use something like this:
String [][] strArr = new String[3][20];
ArrayList<String[][]> tm = new ArrayList<String[][]>();
tm.add(strArr);

How to Convert a vector string to simple string

I need to convert a string vector in a simple string. I do not know how to proceed.
I tried various solutions such as
for(int i=1; i < easy.length; i++){
easyPuzzle = easy[i].toString();
}
System.out.println(" " + easyPuzzle);
but this solution prints only the ith element and not the entire string vector.
Use toString in Arrays class
Arrays.toString(easy);
You keep reassign a new value to easyPuzzle when you really want to concatenate:
easyPuzzle += easy[i].toString();
If easy.length is large, it might make sense to use a StringBuilder which is more efficient at concatenating than String:
StringBuilder builder = new StringBuilder();
for(int i=1; i < easy.length; i++){
builder.append(easy[i].toString());
}
easyPuzzle = builder.toString();
Also by starting your for loop at i=1 you exclude the first item. Not sure if it is on purpose or not. If not, start at i = 0.
Alternatively, to save the pain of writing the loop yourself, you can use #Manoj's answer which replaces your code by one line.
I recommend to you use StringBuilder with append(<data>) method and then convert it to String.
StringBuilder data = new StringBuilder();
for(int i = 1; i < easy.length; i++){
data.append(easy[i].toString());
}
easyPuzzle = data.toString();
String is immutable so work with it is much more consume. When you work with String, i recommend to you use StringBuilder, is more effective and faster.
Update: #Manoj answer is very usefull.

Categories