Java get random class [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I am trying to figure out an effective way of getting a random class from my project. There are nine different classes(ten including the startup), each one with a different behavior(which extend a declaratoid class that is used in the main function). I need to be able to have the function to run differently on each startup. What do I need to do? Edit:Thanks for the answer, but I am now running into another problem. I have to pass the result as the first parameter in another the function now.

Create a factory method that gets a random number and creates an object based on that, in a switch:
public static YourInterfaceType createRandom() {
Random r = new Random();
switch(r.nextInt(10)) {
case 1: return new FirstType();
case 2: return new SecondType();
// etc
default: return new LastType();
}
}
Edit More exact definition of words. :)

you could generate a random number from 1 - 9 and use a switch statement where each case calls a different class

Related

How to expand the Regex to actual values [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I am looking for Opensource Java library which could help me in expanding all the possible values out of Numeric Regex, for example: if I give a range 1234[7-9] as input, it should output 12347,12348, 12349, similarly taking care of 123[4-6][7-9], which would translate to 12347, 12357, 12367 so on. Instead of reinventing wheel I would like to know if there are any libraries which could do this. This is only for Numeric regex with defined range.
I have once tried out Xeger which was good enough for such simple expressions similar to yours above. You will also need automaton jar package, that you can download as a library in order to use Xeger.
Example how to use:
String regex = "123[4-6][7-9]";
Xeger generator = new Xeger(regex);
Set<String> generated = new HashSet<>();
for (int i = 0; i < 100; i++) {
generated.add(generator.generate());
}
System.out.println(generated);
//[12367, 12348, 12359, 12349, 12357, 12368, 12369, 12347, 12358]

Finding similar strings in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I was wondering is there a kind of algorithm or pattern which allows you to compare and find similar words
It will be easier if I use example, here it is:
Supposing that we have a strig:
String keywords = "Mummy's girl";
ArrayList = "Mom, cat, dog, girlfriend, house, mum, girls, fire";
I want to get in result those words (cause they're similar or the same in writing) = "Mom, girlfriend, mum, girls, girl"
Your question is little bit unclear. But in java you can use substring function.
String n = in.next();
String a = n.substring(0,3);
Here, a = Mum . Then go through all elements in the arraylist and find the similar word. In substring 0 is starting point and 3 is ending point.

Unable to ignore selected elements while comparing two XML objects using XML Unit [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
While comparing two XML objects considering the following constraints :
1)Ignore white spaces
2)Ignore the array order
3)ignoring some elements containing date
Please help me consider the 3rd constraint
The following is the code that I tried :
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML));
List<?> allDifferences = diff.getAllDifferences();
if(allDifferences.size() == 0)
System.out.println("Test Passed");
else
System.out.println("Test failed due to following differences: ");
for(int differences = 0; differences < allDifferences.size(); differences++) {
System.out.println(allDifferences.get(differences));

creating and checking URL in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I have a use case where values my function accepts could be of type:-
1. mailto:abc#abc.com
2. tel:3402904323
3. http(s)://www.google.com
4. www.google.com
5. //www.google.com
6. /content/abc/def/index
7. javascript:;
8. blank
9. #
10. http(s)://www.google.com/index.html#abc
11. http(s)://www.google.com/index.html
All these are to be treated as valid and i have to create a URL out of them for e.g. for input 6 i would need to append (.html). For 7th i might need to escapeHtml and rest could be returned as is.
Is there a standard java API to do this or any standard logic i could put into doing this.
Please help.
Use String#matches with the the following regular expressions, respectively.
(?i)mailto:[a-z0-9]{1,}[.a-z0-9]*#[a-z0-9]{1,}[.a-z0-9].[a-z0-9]{2,}
tel:[0-9]{10}
http[s]{0,}://
www\.[a-z0-9\-]{1,}\.com
//www\.[a-z0-9\-]{1,}\.com
/content/([a-z0-9\.\-]{1,}/*)*
nice try
[^\w\W]*
#
http[s]*://[a-z0-9-]{1,}\.[a-z0-9-]{1,}\.com/([a-z0-9-_%\?\=]{1,}/)*[a-zA-Z0-9_-]{1,}\.html#[a-zA-Z_\-\.]{1,}
http[s]*://[a-z0-9-]{1,}\.[a-z0-9-]{1,}\.com/([a-z0-9-_%\?\=]{1,}/)*[a-zA-Z0-9_-]{1,}\.html[a-zA-Z_\-\.]{1,}
Here is an example of using a regular expression, in this case it will return a boolean value (it matches or it doesn't match)
public static void main(String[] args)
{
String pattern = "http[s]*://[a-z0-9-]{1,}\\.[a-z0-9-]{1,}\\.com/([a-z0-9-_%\\?\\=]{1,}/)*[a-zA-Z0-9_-]{1,}\\.html#[a-zA-Z_\\-\\.]{1,}";
String string = "http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#matches-java.lang.String-";
/*
* This will print the boolean literal true or false
*/
System.out.println( string.matches(pattern) );
}
Just try to create a java.net.URI with the string. If there is a syntax error you will get a URISyntaxException. All the above pass provided that "http(s)" really means "http" and "https" separately. If you need to restrict the scheme to what is shown above, just get the scheme and look it up in a table, and fail it if isn't there.

java expression to logical operation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Is there a way in java we can convert an expression (xml or any other) to logical operations.
for example I have a property
prop01=Achivment:APPCom,done&&TODO:getforecast,!done;Achivement done
is there a way I convert it to java code like
Map userData = getUserData();
Map achivements = userData.get("achivements");
Map TODOs = userData.get("TODOs");
String achiv = achivements.get("APPCom");
String todo = TODOs.get("getforecast");
if(achiv == "done" && todo != "done")
system.out.println("Achivement done"); // part after ; in expression
any third party available for this kind of task?
I don't think this can be done with any third party library directly. You may need to use some library like Antlr to write a translator to translate from your expression to your Java code.

Categories

Resources