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
is there an equivalent code in groovy for the following code in java:
import java.util.Arrays;
import java.util.List;
import java.util.Random;
class Text {
public static void main(String [] args) {
String [] array = {"hello", "world","Pasci","Jenny"};
List <String> list = Arrays.asList(array);
{
Random rand = new Random();
System.out.println("String from list: " + list.get(rand.nextInt(list.size())));
}
}
}
Thank you
Groovy's syntax is lighter and not as verbose as Java:
def list = ["hello", "world","Pasci","Jenny"]
Random rand = new Random()
println "String from list: " + list.get(rand.nextInt(list.size()))
Side note: Groovy is almost a super-set of Java, meaning, you can write almost any Java code in a groovy file and it'll compile and run.
Related
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 4 years ago.
Improve this question
I am trying to calculate the common string using lcs, but this algorithm only calculates 1 string. What can I use instead?
LCS = "aaabbbcccxxx" and "aaadddccc" result: "aaa"
but what i want= "aaaccc"
help please:)
You can apply your LCS algorithm once to get the "aaa" result, then remove this result from both strings, and re-apply your LCS algorithm to get the "ccc" result. Finally you will concatenate the temporary results.
Your java code in the main class may look like the following (assuming that you have a method LCS(String string_1 ,String string_2) performing yourLCS algorithm:`
public static ArrayList<String> temp_results;
public static String string_1,string_2,temp_result,final_string;
public static void main(String args[]) {
while (temp_result != null && !temp_result.equals("")) {
temp_result = LCS(string_1,string_2);
string_1.replaceAll(temp_result,"");
string_2.replaceAll(temp_result,"");
temp_results.add(temp_result);
}
for (String iterator_string : temp_results){
final_string = final_string + iterator_string;
}
System.out.println("This is the result "+final_string);
}
public static String LCS(String string_1, String string_2){
return ""; //put your actual LCS logic here, you should not return an empty string!
}
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 4 years ago.
Improve this question
I get this value in String
kyc_CWaccountOperatorName|DANIYAL,kyc_cnic_ind|9110129505705,kyc_fatherName|Abujan,kyc_motherMaidenName|MOTHER,kyc_CWmobileNumber|03312551746,kyc_CWdateOfBirth|20/02/1993,kyc_cnicDateOfExpiry|2027-02-20,kyc_CWplaceOfBirth|KHI,kyc_mailAddHouseFlat No|Dha,kyc_city|Abbottabad
I want to get value of kyc_cnic_ind like
kyc_cnic_ind=9110129505705
Below is the solution for your problem:
import java.util.*;
import java.lang.*;
class Rextester
{
public static void main(String args[])
{
String str = new String("kyc_CWaccountOperatorName|DANIYAL,kyc_cnic_ind|9110129505705,kyc_fatherName|Abujan,kyc_motherMaidenName|MOTHER,kyc_CWmobileNumber|03312551746,kyc_CWdateOfBirth|20/02/1993,kyc_cnicDateOfExpiry|2027-02-20,kyc_CWplaceOfBirth|KHI,kyc_mailAddHouseFlat No|Dha,kyc_city|Abbottabad");
String[] ar = str.replace("|","=").split(",",0);
for(String s : ar)
System.out.println(s);
}
}
You can check output on the below link:
https://ideone.com/kzBYfl
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
hi have some text like this :
What would you like to eat?
شما چي ميل داريد؟
I’d like a bowl of tomato soup, please.
لطفا يک کاسه سوپ گوجه فرنگي برام بياريد
The waiter seems to be in a hurry to take our order.
گارسن بنظر مياد خيلي عجله داره که سفارش ما رو بياره
i want to Detect and put english Sentence in one array and Persian Sentence in another array
How can i do ؟
Assuming all your text is in a file and that English and persian translations are on different lines.
What you need to do is read each line from the file and check if it is ASCII or not.
How do you check that?
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
public class StringUtils {
static CharsetEncoder asciiEncoder =
Charset.forName("US-ASCII").newEncoder(); // or "ISO-8859-1" for ISO Latin 1
public static boolean isPureAscii(String v) {
return asciiEncoder.canEncode(v);
}
public static void main (String args[])
throws Exception {
String test = " برام ";
System.out.println(test + " isPureAscii() : " + StringUtils.isPureAscii(test));
test = "Real";
System.out.println(test + " isPureAscii() : " + StringUtils.isPureAscii(test));
/*
* output :
* برام isPureAscii() : false
* Real isPureAscii() : true
*/
}
}
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 6 years ago.
Improve this question
how to get Name and UserName from this JSON
String str="{Name=jack,UserName=Jacki}"
in java android
Try this, code will use scanner class and its findInLine() method
String str="{Name=jack,UserName=Jacki}";
Scanner sc=new Scanner(str);
sc.findInLine("Name=");
if(sc.hasNext())
{
System.out.println(sc.next()); //This will print Name you can store it in variable as well
}
sc.findInLine("UserName=");
if(sc.hasNext())
{
System.out.println(sc.next()); //This will print UserName you can store it in variable as well
}
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Sample
{
public static void main (String[] args) throws java.lang.Exception
{
String str="{Name=jack,UserName=Jacki}";
String[] str1=str.split(",");
System.out.print(str1[0].substring(1));
System.out.print(str1[1].substring(0,str1[1].length()-1));
}
}
So in this method you basically take the string and split it on the ",". That gives you {Name=jack and UserName=Jacki}. The next two statements just parse these 2 new strings and remove the "{" and "}".
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 a text file with lots of words. I'd like to remove words that contain repetitive letters(eg. zoos - contains 2 o's). What is the best way to do this ?
Regular expressions may work for you. Something like
Pattern p = Pattern.compile("([a-zA-Z])*([a-zA-Z])\\2([a-zA-Z])*");
Matcher m = p.matcher("zoo");
System.out.println(m.matches());
Just add a loop to try every word in file and if m.matches() == true - delete it.
By the way, this won't work if you tYpE lIkE ThIs
This is an example using a regex and the stream api:
package demo;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Demonstration
{
public static void main(String[] args)
{
List<String> input = Arrays.asList( //
new String[] {"a", "bb", "ccc", "ded", "ff", "ghi", "jkll"});
// Prints [a, ded, ghi]
System.out.println(removeWordsWithRepetitiveCharacters(input));
}
private static List<String> removeWordsWithRepetitiveCharacters(List<String> words)
{
return words.stream() //
.filter(word -> !word.matches(".*(\\w)\\1+.*")) //
.collect(Collectors.toList());
}
}