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
*/
}
}
Related
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 2 years ago.
Improve this question
import java.util.Scanner;
public class TextProcessing
{
public static void main(String[] args)
{
String sentence, wordToBeTargeted, wordToBeReplaced, output;
boolean wordToCheck;
Scanner myInput = new Scanner(System.in);
sentence = myInput.nextLine();
do
{
wordToCheck = true;
System.out.println("Please enter the word for replacement:");
wordToBeTargeted = myInput.nextLine;
if(sentence.toLowerCase().indexOf(wordToBeTargeted.toLowerCase()) == -1)
{
wordToCheck = false;
System.out.println("This word cannot be found in the sentence!")
}
else
{
System.out.println("Please enter the word you would like to replace with:");
wordToBeReplaced = myInput.nextLine();
}
}while(wordToCheck = false);
}
}
}
Write a file named TextProcessing.java that will replace all occurrences of a word in a string with another word
The expected outcome is like this:
I won't write the code out - you should still get something out of the exercise, but will give some direction. The first approach that comes to mind:
Split the first input on ' ' into a list
Iterate through the list, conditionally changing values based based on two input strings
As you're iterating you can either output into a new string / stringbuilder, or directly write to console depending on the requirements
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 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 want to get a user input number to print out "Color 1 Color 2..." etc. depending on what number the input is.
I want to do this but in java, and I'm not quite sure where to find it.
How to iterate a for loop for a user input in Java?
That will do the work, using only standard java classes (java.io.Console):
import java.io.Console;
public class Consoler {
public static void main(String[] args) {
final Console console = System.console();
console.printf("How may times?\n");
final String line = console.readLine();
try {
final int quantity = Integer.parseInt(line);
for (int i = 1; i <= quantity; i++) {
System.out.printf("Color %d ",i);
}
System.out.println();
} catch (final NumberFormatException e) {
System.err.println(line + " is not a number.");
}
}
}
java.util.Scanner is probably what you're looking for. As for the for loop, you should google Java for loop and read up on how they work. Then combine the two concepts.
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
I have csv file with below data How I can remove empty rows in java through regex I tried using ^,+$ but Its not working
"Temp-A007","Default","Importing","","",""
"","",""
You can do it through RegEx. Simply use input.replaceAll("(\"\\s*\"\\s*,?)", "");
Output
input : "Temp-A007", "","Default","Importing","","",""
output : "Temp-A007", "Default","Importing",
Code
public class Test {
public static void main(String args[]) {
String input = new String("\"Temp-A007\", \"\",\"Default\",\"Importing\",\"\",\"\",\"\" ");
String output = input.replaceAll("(\"\\s*\"\\s*,?)", "");
System.out.println("input : " + input);
System.out.println("output : " + output);
}
}
An "empty" row will have at most commas, quotes, and spaces, no? How about:
^[", ]*$
?
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 years ago.
Improve this question
I have searched nearly all pages on Stackoverflow but still have not found out how to do this. I have the following string (which is being parsed from file):
f: (matchQuan
(Recipe
(Unique FoodType "slurpee" "xxx-xxx-eee-ddd"))
(Unique IngredType "slurpee" "qqq-rrr-sss") "slurpee"
(Cup-Vol 12)).
Now, I want to parse this string again and extract the string matchQuan, the slurpee, the string Unique and FoodType and Recipe and the ID numbers i.e. xxx-xxx-eee-ddd.
How would I do something like this with multiple extractions from a single string? I can't use Scanner.next() I don't believe because it advances to the next token in string.
Thanks!
Will this help for you?
public static void getString() {
String str = "f: (matchQuan " +
"(Recipe "+
"(Unique FoodType" + " slurpee"+ " xxx-xxx-eee-ddd"+
"(Unique IngredType"+ " slurpee"+ " qqq-rrr-sss"+" slurpee" +
" (Cup - Vol 12)).";
String newStr=str.replaceAll("\\(","").replaceAll("\\)","");
String[] arrStr=newStr.split(" ");
for (int i=0;i<arrStr.length;i++){
System.out.println(arrStr[i]);
}
}