printf VS printWriter.printf
Hi,
While writing a program that writes into a file, I saw that there's a difference in the printf method.
While in a "regular" printf or print I use only \n in order to pass a line, in the printWriter.printf I need to use \r\n .
That one I've been able to find out, but some of the other functions of printf seems to work differently and I can't figure out why :(-: .
when I'm writing :
newFile.printf("%-16s",abc);
newFile.print("here\r\n");
newFile.printf("%-16s",abcd);
newFile.print("here\r\n");
newFile.printf("%-16s",abcde);
newFile.print("here\r\n");
newFile.printf("%-16s",abcdef);
newFile.print("here\r\n");
I expect to see the "here"s always in the same place, after 16 spaces.
But instead, I find the "here"s in different places according to the length of the word before it (abc\abcd\abcde\abcdef)
What am I supposed to do to make it right? (When I say "right" i mean all the "here"s in the same place one below the other)
Thanks!
Re: printf VS printWriter.printf
Quote:
Originally Posted by
Mapisto
Hi,
While writing a program that writes into a file, I saw that there's a difference in the printf method.
While in a "regular" printf or print I use only \n in order to pass a line, in the printWriter.printf I need to use \r\n .
Are you sure it's not down to the text editor your are reading these in?
'\n' doesn't show up in Notepad for example.
Quote:
Originally Posted by
Mapisto
That one I've been able to find out, but some of the other functions of printf seems to work differently and I can't figure out why :(-: .
when I'm writing :
newFile.printf("%-16s",abc);
newFile.print("here\r\n");
newFile.printf("%-16s",abcd);
newFile.print("here\r\n");
newFile.printf("%-16s",abcde);
newFile.print("here\r\n");
newFile.printf("%-16s",abcdef);
newFile.print("here\r\n");
I expect to see the "here"s always in the same place, after 16 spaces.
But instead, I find the "here"s in different places according to the length of the word before it (abc\abcd\abcde\abcdef)
What am I supposed to do to make it right? (When I say "right" i mean all the "here"s in the same place one below the other)
Thanks!
Code:
public class Scratch {
public static void main(String[] args) {
System.out.printf("%-16s", "abc");
System.out.println("x");
System.out.printf("%-16s", "abcd");
System.out.println("x");
System.out.printf("%-16s", "abcde");
System.out.println("x");
System.out.printf("%-16s", "abcdef");
System.out.println("x");
}
}
That (base don your code) lines up correctly, so could it be your file reader casuing some problems?
Re: printf VS printWriter.printf
I'm using Windows and opening the file with notepad.
When using the System.out.printf everything works just fine (in the console).
When I'm using printWriter.printf it prints it different in the file.
I figure the problem must be with the notepad, nontheless, it must be readable in notepad : \ . so how can i make it readable in notepad ?
Thanks!
Re: printf VS printWriter.printf
No it's not.
I would blame notepad before anything else.
As I said, it still doesn't recognise '\n'.
Code:
public static void main(String[] args) {
// System.out.println(0x31);
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter("Test.txt"));
pw.printf("%-16s", "abc");
pw.println("x");
pw.printf("%-16s", "abcd");
pw.println("x");
pw.printf("%-16s", "abcde");
pw.println("x");
pw.printf("%-16s", "abcdef");
pw.println("x");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
}
}
That works and displays correctly in notepad.
Re: printf VS printWriter.printf
As does:
Code:
public static void main(String[] args) {
// System.out.println(0x31);
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter("Test.txt"));
pw.printf("%-16s", "abc");
pw.print("x\r\n");
pw.printf("%-16s", "abcd");
pw.print("x\r\n");
pw.printf("%-16s", "abcde");
pw.print("x\r\n");
pw.printf("%-16s", "abcdef");
pw.print("x\r\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
}
}
So I have no idea what you're seeing.
Re: printf VS printWriter.printf
ok,
here's the line i use:
Code:
newFile.printf("%2d %-16s %-5d \r\n",this.id, this.fullName, this.salary);
in the file it appears like this:
4 SissoVeSimhoo__4000
1 SassonSassoni __1000
3 JamesBond007___ 3000
2 Cleopatra_____2000
8 WinnieThePooh__8000
5 Sinderela_____5000
7 Shilgiya_____7000
6 ElivsIsAlive___6000
I can't figure out what I've done wrong :| I need it to appear like this:
4 SissoVeSimhoo____4000
1 SassonSassoni____1000
3 JamesBond007____3000
2 Cleopatra________2000
8 WinnieThePooh___8000
5 Sinderela________5000
7 Shilgiya_________7000
6 ElivsIsAlive______6000
_ = space
What i mean is that every salary will appear one just below the other.
Re: printf VS printWriter.printf
oh hell, it won't show it right here either :| I'll edit it
---Done---
Re: printf VS printWriter.printf
Oh,
and btw, that does the catch and finally do ? :(think):
Re: printf VS printWriter.printf
Please help :(
I need to give this program on sunday and that's the only thing I haven't been able to solve :(
No matter what I'm trying, it won't place the numbers in the right order.
I've noticed another thing while trying to solve it: if i change the flag for the salary (it's called flag, right ? : \ ) from d (which supposed to accept an int) to s (which supposed to accept a string) it didn't show any error and did the same, isn't it supposed to give an error ?
Thanks again,
I know I'm annoying, but I have no choice :(angel):
Re: printf VS printWriter.printf
The various versions of printf() do the same thing. Whether or not that is the the thing you want.
You can also get new lines using %n which is designed to do the right thing for the platform on which it is run. (Although just not using Notepad is a better solution as that program is broken.) If you have platform specific requirements for the newline, use %n.
As regards the behaviour of %s it is also described in the API docs. (which is the first place to check). The %s conversion is listed in the table there in the "general" category ie it accepts any argument type. Although its behaviour with primitives is not mentioned - either there or in the details section -, it's a fair bet that it does a String.valueOf() or similar.
Re: printf VS printWriter.printf
Hi, thanks.
The thing is that it is supposed to be opened by notepad : \ that's what they're asking.
Is there a way to fix this thing in the program? so it would open correctly in notepad?
If not, with which program can I check the file to see if it's just a notepad problem for sure?
Re: printf VS printWriter.printf
Did you try %n?
Code:
import java.awt.Desktop;
import java.io.File;
import java.io.PrintWriter;
public class NotepadEg {
public static void main(String[] args) throws Exception {
File file = new File("c:/out.txt");
PrintWriter pw = new PrintWriter(file);
for(int i = 0; i < 42; i++) {
pw.printf("Line %d here%n", i + 1);
}
pw.close();
Desktop.getDesktop().open(file);
}
}
This opens Notepad with 42 distinct lines for me.
Re: printf VS printWriter.printf
I've just sent the file to a friend.
It appeared fine in his comp!
So it's only my stupid notepad.
Could be frustrating :|
Thanks a lot!!
Re: printf VS printWriter.printf
Quote:
Originally Posted by
Mapisto
Oh,
and btw, that does the catch and finally do ? :(think):
That's exception handling.
Opening a file, or reading from one, can throw an IOException, so I handle it in the catch block (well, I print the exception).
The finally block is always executed whether there was an exception or not, and it's usually used to close off resources. In this case I use it to close the file stream.
Re: printf VS printWriter.printf