Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.*;

 
Charlie Petrie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just trying to figure out how to use the methods in the Currency class... Here's some code that won't compile:
import java.util.*;
public class CurrencyTest {
String s;
public static void main(String []args){
CurrencyTest ct = new CurrencyTest();
ct.printSymbol();
}
public void printSymbol(){
s = Currency.getSymbol(Locale.FRENCH);
System.out.println(s);
}
}
Can anybody help with this? Thanks.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will help if you show us the error message you get from the compiler.
 
shiv hari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method getSymbol() in the class Currency is not defined as static and hence it cannot be referenced as you are trying to. Only menthods and members that are defined as static can be accessed by a class name.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivc,
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!
 
Charlie Petrie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks shivc...
The thing is is that I tried a few different combinations to get that method to work but to no avail. I realize that this method returns a String but the code I originally had still results in a compiler error. Here's the code I tried and then the error code:
import java.util.*;
public class CurrencyTest {
String s;
String z;
public static void main(String []args){
CurrencyTest ct = new CurrencyTest();
ct.printSymbol();
}
public void printSymbol(){
s = z.getSymbol(Locale.FRENCH);
System.out.println(s);
}
}

C:\Java\Practice Code\CurrencyTest.java:11: cannot resolve symbol
symbol : method getSymbol (java.util.Locale)
location: class java.lang.String
s = z.getSymbol(Locale.FRENCH);
^
1 error
Tool completed with exit code 1

Obviously I just don't have a clue how to implement this methods. This is really bugging me. Can you set me straight here? Thanks.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since z is a String, z.getSymbol() means "call the getSymbol() method of the String class". Only problem is, there isn't any such method in the String class.
 
shiv hari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably what you want can be achieved by the following code. This is just one way of doing it.
import java.util.*;
public class CurrencyTest {
String s;
public static void main(String []args)
{
CurrencyTest ct = new CurrencyTest();
ct.printSymbol();
}
public void printSymbol()
{
Currency c=Currency.getInstance(Locale.US);
s = c.getSymbol(Locale.US);
System.out.println(s);
}
}
The Currency class does not have any constructor, but the static method getInstance() gives you a reference to the Currency object. You need specify your static Locale member as the arguement, then you can use the getSymbol method to get the symbol.
Hope this helps!
 
Charlie Petrie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks shiv hari. That's exactly what I wanted.
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been following this thread because I am a beginner, too and wanted to know how it was done. However, my program compiles, but generates an error and my questions are:
1. Exception in class main NoClassDefFoundError: java/util/Currency at CurrencyTest.printSymbol ,etc. Any guesses as to what might be wrong?
2. How does one pipe the errors to either a file or something which will let me cut and paste the results? I tried:
java CurrencyTest > results.txt but I ended up with an empty file.
Thanks! Janet
 
Rodney Woodruff
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janet,
Can we see the code that is causing the error?
 
shiv hari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janet,
1. You are getting the excpetion of ClassNotFound maybe because you are not using the right JDK version. The Currency class is present in the 1.4 and not in 1.2. Just check your JDK version by giving the command java -version on the command prompt.
2. You get the errors redirected to a file using 2> instead of >. The pipe > will redirect only the output of command to the file whereas the 2> pipe will redirect the errors.
Hope this helps!
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiv Hari-
1. I tried 2 > and I ended up with the same results (no output to the file - just an empty file).
2. Now, here was something "amusing" , I looked at the version (I used java -version) and you are right, it looks like I have 1.3 but the amusing part is that I downloaded the 1.4 sdk (file name: j2sdk-1_4_0_01-windows-i586.exe, with file version of 2.11.15, dates of the readme files were 04/20/02) and even re-installed it just now and it still shows up as 1.3!?!? Release notes from the re-install state 1.4! Here's a sample from the readme file:
README
Java(TM) 2 SDK, Standard Edition
Version 1.4.0
I hesistated downloading a beta version from Sun's site - any recommendations?
Thanks for your help!
=====
To Rodney - I just cut and pasted Shiv's code which he posted on this message thread.
Thanks!
[ August 15, 2002: Message edited by: Janet Wilson ]
 
Greg Ostravich
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Previous to you installing the 1.4 SDK did you have the 1.3 installed? Did you uninstall the 1.3 before installing the 1.4? Also check your classpath. On my box I had a problem because it was using the JRE (C:\Program Files\JavaSoft\JRE\1.3.1_02) instead of the JDK.
 
shiv hari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janet,
did u try 2>, I mean 2 and > without a space. If you give 2 followed by a space and then >, it will result in a blank output file.
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg,
I wish it was this simple. I did not have the JRE installed before. According to the documentation I read, I would not want a classpath to be set. My PATH is set to the location of j2...\bin and I am able to compile the code.
Does that help?
Janet
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiv,
Thanks for clarifying that. The piping now works but so much for my program... Here are my results:
java.lang.NoClassDefFoundError: java/util/Currency
at CurrencyTest.printSymbol(CurrencyTest.java:13)
at CurrencyTest.main(CurrencyTest.java:8)
Exception in thread "main"
Thanks a bunch! Janet
 
shiv hari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janet,
Looks like you are still struggling with the right JDK version. Do you have ur 1.4 jar in the classpath ? Ensure this, then I am sure the program will run. I tried it myself

shiv.
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiv,
You are right, I am struggling with the version and as a result, I made a new thread to help out with the issue.
As for the classpath, I have been playing around with that. According to Sun I shouldn't have to include it, however, what was intesting is that
when I did NOT have the classpath my program execution resulted in:
java.lang.NoClassDefFoundError: java/util/Currency
at CurrencyTest.printSymbol(CurrencyTest.java:13)
at CurrencyTest.main(CurrencyTest.java:8)
Exception in thread "main"
when I DID have the classpath defined as C:\j2sdk1.4.0_01, my program execution resulted in:
java.lang.NoClassDefFoundError: CurrencyTest
Exception in thread "main"
So, your help has been MOST appreciated by this newbie as it helped me diagnose where my problems exist.
Thanks! Janet
 
Greg Ostravich
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janet,
I think you're almost there. It does make sense that you'd need the classpath. I'm not sure why you wouldn't want to do that.
Now if you just add "." to the classpath for the current directory everything should work.
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg,
Thanks for your words of encouragement.
You said I needed to include a period. My classpath is set (w/in the environment variables section of control panel/system) to:
C:\j2sdk1.4.0_01
Where does the period go? You also said something about current directory - I'm unsure what you are referring to.
I have a separate directory off the C root called: janets-java. I do my program compilations and executions from there.
Sorry for being such a dim bulb. What should be a very easy concept I'm made a mountain out of it!
Thanks for putting up with this! Janet
 
Bryant Constant
Greenhorn
Posts: 10
Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is the CLASSPATH to use:
.;C:\j2sdk1.4.0_01
The dot (.) represents the current directory in Windows and Unix. The CLASSPATH (or any other PATH) is a list of directories to look at to resolve which class (or program) to use. The directories are separated by semilcolons ( ; ). The list is evaluated from left to right.
[ August 16, 2002: Message edited by: Bryant Constant ]
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
VICTORY!!!
I do not have to pull my hair out anymore! Thanks to everyone I am back in business and a much happier person.
THANKS to everyone who helped out!!! Janet
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic