Create test which add country code in a loop [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to create a test which will have base URL (https://www.horisen.com) and added part with country code (i.e https://www.horisen.com/se). Problem for me is there are 12 countries to be changed.
I tried to create if loop without any success.
So, in summary, I have to go through all 12 different languages, open those pages in current languages, and continue with next lang. I suppose that I need an array of 12 country codes and call that in a loop, but do not know how to acchieve this.
Thank you in advance
String url = htps://www.horisen.com/
int[] array;
array = new int[6];
array[0] = de;
array[1] = se;
array[2] = dk;
array[3] = fr;
array[4] = en;
array[5] = fi;
for(int i=0; i++) {
do not know how to add after string url country code on the ned
}

Next time, please put some actual java code (that compiles) in your example. Perhaps you're looking for something like this:
String url = "https://www.horisen.com/";
String[] countryCodes = {"de", "se", "dk", "fr", "en", "fi"};
for (String countryCode : countryCodes) {
String countryUrl = url + countryCode;
System.out.println(countryUrl);
}

Related

How to find the Index of string in java array from the given string value [closed]

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 9 months ago.
Improve this question
creditTIDstatusArray=[93312263-1-09722612223, 99802001-1-09102842369, 99802002-1-09102842369];
creditTIDstatusList.addAll(Arrays.asList(creditTIDstatusArry));
searchValue="99802002-1".
int retval=creditTIDstatusList.indexOf("99802002-1");
System.out.println("The element at index is:" retval);
Output: 2
Please let me know how I can find the index of the given above(searchValue)element.
As you never have posted the reproducible code.
Assumptions
creditTIDstatusArray is a String type array.
Your search query always stays in front of each String value in the array.
Multiple indexes may start with the same search value.
String[] creditTIDstatusArray=new String[]{"93312263-1-09722612223", "99802001-1-09102842369", "99802002-1-09102842369"};
String searchValue="99802002-1";
for (int i = 0; i < creditTIDstatusArray.length; i++) {
if(creditTIDstatusArray[i].startsWith(searchValue)){
System.out.println("Index :" + I); // this will print all the indexes that starts with searchvalue
}
}
I am not sure why you have added the array into a list and then searched index of because it will never work as you are searching only part of a String rather than the whole value or object.

Java string format in variable like Python [closed]

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 1 year ago.
Improve this question
In Python, below code can be run:
str = "i = %s"
print(str % i)
or
str = "i = {}"
print(str.format(i))
I know that Java can format string using
String str = String.format("i = %s", i);
But, I want to separate "i = %s" and i to make "i = %s" variable and use it on other codes, like below:
String pathFormat = "/v2.0/%s/search";
System.out.println(String.format(pathFormat, userId);
Is it possible in Java?
Yes, you can do that in Java just as in your last example.
String numberFormat = "i = %s";
int i = 17;
System.out.println(String.format(numberFormat, i));

how to remove multiple characters by their indice from a string in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I remove multiple characters by their index from a string. I thought to use StringBuilder's deleteCharAt function. But if I delete char one by one, I will not keep track the right index.
For example :
String test = "0123456789"
int[] removingIndice = int[] {2, 4, 0};
// remove character from string
// result = "1356789"
Create a new string builder,
iterate on string, add elements to builder if its index not in the array
StringBuilder sb = new StringBuilder("");
for(int i = 0; i< test.length; i++){
if( !ArrayUtils.contains(removingIndice, i))
{
sb.append(test.charAt(i));
}
}
test = sb.toString();
String is immutable in Java, so you will need to create a new String with characters at positions 0,2,4 removed. As one option, you may use StringBuilder for that, as answered here: How to remove single character from a String
I think, you need to redesign the task:
"new string must contains all characters except ..."
Now, seems weak initial data structure and the goal.

Java - Two-dimensional array declaration issue [closed]

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 8 years ago.
Improve this question
I have just declared a two-dimensional array in my program:
String[][] choices = new String[4][];
choices[0] = new String[10];
choices[1] = new String[20];
choices[2] = new String[20];
choices[3] = new String[20];
However, IntelliJ Idea, the IDE I am currently using, threw out a bunch of errors starting from the second line. What is wrong with my code? I have checked other questions regarding two-dimensional arrays and found the exact same syntax being used.
EDIT: Based on my code above, I want the arrays to be of different length. Is that possible to make?
Your code snippet works fine, and you can have inner String arrays of different length, e.g.:
String[][] choices = new String[2][];
choices[0] = new String[1];
choices[1] = new String[2];
choices[0][0] = "Foo";
choices[1][0] = "Bar";
choices[1][1] = "Baz";
System.out.println(choices[0][0] + " " + choices[1][0] + " " + choices[1][1]);
Ideone demonstration.
In short, your problem is elsewhere. Read the errors since that's what they're for. They're often descriptive enough.

Removing periods from strings inside tables. easy. - java [closed]

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 8 years ago.
Improve this question
I have this string I got from a table via 'string.split(" ");' the problem I have is one of them should include a period. How would I go about searching for this period. I do know I have to search for it because I code in Lua. Though we used String.find() methods.
What is the way I would remove a period from a string in a String[] table?
Thanks in advance :)
This is for school.
If I understand you correctly, you have a String that you've split by delimiting on a space. You now want to search for a period in the resultant array of Strings.
for(String s : stringArray) {
if (s.contains(".")) {
//do something
}
}
Unfortunately, I'm not that clever with RegExp, but you could...
String s = "This.is.a.test";
while (s.contains(".")) {
s = s.replace(".", "");
}
System.out.println(s);
I'm sure there's a wonderful single line RegExp to do the same thing
Updated based on comments
Because String is not mutable, you will need to reassign it back to the original array
String[] apples = {"one", "two.", "three"};
for (int index = 0; index < apples.length; index++) {
String s = apples[index];
while (s.contains(".")) {
s = s.replace(".", "");
}
apples[index] = s;
}
for (String s : apples) {
System.out.println(s);
}

Categories