I try to create one big String in appropriate format as i want and print it using PrinterJob class. Here is the code of String:
String bigtext = "The name\n" + "some another text";
Graphics2D's_object.drawString(bigtext, 50, 50);
But it prints as "The name some another text" in one line, "\n" does not work, while i want to print "some another text" in another line.
P.S. I try to print bigtext in printer.
SOLVED: Here is the solution: Problems with newline in Graphics2D.drawString. (after long trouble :))
The linefeed character \n is not the line separator in certain operating systems use \r\n. Additionally i would recommend use of StringBuilder rather then using +
Edit : You can use System.getProperty("line.separator"); as well.
A similar question was asked yesterday and this is what helped:
The problem that you are encountering is because of the "line separator" you are hard coding. It's best to get the System's line separator with:
System.getProperty("line.separator");
So that your code would look like this:
String lineseparator=System.getProperty("line.separator");
// I'd suggest putting this as a class variable, so that it only gets called once
// rather than everytime you call the addLine() method
String bigtext = "The name" + lineseparator + "some another text";
//If you wanted an empty line in between them, then add the lineseparator twice
String bigtext = "The name" + lineseparator + lineseparator + "some another text";
It seems most of the guys don't understand the question. I tried the PrinterJob and it doesn't work for me neither.
I found a solution but not verifyied:
How to print strings with line breaks in java
Related
first time I am asking for help in here so If my thread format is wrong I am so sorry.My problem is I can't make new line in Java JDA(Java Discord API).
When I use:
eb.setDescription("For \n Example");
It works just fine but I am trying to do this.
First I am getting string from config file:
String Message = plugin.getConfig().getString("Discord." + "PrivateMessage")
It gets the String fine but when I use eb.setDescription(Message);
Message coming without lines. There are new line brackets "\n" in message but they are not making new lines.Message coming like:
"For \n Example" without new lines.
I'm assuming your actual string is in a file and you just read it out. When you put \n in a file that doesn't automatically convert to a newline when you read it. You can do string = string.replace("\\n", "\n") to convert it.
I am a novice in Java so please pardon my inexperience. I have a column (source) like below which has empty strings and I am trying to replace it with Non-Disclosed.
Source
Website
Drive-by
Realtor
Social Media
Billboard
Word of Mouth
Visitor
I tried:
String replacedString = Source.replace("", "Non-Disclosed");
After running the above snippet, everything gets replaced by Non-Disclosed:
Non-Disclosed
Non-Disclosed
Non-Disclosed
............
How can I tackle this issue? Any assistance would be appreciated.
I think you simply have to do : Source.replace("\n\n", "\nNon-Disclosed\n")
I am assuming that your entire column is stored in one string.
In that case you can use ^$ regex to represent empty line (with MULTILINE flag (?m) which will allow ^ and $ to represent start and end of lines).
This approach
will work for many line separators \r \n \r\n
will not consume those line separators so we don't need to add them back in replacement part.
To use regex while replacing we can use replaceAll(regex, replacement) method
DEMO:
String text = "Source\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
"Website\r\n" +
"Drive-by\r\n" +
"Realtor\r\n" +
"Social Media\r\n" +
"\r\n" +
"Billboard\r\n" +
"\r\n" +
"Word of Mouth\r\n" +
"\r\n" +
"Visitor";
text = text.replaceAll("(?m)^$", "Non-Disclosed");
System.out.println(text);
Output:
Source
Non-Disclosed
Non-Disclosed
Non-Disclosed
Website
Drive-by
Realtor
Social Media
Non-Disclosed
Billboard
Non-Disclosed
Word of Mouth
Non-Disclosed
Visitor
You can use
String replacedString = Source.trim().isEmpty() ? "Non-Disclosed" : Source;
to replace only the "blank" String.
I have the following text file. I want to remove the lines and spaces so that the text file has a clear delimter to process. I cannot think of any way to remove the gaps between lines, is there a way?
Student+James Smith+Status: Current Student+Student+James Fits+Status: Not a current Student
Textfile
Student
James Smith
Status: Current Student
Student
James Fits
Status: Not a current Student
I know that this
a.replaceAll("\\s+","");
removes whitespaces.
You could remove end of line characters in a similar fashion
a.replaceAll("\n","");
Where 'a' is a String.
use a regex take the whole text in to a string and
string txt = "whole String";
String formatted = txt.replaceAll("[^A-Za-z0-9]", "-");
this will result in changing + sign and " " to replace with "-" sign. so now you have a specific deleimeter.
Something like find \s*\r?\n\s* replace +
Trims whitespace and adds delimiter '+'
Result:
Student+James Smith+Status: Current Student+Student+James Fits+Status: Not a current Student
Try using this one.
\n+\s*
just use it like this :
yourStrVar.replaceAll("\n+ *", "+")
I am reading a CSV file of 4GB in java what I have to do is extract 100000 record from file and make a separate file but problem is when I am reading a line
line = br.readLine() and String[] record = line.split(cvsSplitBy); it adds one extra "" in every string like when I open a record array it look like
""abc"",""bcd"",""cef"",""dgh"",""elk"" it should be like "abc","bcd","cef","dgh","elk"
Kindly let me know why its adding extra commas against every string
Post your code so we can investigate. In the mean time you can remove those extra "" or do something like:
line.split("\"" + cvsSplitBy + "\"")
Post your code and I'll edit this reply.
I am developing a small java application. At some point i am writing some data in a plain text file. Using the following code:
Writer Candidateoutput = null;
File Candidatefile = new File("Candidates.txt"),
Candidateoutput = new BufferedWriter(new FileWriter(Candidatefile));
Candidateoutput.write("\n Write this text on next line");
Candidateoutput.write("\t This is indented text");
Candidateoutput.close();
Now every thing goes fine, the file is created with the expected text. The only problem is that the text was not formatted all the text was on single line. But if I copy and paste the text in MS Word then the text is formatted automatically.
Is there any way to preserver text formatting in Plain text file as well?
Note: By text formatting I am referring to \n and \t only
Use System.getProperty("line.separator") for new lines - this is the platform-independent way of getting the new-line separator. (on windows it is \r\n, on linux it's \n)
Also, if this is going to be run on non-windows machines, avoid using \t - use X (four) spaces instead.
You can use line.separator system property to solve your issue.
E.g.
String separator = System.getProperty("line.separator");
Writer Candidateoutput = null;
File Candidatefile = new File("Candidates.txt"),
Candidateoutput = new BufferedWriter(new FileWriter(Candidatefile));
Candidateoutput.write(separator + " Write this text on next line");
Candidateoutput.write("\t This is indented text");
Candidateoutput.close();
line.separator system property is a platform independent way of getting a newline from your environment.
A PrintWriter does this platform independent - use the println() methods.
You would have to use the Java utility Formatter which can be found here: java.util.Formatter
Then all you would have to do is create an object of Formatter type such as this:
private Formatter output;
In this case, output will be the output file you are writing to.
Then you have to pass the file name to the output object like this:
output = new Formatter("name.of.your.file.txt")
Once that's done, you can either hard-code the file contents to your output file using the output.format command which is similar to the System.out.println or printf commands.
Or use the Scanner utility to input the data into memory then use output.format to output this data to the output object or file.
This is an example on how to write a record to output:
output.format( "%d %s %s %2f\n" , field1.decimal, field2.string, field3.string, field4.double)
There is a little bit more to it than this, but this sure beats parsing data, or using a bunch of complicated third party plugins.
To read this file you would redirect the Scanner utility to read a file instead of the console:
input = new Scanner(new File( "name.of.your.file.txt")
Window's Notepad needs \r\n to display a new-line correctly. Only \n is ignored by Notepad.
Well Windows expects a newline and a carriage return char to indicate a new line. So you'd want to do \r\n to make it work.