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
For example I have this string, where email addresses are differently formatted:
"a.b#c.d" <a.b#c.d>|"Michal pichal (michal.pichal#g.d)" <michal.pichal#g.d>|Melisko Pan <melisko.pan#domena.sk>
I need to extract the email addresses in a form of:
a.b#c.d|michal.pichal#g.d|melisko.pan#domena.sk
My idea was to get any char near # from group [0-9][A-Z][a-z]#.- but I do not know how. Please help with some hint.
This regex extracts emails out of your string:
public static void main(String[] args) {
Pattern pattern = Pattern.compile("[\\w.]+#[\\w.]+");
Matcher matcher = pattern.matcher("\"a.b#c.d\" <a.b#c.d>|\"Michal pichal (michal.pichal#g.d)\" <michal.pichal#g.d>|Melisko Pan <melisko.pan#domena.sk>\r\n");
while(matcher.find()){
String group = matcher.group();
System.out.println("group="+group);
}
It prints:
group=a.b#c.d
group=a.b#c.d
group=michal.pichal#g.d
group=michal.pichal#g.d
group=melisko.pan#domena.sk
import java.util.Scanner;
class test{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String res = ""; //This holds the final result
for(int i=0; i<str.length(); ++i){ //Loop through the entire string
if(str.charAt(i)=='<'){
String build = "";
for(int j=i+1; j<str.length(); ++j){
if(str.charAt(j)=='>'){
break; //Break when we hit the '>'
}
build += str.charAt(j); //Add those between < and >
}
res += build + "|"; //Add the pipe at the end
}
continue;
}
System.out.println(res);
}
}
This ought to do it.
Just run simple nested loops. No need for regex.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
If i have an input smt. like that:
1,10;3,3;4,1. Lets say String input.
How can I split it in ";", so the result could be like this:
[1,10]
[3,3]
[4,1]
Thanks!
I think you want to do something like this.
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input;
//your input is 1,10;3,3;4,1
input = scanner.next();
String[] splitted = input.split(";");
//save the new list or array
ArrayList list = new ArrayList();
for (int i = 0; i < splitted.length; i++) {
System.out.println(splitted[i]);
//for later usage
list.add(splitted[i]);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
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 5 years ago.
Improve this question
I have a problem in my assigment, I want to invert a word like "Indonesia" to "aisenodnI".
public static void main (String[]args){
balik();
}
public static void balik (String nama){
for ( i>=nama.Length-1 ; i=0 ; i-- ){
balik = balik + nama.Length();
System.out.print(balik);
}
}
You can use StringBuffer or StringBuilder for this task, StringBuilder would be my choice since its more efficient. its not thread safe so multiple threads can call its methods simultaneously.
String reversedString = new StringBuilder(originalString).reverse().toString()
If you prefer not to use API support you can do something like this
static String reverse(String stringIn) {
char[] cArr = stringIn.toCharArray();
for (int i = 0; i < cArr.length/2; ++i){
char c = cArr[i];
cArr[i] = cArr[cArr.length-1-i];
cArr[cArr.length-1-i] = c;
}
return new String(cArr);
}
If only reversing of the string is needed then you can go with the below method.
String name= "India";
String reverseString = new StringBuffer(name).reverse().toString();
System.Out.Println("reversed sstring is "+reverseString );
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 7 years ago.
Improve this question
Write a method called multiConcat that takes a String and an integer as parameters. Return a String made up of the string parameter concatenated with itself count time, where count is the integer. for example, if the parameters values are “ hi” and 4, the return value is “hihihihi” Return the original string if the integer parameter is less than 2.
What i have so Far
import java.util.Scanner;
public class Methods_4_16 {
public static String multiConcat(int Print, String Text){
String Msg;
for(int i = 0; i < Print; i ++ ){
}
return(Msg);
}
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
int Prints;
String Texts;
System.out.print("Enter Text:");
Texts = Input.nextLine();
System.out.print("Enter amount you wanted printed:");
Prints = Input.nextInt();
System.out.print(multiConcat(Prints,Texts));
}
}
Just a few hints:
concating strings can be done this way: appendTo += stuffToConcat
repeating an operation n times can be done with a for-loop of this kind:
for(int i = 0 ; i < n ; i++){
//do the stuff you want to repeat here
}
Should be pretty simple to build the solution from these two parts. And just in case you get a NullPointerException: remember to initialize Msg.
Try this:
public static String multiConcat(int print, String text){
StringBuilder msg = new StringBuilder();
for(int i = 0; i < print; i ++ ) {
msg.append(text);
}
return msg.toString();
}
I have used StringBuilder instead of a String. To know the difference, give this a read: String and StringBuilder.
Also, I would guess you are new to Java programming. Give this link a read. It is about Java naming conventions.
Hope this helps!
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
Am having a txt file. which is having line like
if(true) return true;
I need to get the sub string from preceding spaces that is
" if(true) "
and another sub string as
" retrun true; "
I am reading this line using scanner class and assign to a string. from that string am converting it into toCharArray. I have tried using toCharArray[] but the spaces are ignored. How to get the substring from the preceding spaces using toCharArray
kindly anybody help me to get a solution for this issue
Thanks in advance.
You can use StringReader:
String str = "Some String";
int numberOfChars = str.length();
StringReader sr = new StringReader(str);
char[] chars = new char[numberOfChars];
int i = 0, read = 0;
try {
while ((read = sr.read()) != -1) {
chars[i] = (char)read;
i++;
}
} catch (IOException e) {
// handle the exception
}
This will read all the characters, without skipping any of them.
EDIT:
Now that I understand what you are trying to do, I would recommend you to define a grammar for your instructions. In this way you should be able to identify these 2 separate statements or instructions. I would suggest you to use a compiler building tool for that like e.g. ANTLR.
I have found one solution by using Regular expression for this question. The answer is as follows
public class Test{
public static void main(String[] args){
String str = " TestProgram";
Pattern pattern = Pattern.compile("\\s*[a-zA-Z0-9]+$");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println(0,matcher.end());
}
}
}
Thanks
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
I have a String with some text, f.e.
"Thisisalongwordtest and I want tocutthelongword in pieces"
Now I want to cut the to longs word in 2 pieces with a blank. The word should be cut if it's longer than 10 characters.
The result should be:
"Thisisalon gwordtest and I want tocutthelo ngword in pieces"
How can I achieve this efficiently?
are you looking for this? or I misunderstood the question?
String newString = oldStr.replaceAll("\\w{10}","$0 "))
with your example, the newString is:
Thisisalon gwordtest and I want tocutthelo ngword in pieces
Edit for Pshemo's good comment
to avoid to add space after words with exact 10 chars:
str.replaceAll("\\w{10}(?=\\w)","$0 "));
.replaceAll("(\\w{10})(?=\\w)", "$1 ")
Tested with:
test("abcde fghij klmno pqrst");
test("abcdefghijklmnopqrst");
test("abcdefghijklmnopqrstuv");
test("abcdefghij klmnopqrstuv");
test("abcdefghij klmnopqrst uv");
separate text into words. (by space)
cut long words and replace source word with new words
assemble text again
Note, that this approach will kill multiple-spaces.
(?=\w{10,}\s)(\w{10})
Should be replaced by
"\1 "
you can use replace function.
If it has number or special characters
(?=\S{10,}\s)(\S{10})
can be used.
This is the code i wrote check it once.....
public class TakingInput {
public static void main(String[] args) {
String s="Thisisalongwordtest and I want tocutthelongword in pieces";
StringBuffer sb;
String arr[]=s.split(" ");
for(int i=0;i<arr.length;i++){
if(arr[i].length()>10){
sb=new StringBuffer(arr[i]);
sb.insert(10," ");
arr[i]=new String(sb);
}
}
for(String ss: arr){
System.out.println(ss);//o/p: "Thisisalon gwordtest and I want tocutthelo ngword in pieces"
}
}
}
This code will do exactly what you want.
First create a method that splits a String if its longer than 10 chars:
String splitIfLong(String s){
if(s.length() < 11) return s + " ";
else{
String result = "";
String temp = "";
for(int i = 0; i < s.length(); i++){
temp += s.charAt(i);
if(i == 9)
temp += " ";
result += temp;
temp = "";
}
return result + " ";
}
}
Then use Scanner to read every word in the sentence seperated by a white space" ":
String s = "Thisisalongwordtest and I want tocutthelongword in pieces";
String afterSplit = "";
Scanner in = new Scanner(s);
Then call the splitIfLong() method for every word in the sentence. And add what the method returns to a new String:
while(in.hasNext())
afterSplit += splitIfLong(in.next());
Now you can use the new String as you wish. If you call:
System.out.println(afterSplit);
it will print:
Thisisalon gwordtest and I want tocutthelo ngword in pieces
Hope this helps