aspose file tools*
The moose likes Linux / UNIX and the fly likes probably a stupid question..but Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "probably a stupid question..but" Watch "probably a stupid question..but" New topic
Author

probably a stupid question..but

Anne Poe
Greenhorn

Joined: Sep 10, 2004
Posts: 13
alright,
I just began java and have assignments to finish up today...i was working on this stuff last night when my lil program wouldn't compile anymore.
It started out with a 'Welcome to java programming!' message which worked just fined. problem occurred after I added an integer and wanted it to be displayed:

{
//main method begins execution of Java app.
public static void main( String args[] )
{
System.out.println( "Welcome to Java Programming!" );
System.out.println("This should work!");
int x;
x = 10;
System.out.printf("%d", x);
} //end method main
} //end class Welcome1


when i say javac Welcome1.java, this is the error i receive:
> javac Welcome1.java
Welcome1.java:13: cannot resolve symbol
symbol : method printf (java.lang.String,int)
location: class java.io.PrintStream
System.out.printf("%d", x);
^
1 error

I'm sure the problem is probably embarassingly clear, just not to me yet. Never done this, so I'd appreciate if someone can point out why when i try to print x with the placeholder, it won't print (that's where the problem occurs, tried printf, println, and print, no luck).
Thanks in advance!
[ September 10, 2004: Message edited by: gimme pizza ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hmm "gimme pizza" - that name is not going to last long round here.

What you need to do when you use a method is check the documentation for that method. Lets examine the problem line:

If I look in the JavaDocs for System, I find it has a public field called "out" which is a PrintStream. So I check the PrintStream docs - and hey presto theres all the methods avaliable to a PrintStream. A quick check and I dicover that there is no printf(String, int) method. There are lots of other print methods - none of which take two parameters as you would like, but there is one which takes a String. If you read up on string concatenation - you can use that.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
You need to concatenate the int to the string. It is surprisingly simple to do
[ September 10, 2004: Message edited by: Nigel Browne ]
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Pizza. You've created a monster here...some ugly hybrid of C and Java. printf has no place in Java, please, put it back in its proper home. It looks like you need a good intro Java book. Or, you can always go to Sun's site, which has a number of great tutorials that will get you started.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
gimme pizza,

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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
There is absolutely nothing wrong with the initial poster's example program, if you're using Java 2 v5.

If you take a look at the Java 2 v5 PrintWriter class documentation, you'll discover that there is indeed a printf method available that takes a String and a variable number of objects. It's true that an int is not an object, and that's where autoboxing comes to play - the int is automatically wrapped up in an Integer object.

So, original poster, I'd guess that you're getting the posted error because you're not using a Java 2 v5 compiler. What happens when you run the following command?

javac -version
Anne Poe
Greenhorn

Joined: Sep 10, 2004
Posts: 13
Hey everyone,
thanks for the replies. Dirk, you hit close to what my teacher said. i think the version of the compiler has something to do with this.
(also keep in mind i'm using unix here). My teacher said something to the effect of "Your path variable needs to have /usr/jdk1.5.0/bin/ before any other sources for javc, java, or jar."

Dirk--I will type in that command and let you know what version I'm using. I guess I should clarify that I'm not good with Unix and am in the process of learning. If you could assist me in doing what my teacher has told me to, I'd be grateful (i.e.the steps I need to take to change the path?, the unix commands that'll accomplish this task!)
ps. i'd ask my teacher but he may never email me back before Monday.

To all the others--I didn't mean to annoy. I know how it can be with a noob. You all seem to have a good understanding of java so I posted here. Hopefully I'll be using this forum throughout the semester and onwards.

Lastly, I will change my name as soon as I have a couple of free min's. Right now, assignments need to be completed.

(Lastly was a lie-- in java, can you not say: int x = 1;? or is that strictly C++?)

Thanks all.

(Added: Dirk--i typed in java -version and here's what I got:
> java -version
java version "1.4.2_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)

How may I update it to 1.5.0? Thank you for your time.)
[ September 10, 2004: Message edited by: Anne S. ]

[ September 10, 2004: Message edited by: Anne S. ]

Ernest-done.

sorry to edit so many times, but I don't know how many posts a new member can make. Just wanted to add that i typed in :whereis java and what i get is: java: /usr/bin/java
I'm doing google searches and nothing's helping so far. The help will be appreciated. I have to use the new version to get my homework done.
[ September 10, 2004: Message edited by: Anne Poe ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Originally posted by gimme pizza:

(Added: Dirk--i typed in javac -version, and got:
javac: invalid flag: -version
Then it lists all the other flags I can use... I will only post 'em if you want me to. Any other suggestions? Thanks for your time.)



Hi Anne,

The javac command doesn't have a -version switch; I'm sure that Dirk meant to type just "java -version".

Thanks for changing your display name, but note that the rule is that you have to have a complete real-sounding last name (preferably your real name), not an initial; one more time, please?


[Jess in Action][AskingGoodQuestions]
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Hi Anne,
If you do a ls-la from the usr directory do you have jdk1.5.0 installed as well as jdk1.4.2 ? If so by typing usr/jdk1.5.0/bin/javac Welcome1.java will compile your class using the 1.5 version of java

p.s There is no restriction on the amount of times you may post so to make it easier for everyone to follow the thread just add your replys to the end of the thread.
[ September 10, 2004: Message edited by: Nigel Browne ]
Anne Poe
Greenhorn

Joined: Sep 10, 2004
Posts: 13
Nigel--thanks a lot for your reply. However, I had to use /usr/jdk1.5.0/bin/javac (or java to execute), but thank you. Sorry for listing so many questions, but is there a way that I can set my javac, java, jar versins to jdk1.5.0 permanently?
It's kind of a pain to type it all out, but I will do what I have to.
Thanks a lot guys, though I'm sure I'll be creating another post over the weekend
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Edit the PATH variable in /etc/profile so that it refers to /usr/jdk1.5.0/bin instead of /usr/jdk1.4.2/bin
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Ernest Friedman-Hill:
The javac command doesn't have a -version switch; I'm sure that Dirk meant to type just "java -version".


Ernest, wanna bet?

With Java 2 v5, javac does indeed have a -version switch.
[ September 10, 2004: Message edited by: Dirk Schreckmann ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Originally posted by Dirk Schreckmann:

With Java 2 v5, javac does indeed have a -version switch.


SO to tell what version of JDK you have, you type javac -version. If you get an error message, it's pre-v5. If you get useful info, it's v5.

Or, you could just type java -version ...

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
(Continuing this silly tangent... It is Friday afternoon, after all, and I'd hate to be encumbered with doing any real work...)

But of course. After all, we all know the best way to traverse an array is to just keeping referencing subsequent components until an ArrayIndexOutOfBoundsException is thrown, at which point you stop.

Just kidding.

Since the initial problem related to compiling, I suggested exploring which compiler was in use. I've seen more than one system where the PATH was configured such that different Java installations were invoked by java and javac. In this fancy set up, the java command would invoke a JRE, the location of which was described at the beginning of the PATH value, while the javac command would invoke a J2SDK, the location of which was described at a later point in the PATH value.

So, just running java -version in this setup wouldn't tell the story of which Java compiler was actually being used when running javac.

Anne, did Nigel's suggestion do the trick for you? Are you using a Mac, or some other Linux flavored installation?

I ask because if it's a Mac, then I'd like to update our FAQ on setting the CLASSPATH to indicate that the Linux instructions work on a Mac.
[ September 10, 2004: Message edited by: Dirk Schreckmann ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Originally posted by Dirk Schreckmann:
In this fancy set up, the java command would invoke a JRE, the location of which was described at the beginning of the PATH value, while the javac command would invoke a J2SDK, the location of which was described at a later point in the PATH value.


Good point!

You can actually say "javac -J-version", which produces meaningful info for pre-v5 JDKs -- I don't know about Tiger, ut it probably still works.
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Originally posted by Jeffrey Hunter:
Pizza. You've created a monster here...some ugly hybrid of C and Java. printf has no place in Java, please, put it back in its proper home. It looks like you need a good intro Java book. Or, you can always go to Sun's site, which has a number of great tutorials that will get you started.


Here's a thought for me...read the Tiger docs before offering such ludicrous advice!.

My apologies, up until this point I hadn't paid much attention to v1.5, but I see it has some pretty cool output formatting (hurrah, now I'm going to baffle my co-workers with the C-like hybrid System.out.printf()). Very nice feature.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
That's a good one to know, and it does work with Java 2 v5.
Anne Poe
Greenhorn

Joined: Sep 10, 2004
Posts: 13
Jeffrey--you appologized, so it's all good. heh, you people love your java

Ok, I'm sorry to be such a shallow, inexperienced programmer...plus I'm no good at Unix. Don't know except for a couple of commands. Sometimes I use the SSH Secure Shell to connect to strauss or telnet from home to work on strauss. ah, and we're using Unix. That's all I know, sadly.

I'd like to change all this and learn a little more about Unix (especially these 'Path variables' and using 'tcsh', and letting go of Pico to learn vi or emacs). I'm clueless about these things and being here at Univ. for at least two more years, it'd do me good to learn about these sings.

I know Nigel gave me some advice, but that went straight over my head. Please, if someone wants to tell me what commands to use to find out what changes I make to what file/directory, I'll be happy to do that.

This stuff seems confusing, but hopefully I can learn a few things here. Seems pretty friendly around here.

Thanks all for your time, always
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
You should be able to print with: the println method is "overloaded" so that it can take an int, char, float, String or several other parameters. You don't need that %d stuff.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Anne, what is your operating system?
Anne Poe
Greenhorn

Joined: Sep 10, 2004
Posts: 13
Dirk--all I know is I'm using Unix for my univ. work.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Try opening a terminal window, type "which java" and see what the results are.

Also, in your home directory, look around for a file named .profile. You can use it to set your path/classpath.

Also, what does your prompt (in the terminal window) look like? A percent sign? An angle brace (> ?
[ September 11, 2004: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Anne Poe
Greenhorn

Joined: Sep 10, 2004
Posts: 13
> which java
/usr/bin/java

oh, and the prompt looks like this:
>

the .profile, i'll have to look for in the morning, as i'm tackling my 2nd java homework

Added: Sorry, I should've been more observant, as I just saw 'SunOS 5.9' before I log in. Does that answer your operating system, Dirk?
[ September 11, 2004: Message edited by: Anne Poe ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Yes. It seems to me that the solution you're searching for is specific to your operating system, which is a flavor of UNIX.

So, I'm moving this to the Linux / UNIX forum...
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Welcome Anne Poe,

I'm using linux, a different unix-flavor, and bash, not tclsh, another shell.
So my suggest might not work for you.
If you don't find the correct place to set your path, you could write a small script mypath:

and call it by
- but as mentioned, it might not work on tclsh/ SunOs 5.9


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: probably a stupid question..but
 
Similar Threads
Problems compiling program-can anyone help??
Class path Problems
how to insert symbols
help clarify signed, long, int
stars