how do I use String's format? not working on XP  
Author Message
Jim Michaels





PostPosted: 2007-6-2 3:51:00 Top

java-programmer, how do I use String's format? not working on XP import java.lang.String;
...
public void paint(Graphics g) {
Graphics2D comp2d = (Graphics2D)g;
Dimension appletSize = this.getSize();
appletsize_x = appletSize.width;
appletsize_y = appletSize.height;
String sx=String.format("%d", appletsize_x);
String sy=String.format("%d", appletsize_y);
g.drawString(sx, 200,30);
g.drawString(sy, 200,60);
g.setColor(Color.red);
g.fillOval(x_pos - radius, y_pos - radius, 2*radius, 2*radius);
}
...
}

ballapplet.java:101: cannot resolve symbol
symbol : method format (java.lang.String,int)
location: class java.lang.String
String sx=String.format("%d", appletsize_x);
^
ballapplet.java:102: cannot resolve symbol
symbol : method format (java.lang.String,int)
location: class java.lang.String
String sy=String.format("%d", appletsize_y);
^
2 errors
--

------------------------------------
Jim Michaels
for email, edit the address

RAM Disk is *not* an installation method.
 
Donkey Hot





PostPosted: 2007-6-2 4:27:00 Top

java-programmer >> how do I use String's format? not working on XP Jim Michaels <email***@***.com> wrote in news:k-
email***@***.com:

> import java.lang.String;
> ...

It's not dependent on XP, but Java runtime version.

1.4.2 String does not have format(), while Java 1.5 (5) and later does.

--
Alcoholics Anonymous is when you get to drink under someone else's name.

 
Jim Michaels





PostPosted: 2007-6-3 5:15:00 Top

java-programmer >> how do I use String's format? not working on XP Donkey Hot wrote:
> Jim Michaels <email***@***.com> wrote in news:k-
> email***@***.com:
>
>> import java.lang.String;
>> ...
>
> It's not dependent on XP, but Java runtime version.
>
> 1.4.2 String does not have format(), while Java 1.5 (5) and later does.
>

huh?
I justr installed the latest Java SDK.

--

------------------------------------
Jim Michaels
for email, edit the address

RAM Disk is *not* an installation method.
 
 
Jim Michaels





PostPosted: 2007-6-3 5:21:00 Top

java-programmer >> how do I use String's format? not working on XP Jim Michaels wrote:
> Donkey Hot wrote:
>> Jim Michaels <email***@***.com> wrote in news:k-
>> email***@***.com:
>>
>>> import java.lang.String;
>>> ...
>>
>> It's not dependent on XP, but Java runtime version.
>>
>> 1.4.2 String does not have format(), while Java 1.5 (5) and later does.
>>
>
> huh?
> I justr installed the latest Java SDK.
>

hmm. correction. I have 2 sdk's installed, 1.4.2 and the latest, and
the one that's in the path is 1.4.2...
so... how do I convert an int to a String in 1.4.2?

--

------------------------------------
Jim Michaels
for email, edit the address

RAM Disk is *not* an installation method.
 
 
Dorian





PostPosted: 2007-6-4 3:08:00 Top

java-programmer >> how do I use String's format? not working on XP On 2007-06-02 16:20:37 -0500, Jim Michaels
<email***@***.com> said:

> Jim Michaels wrote:
>
> hmm. correction. I have 2 sdk's installed, 1.4.2 and the latest, and
> the one that's in the path is 1.4.2...
> so... how do I convert an int to a String in 1.4.2?

How about:

int i = 22;
String s = i + "";

Bob



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
 
Lew





PostPosted: 2007-6-4 7:44:00 Top

java-programmer >> how do I use String's format? not working on XP Jim Michaels wrote:
>> hmm. correction. I have 2 sdk's installed, 1.4.2 and the latest, and
>> the one that's in the path is 1.4.2...
>> so... how do I convert an int to a String in 1.4.2?

Take note:
<http://java.sun.com/j2se/1.4.2/index.jsp>
> J2SE 1.4.2 has begun the Sun End of Life (EOL) process.

At over five years old, 1.4.2 is getting long in the tooth. Java 5 is almost
three, and isn't even the current version.

Dorian wrote:
> How about:
>
> int i = 22;
> String s = i + "";

Which in turn acts as if it invokes the
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#toString()>
for an Integer version of the int, which converts that value
> exactly as if the integer value were given as an argument to the toString(int) method.
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#toString(int)>
This is also how the
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#valueOf(int)>
method works. Any particular reason not to use one of those directly?

<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.1.1>
> Any type may be converted to type String by string conversion.
> A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression:
> ... This reference value is then converted to type String by string conversion.
> ... the conversion is performed as if by an invocation of the toString method of
the referenced object with no arguments; but if the result of invoking the
toString method is null, then the string "null" is used instead.

--
Lew
 
 
Jim Michaels





PostPosted: 2007-6-6 5:46:00 Top

java-programmer >> how do I use String's format? not working on XP Lew wrote:
> Jim Michaels wrote:
>>> hmm. correction. I have 2 sdk's installed, 1.4.2 and the latest, and
>>> the one that's in the path is 1.4.2...
>>> so... how do I convert an int to a String in 1.4.2?
>
> Take note:
> <http://java.sun.com/j2se/1.4.2/index.jsp>
>> J2SE 1.4.2 has begun the Sun End of Life (EOL) process.
>
> At over five years old, 1.4.2 is getting long in the tooth. Java 5 is
> almost three, and isn't even the current version.
>
> Dorian wrote:
>> How about:
>>
>> int i = 22;
>> String s = i + "";
>
> Which in turn acts as if it invokes the
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#toString()>
> for an Integer version of the int, which converts that value
>> exactly as if the integer value were given as an argument to the
>> toString(int) method.
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#toString(int)>
>
> This is also how the
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#valueOf(int)>
>
> method works. Any particular reason not to use one of those directly?
>

already tried this. my java must be busted.


import java.lang.String;

class doit {
public static void main(String args[]) {
int i=1;
String s=i.toString();
}
}


C:\prj\java\swing>javac str.java
str.java:6: int cannot be dereferenced
String s=i.toString();
^
----------------------------------------

import java.lang.String;

class str {
public static void main(String args[]) {
int i=1;
Integer i1=new Integer(i);
String s=i1.toString();
}
}


C:\prj\java\swing>java str
Exception in thread "main" java.lang.NoClassDefFoundError: str

> <http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.1.1>
>
>> Any type may be converted to type String by string conversion.
>> A value x of primitive type T is first converted to a reference value
>> as if by giving it as an argument to an appropriate class instance
>> creation expression:
>> ... This reference value is then converted to type String by string
>> conversion.
>> ... the conversion is performed as if by an invocation of the
>> toString method of
> the referenced object with no arguments; but if the result of invoking
> the toString method is null, then the string "null" is used instead.
>


--

------------------------------------
Jim Michaels
for email, edit the address

RAM Disk is *not* an installation method.
 
 
Lew





PostPosted: 2007-6-6 21:47:00 Top

java-programmer >> how do I use String's format? not working on XP Jim Michaels <email***@***.com> wrote in
>> already tried this. my java [sic] must be busted.

Your Java is fine. You need to review the difference between primitive types
and object types, though.

>> import java.lang.String;
>>
>> class doit {
>> public static void main(String args[]) {
>> int i=1;
>> String s=i.toString();
>> }
>> }

Donkey Hot wrote:
> int is not a class, and it has no method toString()
>
> You can use the static method Integer.toString(i) to get a string from
> int.

or String x = String.valueOf( i );

> ----------------------------------------
>> import java.lang.String;
>>
>> class str {
>> public static void main(String args[]) {
>> int i=1;
>> Integer i1=new Integer(i);
>> String s=i1.toString();
>> }
>> }
>> C:\prj\java\swing>java str
>> Exception in thread "main" java.lang.NoClassDefFoundError: str

Donkey Hot wrote:
> But then, no idea why that does not work... I tried it in 1.4.2 JRE and
> it worked fine.

The OP did not compile the class first.

--
Lew

P.S., Donkey Hot, eh? Keep tilting at those windmills!
 
 
Lew





PostPosted: 2007-6-6 21:48:00 Top

java-programmer >> how do I use String's format? not working on XP Jim Michaels <email***@***.com> wrote in
>>> already tried this. my java [sic] must be busted.

>>> import java.lang.String;

You don't need to import java.lang.

--
Lew
 
 
Jim Michaels





PostPosted: 2007-6-7 14:21:00 Top

java-programmer >> how do I use String's format? not working on XP Lew wrote:
> Jim Michaels <email***@***.com> wrote in
>>> already tried this. my java [sic] must be busted.
>
> Your Java is fine. You need to review the difference between primitive
> types and object types, though.
>
>>> import java.lang.String;
>>>
>>> class doit {
>>> public static void main(String args[]) {
>>> int i=1;
>>> String s=i.toString();
>>> }
>>> }
>
> Donkey Hot wrote:
>> int is not a class, and it has no method toString()
>>
>> You can use the static method Integer.toString(i) to get a string from
>> int.
>
> or String x = String.valueOf( i );
>
>> ----------------------------------------
>>> import java.lang.String;
>>>
>>> class str {
>>> public static void main(String args[]) {
>>> int i=1;
>>> Integer i1=new Integer(i);
>>> String s=i1.toString();
>>> }
>>> }
>>> C:\prj\java\swing>java str
>>> Exception in thread "main" java.lang.NoClassDefFoundError: str
>
> Donkey Hot wrote:
>> But then, no idea why that does not work... I tried it in 1.4.2 JRE
>> and it worked fine.
>
> The OP did not compile the class first.
>

how's that? I'm the OP. please be more specific in your reply. I
don't know what you're talking about. I'm still kind of a java
beginner. I thought I *did* compile the class. I ended up with a
str.class file. I did
javac str.java

C:\prj\java\swing>dir
Volume in drive C is wdc160
Volume Serial Number is ACB2-51DD

Directory of C:\prj\java\swing

06/05/2007 02:44 PM <DIR> .
06/05/2007 02:44 PM <DIR> ..
06/05/2007 02:42 PM 363 str.class
06/05/2007 02:42 PM 184 str.java
------------------------------------
Jim Michaels
for email, edit the address

RAM Disk is *not* an installation method.
 
 
Jim Michaels





PostPosted: 2007-6-7 14:44:00 Top

java-programmer >> how do I use String's format? not working on XP Jim Michaels wrote:
> Lew wrote:
>> Jim Michaels <email***@***.com> wrote in
>>>> already tried this. my java [sic] must be busted.
>>
>> Your Java is fine. You need to review the difference between
>> primitive types and object types, though.
>>
>>>> import java.lang.String;
>>>>
>>>> class doit {
>>>> public static void main(String args[]) {
>>>> int i=1;
>>>> String s=i.toString();
>>>> }
>>>> }
>>
>> Donkey Hot wrote:
>>> int is not a class, and it has no method toString()
>>>
>>> You can use the static method Integer.toString(i) to get a string
>>> from int.
>>
>> or String x = String.valueOf( i );
>>
>>> ----------------------------------------
>>>> import java.lang.String;
>>>>
>>>> class str {
>>>> public static void main(String args[]) {
>>>> int i=1;
>>>> Integer i1=new Integer(i);
>>>> String s=i1.toString();
>>>> }
>>>> }
>>>> C:\prj\java\swing>java str
>>>> Exception in thread "main" java.lang.NoClassDefFoundError: str
>>
>> Donkey Hot wrote:
>>> But then, no idea why that does not work... I tried it in 1.4.2 JRE
>>> and it worked fine.
>>
>> The OP did not compile the class first.
>>
>
> how's that? I'm the OP. please be more specific in your reply. I
> don't know what you're talking about. I'm still kind of a java
> beginner. I thought I *did* compile the class. I ended up with a
> str.class file. I did
> javac str.java
>
> C:\prj\java\swing>dir
> Volume in drive C is wdc160
> Volume Serial Number is ACB2-51DD
>
> Directory of C:\prj\java\swing
>
> 06/05/2007 02:44 PM <DIR> .
> 06/05/2007 02:44 PM <DIR> ..
> 06/05/2007 02:42 PM 363 str.class
> 06/05/2007 02:42 PM 184 str.java
> ------------------------------------
> Jim Michaels
> for email, edit the address
>
> RAM Disk is *not* an installation method.

solution is classpath does not include .; in front, as per
http://forum.java.sun.com/thread.jspa?threadID=571464
It used to be the classpath had to be set for things to work, and you
had to include the library path along with it. I don't know how true
this is anymore, or if it's only the case on a UNIX box.
I'm on XP.


--

------------------------------------
Jim Michaels
for email, edit the address

RAM Disk is *not* an installation method.
 
 
Lew





PostPosted: 2007-6-7 21:22:00 Top

java-programmer >> how do I use String's format? not working on XP Jim Michaels wrote:
>>>>> already tried this. my java [sic] must be busted.

Lew wrote:
>>> The OP did not compile the class first.

Jim Michaels wrote:
>> how's that? I'm the OP. please be more specific in your reply. I
>> don't know what you're talking about. I'm still kind of a java

I was confused by the fact that you showed the javac command for your first
example but the java command for your second.

> solution is classpath does not include .; in front, as per
> http://forum.java.sun.com/thread.jspa?threadID=571464

And per the advice in your other thread in this forum.

> It used to be the classpath had to be set for things to work, and you
> had to include the library path along with it. I don't know how true
> this is anymore, or if it's only the case on a UNIX box.
> I'm on XP.

See my advice on the other thread about CLASSPATH versus the "-cp" option to Java.

Bear in mind that absent any overriding setting, CLASSPATH defaults to ",".

Also that you should stop using the default package once you get the hang of
things.

--
Lew
 
 
Jim Michaels





PostPosted: 2007-6-9 9:47:00 Top

java-programmer >> how do I use String's format? not working on XP Lew wrote:
> Jim Michaels wrote:
>>>>>> already tried this. my java [sic] must be busted.
>
> Lew wrote:
>>>> The OP did not compile the class first.
>
> Jim Michaels wrote:
>>> how's that? I'm the OP. please be more specific in your reply. I
>>> don't know what you're talking about. I'm still kind of a java
>
> I was confused by the fact that you showed the javac command for your
> first example but the java command for your second.
>
>> solution is classpath does not include .; in front, as per
>> http://forum.java.sun.com/thread.jspa?threadID=571464
>
> And per the advice in your other thread in this forum.
>
>> It used to be the classpath had to be set for things to work, and you
>> had to include the library path along with it. I don't know how true
>> this is anymore, or if it's only the case on a UNIX box.
>> I'm on XP.
>
> See my advice on the other thread about CLASSPATH versus the "-cp"
> option to Java.
>
> Bear in mind that absent any overriding setting, CLASSPATH defaults to ",".
>
> Also that you should stop using the default package once you get the
> hang of things.
>

what is a "default package"?

--

------------------------------------
Jim Michaels
for email, edit the address

RAM Disk is *not* an installation method.
 
 
Jim Michaels





PostPosted: 2007-6-9 10:24:00 Top

java-programmer >> how do I use String's format? not working on XP Lew wrote:
> Jim Michaels wrote:
>>>>>> already tried this. my java [sic] must be busted.
>
> Lew wrote:
>>>> The OP did not compile the class first.
>
> Jim Michaels wrote:
>>> how's that? I'm the OP. please be more specific in your reply. I
>>> don't know what you're talking about. I'm still kind of a java
>
> I was confused by the fact that you showed the javac command for your
> first example but the java command for your second.
>
>> solution is classpath does not include .; in front, as per
>> http://forum.java.sun.com/thread.jspa?threadID=571464
>
> And per the advice in your other thread in this forum.
>
>> It used to be the classpath had to be set for things to work, and you
>> had to include the library path along with it. I don't know how true
>> this is anymore, or if it's only the case on a UNIX box.
>> I'm on XP.
>
> See my advice on the other thread about CLASSPATH versus the "-cp"
> option to Java.
>
> Bear in mind that absent any overriding setting, CLASSPATH defaults to ",".
>
> Also that you should stop using the default package once you get the
> hang of things.
>

so it's as simple as inserting
package swingdemo;
at the top of the program and that's it? I am not understanding this
core java book really well. it gives no examples and it doesn't really
explain itself.

--

------------------------------------
Jim Michaels
for email, edit the address

RAM Disk is *not* an installation method.
 
 
Lew





PostPosted: 2007-6-9 21:10:00 Top

java-programmer >> how do I use String's format? not working on XP Lew wrote:
>> Also that you should stop using the default package once you get the
>> hang of things.

Jim Michaels wrote:
> what is a "default package"?

Only one of the most basic concepts in Java - it means there is no package for
the class.

Jim Michaels wrote:
> so it's as simple as inserting
> package swingdemo;
> at the top of the program and that's it? I am not understanding this
> core java [sic] book really well. it gives no examples and it doesn't really
> explain itself.

I'm afraid it's not that simple. On a file system, which is what most of us
use for our class files most of the time, the directory structure must match
the package hierarchy, rooted at an element of the class path.

Rephrased, that means if your class path includes the current working
directory (cwd, or "."), and the package (which is supposed to be based on
your Internet domain) is com.jimmichaels.swingdemo, then relative to your cwd
the class file must be in the directory "com/jimmichaels/swingdemo".

Have you even tried to read the tutorials from Sun? They might be a better
place to start than /Core Java/ for you. (It's not "core java" - case matters
in this world.)
<http://java.sun.com/docs/books/tutorial/reallybigindex.html>

Also, GIYF. If you see a term you don't recognize, how about looking it up,
hm? UseNet helps those who help themselves.

--
Lew