String and imag array random in NetBeans? [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 8 years ago.
Improve this question
How can I generate a String and an image array in NetBeans? I try to make the arrays like
string{"lion","cat","dog","bird"};
How can I use the Random class to get s[i] where i is random?

String [] animals = {"lion","cat","dog","bird"};
int rndindex = (int)(Math.random()*number_of_element);
String rndAnimal = animals[rndIndex];
Make the array, get a random number between 0 and n inclusively where n is the number of elements in the array. Then retrieve that element.

java.util.Random rand = new java.util.Random();
int index = rand.nextInt(4);
System.out.println(string[index]);

If I understand your question, one solution is to use Collections.shuffle(List). I know, you wanted to do it with a String[] bute we can use Arrays.asList(T...) and use the fact that this List<String> is backed by a String[] like so,
public static void main(String[] args) {
String[] animals = { "lion", "cat", "dog", "bird" };
System.out.println(Arrays.toString(animals));
Collections.shuffle(Arrays.asList(animals));
System.out.println(Arrays.toString(animals));
}
Output is
[lion, cat, dog, bird]
[cat, lion, dog, bird]
Edit
If you just want one element at a random position in the array, that would be
String[] animals = { "lion", "cat", "dog", "bird" };
System.out.println(animals[rand.nextInt(animals.length)]);

Related

Create test which add country code in a loop [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
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);
}

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.

I want to split into `String and `int` arrays 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 7 years ago.
Improve this question
I have text data and I want to split into String and int arrays Java
String text = "one1two20three300four4000";
Desired output:
String str[] = "one","two","three","four";
int num[] = 1,20,300,4000;
This one will work for you in Java :
public static void main(String[] args) {
String text = "one1two20three300four4000";
String arr1[] = text.replaceFirst("^\\d+", "").split("\\d+");
String arr2[] = text.replaceFirst("^\\D+", "").split("\\D+");
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2)); // parse each value as an in using Streams (preferably) or a loop.
}
O/P :
[one, two, three, four] [1, 20, 300, 4000]
PS : In java 8 Use int[] arr2Int = Arrays.stream(arr2).mapToInt(Integer::parseInt).toArray(); to convert your String array to int array.

Need to randomize Array of Strings to be returned as both ques. and answer for jeopardy program [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
String[] am = new String[10];
am[0] = "Sam";
am[1] = "Sam";
am[2] = "Sam";
am[3] = "Sam";
am[4] = "Sam";
am[5] = "Sam";
am[6] = "Sam";
am[7] = "Sam";
am[8] = "Sam";
am[9] = "Sam";
For a jeopardy game, random questions per GUI Jpanel button click
Approach 1: Shuffling the list
You can shuffle your list using:
List<String> list = Arrays.asList(am);
Collections.shuffle(list);
Even better is starting with an ArrayList, and adding your possibilites to that ArrayList, completely ignoring the String[] array
List<String> list = new ArrayList<String>();
list.add("value1");
list.add("value2");
...
Collections.shuffle(list);
You can then retreive an element from the shuffeled list using get(index), i.e. :
String random = list.get(0);
Approach 2: Randomizing the index
If you insist on using the String[]-array, you can do
String random = am[new Random().nextInt(am.length)]
This will create a Random-object, and generate a random integer ranging from 0 to the length of your array minus one. Once this integer is generated, it will be used as an index for to get an element from your String[]-array.
You can use the same approach on an ArrayList:
String random = list.get(new Random().nextInt(list.size));

Taking values from multiple arraylist in order [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 9 years ago.
Improve this question
I have 4 array list
ArrayList<String[]> split_url = new ArrayList<String[]>();
ArrayList<String> mediaID = new ArrayList<String>();
ArrayList<String> productID = new ArrayList<String>();
ArrayList<String> clusResult = new ArrayList<String>();
and each arraylist contains some values. i want to take first value from each arralist and save that into one row in database then take second values from each database and save that to next row and so on.... how can i do that ?
1) first initialize all the array lists with zeros upto a certain length l.
2) add some values to each array list.
3) run the a loop like this,
for(int x=0;x<l;x++)
{
String a=split_url(x);
String b=mediaId(x);
String c=productId(x);
String d=clusResult(x);
// now store a,b,c,d as a record in your database;
}

Categories