Why Random random = new Random()? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi guys rather new to java, I am just having a bit of problem understanding some of the basics.
When you declare a random value you use Random random = new Random(), can someone please explain what the first Random, second random and "new Random()" does and the logic behind it?

Just a note, the first commenter is correct, you probably could have found this through a bit of Googling, but here's the answer as best as I can explain it:
Let's take that code:
Random random = new Random();
The first random just says what type of data the variable is going to store - in this case, "Random." The second random is the name of the variable. You can call this almost anything you want, "random," "ran," even something completely unrelated like "ThisIsAVar." Then you set the new variable (using the "=") to a new Random type.
A more generic example would be (as "Random" in the last explanation messes with the grammar):
string NewString = new String("Hello there!");
You're creating a new variable called NewString with type of string, and setting that to a new String type with the parameter "Hello there!"

Related

code for buildShortArray for audiomixing [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to android and i just want to create a dj player. but for that the starting step is mixing two files. The rough code for that i found on the following link but in that i did not understand how to code for buildShortArray(music1).
I already tried this code but got stuck in the above mentioned method's code.
thank you in advance for help.
Docs here:Mix two files audio wav on android use short array
The code from the link does not show the buildShortArray method.
You need to transform List<Short> into array short[]:
List<Short> music1 = ...;
short[] arrayMusic1 = buildShortArray(music1);
You could write the method buildShortArray like this:
public short[] buildShortArray(List<Short> list) {
short[] array = new short[list.size()];
for(int i = 0; i < list.size(); i++) {
array[i] = list.get(i);
}
return array;
}
However, i'd like to warn you, copy-pasting code is never a good idea.

How to add Dynamic String value to String array in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string name called label,which is dynamically having values.I want to add this every time generated values into array list.
List<String> container = new ArrayList<String>(Arrays.asList(label));
I tried this way but its printing only the last value, where I'm doing wrong, any suggestion?
In this every time the dynamically changing value of label will be added into the container.
List<String> container = new ArrayList<String>();
String label = "Sample1";
container.add(label);
label = "Sample2";
container.add(label);
label = "Sample3";
container.add(label);
System.out.println("container : "+container);
But you are creating new list every time. using the label variable
List<String> container = new ArrayList<String>(Arrays.asList(label));
I think everytime you are resetting your container variable by the declaration:
List<String> container = new ArrayList<String>(Arrays.asList(label));
Ideally you should do it only at first time, after that simply add the newly created label:
container.add(label);

How to create random cell number for testing purpose [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to create a random cellnumber 07939393914 for automation testing purpose.
Last 079393(5 digits) digits should change randamly.. each time test runs..
Can any one suggest JAVA code for this ? or else Selenium Java code ?
Thanks
Is this a number or a String? I ask as it has a leading zero.
Take you initial number as 7939300000
then add to it Math.round(Math.Random()*10000)
If you want it as a String, take your string as "079393" and use Integer.toString on the result above, then concatentate them
Use RandomStringUtils class.
String randomNumbers = RandomStringUtils.randomNumeric(5);
String phNo = 079393+randomNumbers;
var num=Math.ceil(Math.Random()*100000)
for random first 5 digit numbers
and add
var your_last_5digits; //as string to your last 5 digits
cellNumber='0'+num+your_last_5digits;

Need to get string part between two / in a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to java need help
the main string i have is
eg.
"String1/string2/string3/string4/all_free.sdx"
"file1/file2/string3/string4/all_free.sdx"
the end result i need is to be able to isolate and get string3
i can indexof but not able to achieve it in few steps need brainy people help as i am new to JAVA
If you know it's the third item that you want to get, one simple approach could be using split method, like this:
String myString = "String1/string2/string3/string4/all_free.sdx";
String string3 = myString.split("/")[2];
The call of split("/") produces an array of strings like this:
{"String1", "string2", "string3", "string4", "all_free.sdx"}
Now you can apply the subscript operator to grab the element that you want.

Converting the string phone number to integer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am fairly inexperienced in Java as well as Android. I am trying to retrieve a phone number stored in one of the contacts of the android phone emulator. While I am successful to fetch it, the number has been returned in a string in a format like "(987) 654-3210".
I want to convert it to integer or long. How can I do that? If I use Integer.parseInt(String number), it returns a NumberFormatException. Failed while tried using Long.valueOf(String number) too. What should I do then? I want it like "9876543210" without any braces or hyphens.
using the long for storing number will be better
String str="(987) 654-3210";
String stt=str.replaceAll("\\D+","");
long num= Long.parseLong(stt);
System.out.println(num);
This should be simple. You could use regex in java to do this like below
phoneStr = str.replaceAll("\\D+","");
This will delete the non digits from the string and give you only numbers. Then you can use
int number = Integer.parseInt(phoneStr);

Categories