• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

import java.io.*

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-------------------------------------
import java.io.*;//line 1
//import java.io.PrintWriter; line 2
//import java.io.OutputStreamWriter; line 3

public class foo {
public static void main(String[] args) throws Exception
{
PrintWriter out = new PrintWriter (new java.io.OutputStreamWriter (System.out), true);
System.out.println("Hello");
}
}
-------------------------------------
The above is foo.java file.
If line 1 is uncommented, it compiles and runs well.
If line 2 is uncommented, it compiles and runs well.
BUT if line 3 is uncommented, it does not compile and the error said symbol could not be resolved...
Can anybody explain this? Thanks a bunch!
Moya
[ August 30, 2002: Message edited by: Moya Green ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for me it compiled and run ok
 
Moya Green
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anybody else tried?
I tried on both jdk 1.3 and 1.4. No luck.
Hi Jose, thanks for your prompt reply. Did you copy and paste my code (use
import java.io.OutputStreamWriter; //line 3
in steadof
import java.io.*;//line 1
or
import java.io.PrintWriter; //line 2
)? Sorry to be a pain. Just try to figure out what's wrong with it? Thank you!
Moya
[ August 30, 2002: Message edited by: Moya Green ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I swore it. I did it.
I uncommented the import and all it's right.
Can you write the error message?
Can you check the spelling?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Worked fine for me too. Line 1 being uncommented eliminates the need for line 2 and 3.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For me using JDK 1.4, the above code compiles and executes without any modifications.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I retracted my last post after trying it again.
I can confirm that with only line 3 uncommented that I get an unresolved symbol class PrintWriter when using plain vanilla javac 1.4.
Same using JEdit and BlueJ environments.
Using only line 2 the import of java.io.PrintWriter causes the OutputStreamWriter class to be implicitly imported.
Is that what you're at?
-Barry
[ August 31, 2002: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't know there was such a thing as "implicit import" in Java. Where can I read more about it?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ron, I didn't mean that as a statement of fact. I guess I should have said "seems as if it had been implicitly imported".
The fact is that just using import java.io.PrintWriter enables a successful compile.
So do you have any idea where the apparent "implicit import (my words)" of OutputStreamWriter comes from?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to import java.io.OutputStreamWriter at all because you name it explicitly in the one place where you use it.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Using only line 2 the import of java.io.PrintWriter causes the OutputStreamWriter class to be implicitly imported.


But I can see a java.io before OutputStreamWriter. You don't need to the third import. :roll:
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah well, er, I mean, um, you could be right there... I'll have to investigate that a bit... :roll:
Hey Moya, fine mess you got us into!
I just took out the java.io in the call to the PrintWriter constructor, and guess what? it don't compile anymore.
Well win some, lose a lot...
[ August 31, 2002: Message edited by: Barry Gaunt ]
 
Moya Green
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everyone, thank you for your help. I was out of town last weekend and could not join your discussion. I truly appologize for it.
I am sorry I did not say clearly about my question. Barry, you are right! If ONLY line 3 is uncommented, compile errors occurred (like unresolved symbol class PrintWriter...). I don't understand why.
-------------------------------
import java.io.OutputStreamWriter; //line 3
public class Foo {
public static void main(String[] args) throws Exception
{PrintWriter out = new PrintWriter (new java.io.OutputStreamWriter (System.out), true);System.out.println("Hello");
}
}
--------------------------------
Anyway, many thanks again!
Moya
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are not importing the package that PrintWriter is in.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome back, Moya
To summarize, and to put this one to rest.
Notice that OutputStreamWriter is used like this java.io.OutputStreamWriter in the PrintWriter constructor. This means that we do not have to import it. I was blind to that until the other guys practically !!!SHOUTED!!! it at me.
Bearing that in mind, if you uncomment just:
Line 1. then PrintWriter gets imported because you import every class in package java.io by virtue of using the "*".
Line 2. then PrintWriter is imported explicitly.
Line 3. Here we import OutputStreamWriter explicitly, but we do not need to do it. PrintWriter, as Thomas says, has not been imported. Therefore the compiler freaks out.

Cheers,
-Barry
[ September 03, 2002: Message edited by: Barry Gaunt ]
 
Moya Green
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry, Thomas,
I appreciate your help very much. I get it.
-Moya
 
Dinner will be steamed monkey heads with a side of tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic