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
Here is my code:
public String display() {
return "\n......................\nFixed Employee:\n" +
"Name: " + super.fullName() +
"\nSalary: " + salary() +
" tk\n......................";
}
But when I'm invoking this method from main class, "\n" newLine not working. just showing one line output. Will you plz help to solve the problem?
Thanks
For saving in files use \r\n. \n as new lines is viable on printstreams but not writing to files.
You may need the system independent line separator as it might differ from one OS to another. Just replace the \n with the value of line separator:
I can be retrieve as you load any system property:
public String display() {
String separator = System.getProperty("line.separator"); // Load the system property using its key.
return "\n......................\nFixed Employee:\n"
+ "Name: "
+ super.fullName() +
"\nSalary: "
+ salary()
+ " tk\n......................"
.replace("\\n", separator); // replace the \n before returning your String
}
Or simply use System#lineSeparator method as #Deepanshu Bedi suggested:
public String display() {
String separator = System.lineSeparator(); // Consider it as a shortcut.
return "\n......................\nFixed Employee:\n"
+ "Name: "
+ super.fullName() +
"\nSalary: "
+ salary()
+ " tk\n......................"
.replace("\\n", separator); // replace the \n before returning your String
}
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
how can I say that is my String match with this pattern: (install OS #name #version) which name and version can be any String without white space.
As you can use following Code:
// assuming parenthesis an sharp are pattern included:
String s = "(install OS #testname #testversion)";
if (s.matches("\\(install\\sOS\\s#\\S+\\s#\\S+\\)")) {
String[] splitted = s.split("\\s");
String name = splitted[2].replace("#", "");
String version = splitted[3].replace(")", "").replace("#", "");
System.out.println("name: " + name);
System.out.println("version: " + version);
}
// assuming parenthesis an sharp are both not pattern included:
s = "install OS testname2 testversion2";
if (s.matches("install\\sOS\\s\\S+\\s\\S+")) {
String[] splitted = s.split("\\s");
String name = splitted[2];
String version = splitted[3];
System.out.println("name: " + name);
System.out.println("version: " + version);
}
(if you need to allow mutliple whitespaces between parts, you have replace in both, split regex and match regex, \s with \s+)
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 3 years ago.
Improve this question
When I am trying to concatenate strings in Java like so:
COsavedform = vtd + "," + stricode + "," + striname+ ","
+ striqty + "," + strirate + "," + striamt;
every variable containing some String Value and returns correct value of COsavedform but when I add something more to the same COsavedform like..
COsavedform = vtd+","+ccode.getText()+","+striqty
+ "," + strirate + "," + striamt+","+mode+",Customer";
mode is also a string variable. Then it returns only COsavedform=","+mode+",Customer", only not the entire things.
Problem part of that full code_--
System.out.println("striname:"+striname);
System.out.println("stricode:"+stricode);
System.out.println("striqty:"+striqty);
System.out.println("strirate:"+strirate);
System.out.println("striamt"+striamt);
System.out.println("ccode"+ccode.getText());
System.out.println("mode"+mode);
String Csavedform = vtd + "," + stricode + "," + striname+ "," + striqty + "," + strirate + "," + striamt;
System.out.println("CSaved::"+Csavedform);
//System.out.println("COSaved::"+COsavedform);
String Isavedform = vtd+","+ccode.getText()+","+striqty+","+strirate+","+striamt+","+mode+",Customer";
System.out.println("Item savedforitem::"+Isavedform);
```
Output Generated::
vdt:31/12/2019
striname:Kazu
stricode:kazu
striqty:1.0
strirate:1000.0
striamt1000.0
ccode0001
modePending
CSaved::31/12/2019,kazu,Kazu,1.0,1000.0,1000.0
,Pending,Customer
Output Expected::
vdt:31/12/2019
striname:Kazu
stricode:kazu
striqty:1.0
strirate:1000.0
striamt1000.0
ccode0001
modePending
CSaved::31/12/2019,kazu,Kazu,1.0,1000.0,1000.0
Item savedforitem::31/12/2019,0001,1.0,1000.0,1000.0,Pending,Customer
You can use +=
COsavedform += vtd+","+ccode.getText()+","+striqty + "," + strirate + "," + striamt+","+mode+",Customer";
But better to use StringBuilder
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("str1").append("str2")...
COsavedform = stringBuilder.toString();
because its better for memory
Use concat()
Java string concat() method concatenates multiple strings. This method appends the specified string at the end of the given string and returns the combined string. We can use concat() method to join more than one strings
Edited Answer after comment
There are 4 Ways to concatenate String in Java
Concatenation operator (+)
StringBuffer class
StringBuilder class
String.concat() function
Use + operator to concatenate
String stringA= "Stack";
String stringB = "overflow";
String result = stringA + "," + stringB;
System.out.println(result);
By using StringBuilder
StringBuilder result = new StringBuilder();
result.append(stringA).append(",").append(stringB);
System.out.println(result.toString());
By using StringBuffer
StringBuffer result = new StringBuffer();
sBuffer.append(stringA).append(",").append(stringB);
System.out.println(result.toString());
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 3 years ago.
Improve this question
I would like to transfer the following code from Python to Java, but I get an error, while doing it:
import re
payload = re.search(
r'decrypt\.setPrivateKey\("(?P<privateKey>[^"]+)".*?'
r'decrypt\.decrypt\("(?P<cryptText>[^"]+)".*?'
r'document\.cookie="ipp_uid=(?P<ipp_uid>[^"]+)".*?'
r'document\.cookie="ipp_uid1=(?P<ipp_uid1>[^"]+)".*?'
r'document\.cookie="ipp_uid2=(?P<ipp_uid2>[^"]+)".*?'
r'url\s\+=\s"(?P<makeURL>.*?)"\;.*?'
r'salt="(?P<salt>[^"]+)"',
ret.content.decode('utf-8'),
re.MULTILINE | re.DOTALL
)
I have already tried the following code:
String patternString = "decrypt\\.setPrivateKey\\(\"(?P<privateKey>[^\"]+)\".*?\n"
+ " decrypt\\.decrypt\\(\"(?P<cryptText>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid=(?P<ipp_uid>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid1=(?P<ipp_uid1>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid2=(?P<ipp_uid2>[^\"]+)\".*?\n"
+ " url\\s\\+=\\s\"(?P<makeURL>.*?)\"\\;.*?\n"
+ " salt=\"(?P<salt>[^\"]+)\"";
Pattern payload = Pattern.compile(patternString);
String content = new String(html.getBytes(), "UTF-8");
Matcher m = payload.matcher(html);
if(m.find()){
System.out.println("Found: " + m.group(0));
}else{
System.out.println("not found");
}
... but I am getting this error:
java.util.regex.PatternSyntaxException: Unknown inline modifier near index 27
decrypt\.setPrivateKey\("(?P<privateKey>[^"]+)".*?
decrypt\.decrypt\("(?P<cryptText>[^"]+)".*?
document\.cookie="ipp_uid=(?P<ipp_uid>[^"]+)".*?
document\.cookie="ipp_uid1=(?P<ipp_uid1>[^"]+)".*?
document\.cookie="ipp_uid2=(?P<ipp_uid2>[^"]+)".*?
url\s\+=\s"(?P<makeURL>.*?)"\;.*?
salt="(?P<salt>[^"]+)"
^
at java.util.regex.Pattern.error(Pattern.java:1957)
at java.util.regex.Pattern.group0(Pattern.java:2896)
at java.util.regex.Pattern.sequence(Pattern.java:2053)
at java.util.regex.Pattern.expr(Pattern.java:1998)
at java.util.regex.Pattern.compile(Pattern.java:1698)
at java.util.regex.Pattern.<init>(Pattern.java:1351)
at java.util.regex.Pattern.compile(Pattern.java:1028)
at fabian.site.MyModule.test(MyModule.java:76)
at fabian.site.MyModule.run(MyModule.java:61)
at fabian.thread.ThreadPool$PoolThread.run(ThreadPool.java:50)
Thank you for your help guys!!
Two things stand out to me:
Named capturing groups in Java are structured like (?<name>X), not (?P<name>X), so you should remove the Ps
The names cannot contain "_", so you should replace ipp_uid with something like ippUid (only letters and numbers)
String patternString = "decrypt\\.setPrivateKey\\(\"(?<privateKey>[^\"]+)\".*?\n"
+ " decrypt\\.decrypt\\(\"(?<cryptText>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid=(?<ippuid>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid1=(?<ippuid1>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid2=(?<ippuid2>[^\"]+)\".*?\n"
+ " url\\s\\+=\\s\"(?<makeURL>.*?)\"\\;.*?\n"
+ " salt=\"(?<salt>[^\"]+)\"";
I don't have any sample data, so it's hard to tell whether it works this way, but it does compile without errors.
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
Given a String,
Using regex in java or java code i have to find out Electors name from the given String :
"K ~\n" +
"m W swim\n" +
"sfiaruqsr\n" +
"wvnn 021m: r’ i\" W' _ ~\n" +
"_ 4'“; x ‘ D\n" +
"W ma ¢ “ii-a? “Rm qwfl\n" +
"Electors name ; Moleslwar Moreshva;\n" +
"Tuwal\n" +
"mam-a #02,qu _\n" +
"F\n" +
"Fm“ Name : Momma! Tuppal I\n" +
"‘ e\n" +
"Pam sq» : w! MALE '\n" +
"“WW/Dale m mm ; XX/xxnsae _‘"
Like for the given String the output should be : Moleslwar Moreshva Tuwal
Can't hardcode anything as the String response changes everytime.
Please help:)
There is no name end delimiter hence will have to assume the full name needs to be captured from the line where Electors name is present and the next line only. And once it is captured, replace unwanted character with a space.
Here is the java code that parses the name for you,
public static void main(String[] args) {
String s = "K ~\n" +
"m W swim\n" +
"sfiaruqsr\n" +
"wvnn 021m: r’ i\" W' _ ~\n" +
"_ 4'“; x ‘ D\n" +
"W ma ¢ “ii-a? “Rm qwfl\n" +
"Electors name ; Moleslwar Moreshva;\n" +
"Tuwal\n" +
"mam-a #02,qu _\n" +
"F\n" +
"Fm“ Name : Momma! Tuppal I\n" +
"‘ e\n" +
"Pam sq» : w! MALE '\n" +
"“WW/Dale m mm ; XX/xxnsae _‘";
Pattern p = Pattern.compile("Electors name\\s*;\\s*([\\w; ]+\\n\\w+)");
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println(m.group(1).replaceAll(";\\n", " "));
} else {
System.out.println("Didn't match");
}
}
This prints following output,
Moleslwar Moreshva Tuwal
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
What are some better ways to print the following section? I think it will look better in a table format of some type with a heading. I want to adjust them to fixed lenghts, I think. Here is the code snippet I want to format to look better. The print elements are elements in a sql database.
System.out.println("\nAll records in your table:");
System.out.println("ID# Name GPA Status Mentor Level Thesis Advisor Company"); //table heading...
while (rs.next()) {
String output = " ";
output += rs.getString("studentID") + " "
+ rs.getString("firstName") + " "
+ rs.getString("lastName") + " "
+ rs.getString("gpa") + " "
+ rs.getString("status") + " "
+ rs.getString("mentor") + " "
+ rs.getString("level") + " "
+ rs.getString("thesisTitle") + " "
+ rs.getString("thesisAdvisor") + " "
+ rs.getString("company") + "\n";
System.out.printf("%s", output);
}
|You can|
|Do something|
|like this|
|studentname|
|firstname|
|middlename|
|.......|
"I want to adjust them to fixed lengths":
is that why your concatenating whitespaces in different lengths ?
You can try to concatenate "\t\t" instead.
In case it's HTML, using HTML Table will take care of it for you.