• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding print() and println()  RSS feed

 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are two classes testing Java I/O, got an error when compiling:
TestFormattedWriter.java [30:1] reference to print is ambiguous, both method print(int) in java.io.PrintWriter and method print(double) in FormattedWriter match
out.print(numbers[i]);
^
1 error
Errors compiling TestFormattedWriter. Is there anyone could kindly direct me to solve this problem? Thank you very much:-)

the following are the source codes:
import java.io.*;
public class FormattedWriter extends PrintWriter {
public final static int LEFT_JUSTIFIED = 1;
public final static int RIGHT_JUSTIFIED = 2;
private int justification = RIGHT_JUSTIFIED;

private int width = 0; // Field with required for output

// Constructor with a specific field width, autoflush, and justification
public FormattedWriter(Writer output, boolean autoflush, int width, int justification ) {
super(output, autoflush); // Call PrintWriter constructor
if(width>0)
this.width = width; // Store the field width
if(justification == LEFT_JUSTIFIED || justification == RIGHT_JUSTIFIED)
this.justification = justification;
}

// Constructor with a specified field width
public FormattedWriter(Writer output, int width){
this (output, false, width, RIGHT_JUSTIFIED);
}

// Constructor with a specified field width and justification
public FormattedWriter (Writer output, int width, int justification) {
this(output, false, width, justification);
}

// Constructor with a specified field width and autoflush option
public FormattedWriter(Writer output, boolean autoflush, int width) {
this (output, autoflush, width, RIGHT_JUSTIFIED);
}

// Helper method to form string
private String pad(String str) {
if (width == 0) {
return str;
}

int blanks = width - str.length(); // Number of blanks needed
StringBuffer result = new StringBuffer(); // Will hold the output

if (blanks< 0) { // Data does not fit
for (int i = 0; i < width; i ++ )
result.append('x'); // so append X's
return result.toString(); // and return the result
}

if (blanks>0) // If we need some blanks
for (int i = 0; i < blanks; i ++)
result.append(' '); // append them

// Insert the value string at the beginning or the end
result.insert(justification == LEFT_JUSTIFIED ? 0 : result.length(), str);
return result.toString();
}

// Output type long formatted in a given width
public void print(long value) {
super.print(pad(String.valueOf(value))); // Pad to width and output
}

// Output type double formatted in a given width
public void print(double value) {
super.print(pad(String.valueOf(value))); // Pad to width and output
}

// Output type String formatted in a given width
public void print(String str) {
super.print(pad(str)); // Pad to width and output
}

public void println(int value) {
super.println(pad(String.valueOf(value))); // Pad to width and output
}

public void setWidth(int width) {
if(width >= 0)
this.width = width;
}
}

import java.io.*;
public class TestFormattedWriter {
public static void main(String[] args) {
// Some arbitrary data to output
int[] numbers = {
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377
};
double[] values = {
1.0, 1.0, 1.414, 1.732, 2.236, 2.828, 3.606, 4.582, 5.831,
-123456789.23456
};
String[] strings = {
"one", "one", "two", "three", "five", "eight", "thirteen"
};
// Create a formatted writer for a buffered output to the command line
FormattedWriter out = new FormattedWriter(
new BufferedWriter(
new OutputStreamWriter(System.out)), true, 12,
FormattedWriter.RIGHT_JUSTIFIED);

for (int i = 0; i < numbers.length; i++) {
if (i % 6 == 0) { // New line before each line of five values
out.println();
}
out.print(numbers[i]);
}
out.setWidth(10);
for (int i = 0; i < values.length; i++) {
if (i % 5 == 0) { // New line before each line of four values
out.println();
}
out.print(values[i]);
}
for (int i = 0; i < strings.length; i++) {
if (i % 4 == 0) { // New line before each line of three
out.println();
}
//out.print(strings[i]); // Override width
}
}
}
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fuzizhe,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
---------------------
Also, please note that formatted code is often easier to read and understand. You can preserve the display of much of your code formatting by surrounding it with the [code] and [/code] UBB Tags.
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm terribly sorry for all the uncomfortableness I caused here, I simply didn't read the notice when I registered. And Thank you very much for your remind, I think that won't happen again:-). I have changed my display name to fit your rule. and I'd like to repaste the code here(and also try the UBB tab;-):
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Mr. Schreckmann:
Just now I read that "Official policy on registered names" plus the post discussing it word by word and found it very interesting. I am a Chinese, one of my friends' name is really Zizhe, then will you consider it an " obvious fictitious name" and then block the account? Just like German's Zimmer, Zimmermann, Ziel..., Chinese people also have many names start with a "Z" when spelled in English, but they are more odd than German names for English people to pronunce, for example: Zhan, Zhen, Zhang....If those people use their real names here, will you think they are not professional enough because of the strange names? Or it's better for all the Chinese people to wear an English name like Tom, John, Mary....? It is no problem for me, and I have done so. But for some people, that might hurt. Just a thought.
Thank you very much for your kind remind again.

Best Regards,
Ellen Fu
[ September 18, 2002: Message edited by: Ellen Fu ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is covered by section 15.12 of the JLS, which is one of the harder parts of the JLS to understand. Valentin Crettaz wrote an article which may help. Basically, you're caught on step 2 (JLS 15.12.2.2 of the process - the compiler is looking at
out.print(numbers[i]);
trying to decide whether it should refer to
print(int) in PrintWriter
or
print(double) in FormattedWriter
The compiler looks for the "most specific" method to invoke. In general, methods in subclasses are considered more specific than methods in superclasses - which would lead the compiler to pick print(double) in FormattedWriter. However the compiler also looks at the parameters, and considers an int argument more specific than a double. (Because all ints can be converted to doubles, but not all doubles can be converted to ints (without dropping the digits after the decimal point).) This would lead the compiler to choose
print(int) in PrintWriter. Since these two rules contradict, the compiler can't decide which one to use, and asks you. You can resolve this by providing an additional method in FormattedWriter which is more specific than either of the other two:

This tells the compiler which of of the two methods to forward the call to. You should probably have similar methods for char and float as well. And similarly for the println() methods. Seems tedious, I know - but the fact that PrintWriter defined each of these methods separately means you need to override each one separately to get it to work in this case.
[ September 19, 2002: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the naming policy - the problem wasn't that the name was "obviously fictiious"; it was that the name was only one word. We want something with at least one internal space, that at least looks like a first and last name (to our western eyes at least) . We have no problem with names beginning with X or Z (or even Y).
[ September 19, 2002: Message edited by: Jim Yingst ]
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It�s so kind of you to reply my questions, thank you very much!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!