Sublime Text cannot recognize NumberFormat - java

I have the following simple code in a (Windows 10) sublime text 3 file NumberFormat.java, to format a double to US currency:
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
public class NumberFormat {
public static void main(String[] args) {
double num = 1000.322;
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
System.out.println("US: " + defaultFormat.format(num));
return;
}
}
When I build, however, the follow errors show up exclusively for NumberFormat.
NumberFormat.java:2: error: NumberFormat is already defined in this compilation unit
import java.text.NumberFormat;
^
NumberFormat.java:10: error: cannot find symbol
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
^
symbol: method getCurrencyInstance()
location: class NumberFormat
NumberFormat.java:11: error: cannot find symbol
System.out.println("US: " + defaultFormat.format(num));
^
symbol: method format(double)
location: variable defaultFormat of type NumberFormat
3 errors
As far as I can see, there are no errors with the code. In addition, this code snippet outputs US: $1000.32 as it should on all other IDEs and text editors. Can someone explain what these errors mean? And why they exclusively occur with sublime text?

The problem is that you have imported a class called NumberFormat into a class with the same name. This is resulting in confusion over which class NumberFormat actually refers to.
(The confusion is in your mind actually. The Java language spec is clear about it. It says that NumberFormat will refer exclusively to your class. The import has not effect. Any compliant Java compiler will make the same interpretation.)
There are two solutions:
Don't import the class. Instead refer to it using its fully qualified name.
Change the name of your class so that it doesn't collide with the class you are importing.
To my mind, the second solution is better. Especially since NumberFormat is not a good name for your class anyway.
And why they exclusively occur with sublime text?
They don't. The code you have written won't compile with any conformant Java compiler.

I think you should modify your class name to a class that is different from the imported class name,or you can change you source to
java.text.NumberFormat defaultFormat = java.text.NumberFormat.getCurrencyInstance();

This is not an issue of Sublime. Sublime simply invokes the javac command from your path. You get the same result if you compile using javac outside the sublime
changing class name or be part of an explicit package name solves the problem

The
import java.text.NumberFormat
conflicts with a type defined in the same file. Changing your class name would resolve the problem.

Related

InOut.readInt() only working in Windows Java Editor

At school, I write Java programs with Windows’ Java Editor (console mode). There, InOut.readInt() (used for user input) works without problems and I don’t have to import anything.
Now, I have a Java homework for the holidays, and I try to write Java programs on my Mac. In online console Java editors, the line InOut.readInt() causes this error:
/IntBetween10And100.java:8: error: cannot find symbol
int input = InOut.readInt("Integer --> ");
^
symbol: variable InOut
location: class IntBetween10And100
1 error
I already tried import lines (placed before the class) like:
import java.*
import java.util.*
import java.util.InOut
import java.io.BufferedStreamReader
import java.util.*;
public class IntBetween10And100 {
public static void main(String args[]) {
int input = InOut.readInt("Integer --> ");
}
}
int input = InOut.readInt("Integer --> ");
should produce the line
Integer -->
but instead, the error message (seen above) appears.
OK, so you are using the "Java-Editor" tool on Windows for writing your Java code.
It turns out that Java-Editor includes a class called InOut as an example class. (You can see it here: http://javaeditor.org/doku.php?id=en:examples).
For various reasons, it is not suitable for use in production code of any kind:
It is not part of the Java SE class library, or any 3rd-party libraries.
It is a class in the default package
It has limited functionality, even compared to the real System.in and System.out
It would interfere with any application or 3rd party library code that uses System.in in the normal way. (It creates its own BufferedReader to wrap System.in. That is liable to capture "type-ahead" input.)
You don't really need to use it for educational purposes either. It is only a wrapper class ...
However, if you want to use InOut outside of the Java-Editor context, you could simply download the source code from the page above and add it to your project. I can't tell you exactly how, but adding classes should be explained in the documentation of the tool you are using now! (If you are serious about learning Java, I would actually recommend that you download and install a real Java JDK and a real Java IDE on your own computer.)
The authors have neglected to include an explicit copyright notice in the InOut.java file. However, the Java-Editor site as a whole is licensed as "CC Attribution-Share Alike 4.0 International".

How to instantiate a class in a Java .class using Visual Studio Code?

I am using Visual Studio Code (version 1.32.3) with Java. I have a .class file called 'RandomGenerator.class' which I want to instantiate from the main. The main.java and RandomGenerator.class are in the same directory.
My statement:
RandomGenerator rdgen = new RandomGenerator(1,1,1,1);
will have an error :RandomGenerator could not be resolved to a type java(16777218)
I tried to include import RandomGenerator; (I don't need but try to see if it is required) - I get an error "RandomGenerator cannot be resolved java(268435846)" at the import statement.
The specification of the constructor given is RandomGenerator​(int seed, double lambda, double mu, double rho)
Appreciate help to make it work in Visual Studio Code. Thanks.
Make sure RandomGenerator.class is on your path!
Import the class in the file that is using it!
If that does not work - is RandomGenerator visible or your class, i.e. public or package private and in the same package?

Getting the hash value of String

I am having the following code to get the hash value of String:
package encryption;
import java.security.MessageDigest;
public class MessageDigestExample {
public static void main(String[] args)throws Exception{
String input = "This is a message";
MessageDigest hash = MessageDigest.getInstance("SHA1");
System.out.println("input : " + input);
hash.update(Utils.toByteArray(input));
System.out.println("digest : " + Utils.toHex(hash.digest()));
} }
I am getting this exception at the moment:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Utils cannot be resolved
Utils cannot be resolved
I added apache-storm-1.2.2 lib, but does not work,
Any help, please!
You are using the (class?) name Utils, without ever importing it.
You probably lack some import what.ever.Utils statement here.
Beyond that, you get a runtime exception because you try to run code that did not compile. Although some IDEs, like eclipse, allow for that, it is in generally a bad idea, especially when you are a newbie to Java. You should always fix all compiler errors before you try to run a class.
You should import the Utils library you are using. If I am reading it correctly, add the following line underneath the package section:
import org.apache.storm.utils.Utils;
This should resolve the library Utils for you

Getting Specific symbols to appear using getCurrencyInstance() in Java

I'm writing a very simple and small example class to try and show how the .getCurrencyInstance() of the NumberFormat class in Java works, but my output is looking a little funky. It seems the class is working, but when it tries to print out the actual symbols for currency (like the Japanese yen symbol or the British pound symbol) I get weird symbols instead. Is there any way to fix this? Is there something I need to import to get these symbols on my computer?
Here's my code:
import java.util.*;
import java.text.*;
public class CurrencyFormatExample
{
public static void main(String[] args)
{
double convert = 9398.9398;
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
System.out.println("American: " + currencyFormat.format(convert));
Locale swedish = new Locale("sv", "SE");
NumberFormat swedishFormat = NumberFormat.getCurrencyInstance(swedish);
System.out.println("Swedish: " + swedishFormat.format(convert));
Locale japanese = new Locale("ja", "JP");
NumberFormat japaneseFormat = NumberFormat.getCurrencyInstance(japanese);
System.out.println("Japanese: " + japaneseFormat.format(convert));
Locale russian = new Locale("ru", "RU");
NumberFormat russianFormat = NumberFormat.getCurrencyInstance(russian);
System.out.println("Russian: " + russianFormat.format(convert));
Locale british = new Locale("en", "GB");
NumberFormat britishFormat = NumberFormat.getCurrencyInstance(british);
System.out.println("British: " + britishFormat.format(convert));
}
}
Here is the output I get with this:
American: $9,398.94
Swedish: 9á398,94 kr
Japanese: ?9,399
Russian: 9á398,94 ???.
British: ú9,398.94
As you can see that's clearly not right. Any way I can remedy this?
In jGRASP: "Settings" > "Font", "Charset" tab. Change "I/O Charset" to UTF-8. In the "Compiler" tab, "Flags / Args" sub-tab, for "FLAGS2" of the "Run" command, add "-Dfile.encoding=UTF-8".
The "file.encoding" change will also change the default encoding files that your program reads and writes. Unfortunately, Java does not provide a way to specify a different charset for normal file I/O and I/O from stdin, stdout, and stderr when they are connected to pipes rather than a console (most IDEs will use pipes). So if you want native encoding to be used by default within your program and still want UTF-8 for I/O to the IDE, there is no way to do it. Thus, it would not be wise for the IDE to automatically add the "file.encoding" flag, unless there is a "Just use UTF-8 for everything" setting.
If you're executing this code in Eclipse, adjust your Window > Preferences > General > Workspace text encoding to UTF-8

Weird error with Locale.getISOCountries()

I'm using this code:
for (final String code : Locale.getISOCountries())
{
//stuff here
}
But on compile I get this error:
[ERROR] Line 21: No source code is available for type java.util.Locale; did you forget to inherit a required module?
And then a stack trace of compiler errors.
I'm doing both of these imports at the beginning of the class:
package com.me.example;
import java.util.Locale;
import java.util.*;
What can be wrong?
In Netbeans i see the autocomplete options and no syntax error for the Locale object...
Something screwy with your setup, the folllowing program works fine for me.
import java.util.*;
import java.util.Locale;
public class Donors {
public static void main (String [] args) {
for (final String code : Locale.getISOCountries()) {
System.out.println (code);
}
}
}
The fact that it's asking for source code leads me to believe that it's trying to compile or run it in some sort of debugging mode. You shouldn't need the source code for java.util.* to compile, that's just bizarre.
See if my simple test program works in your environment, then try looking for something along those lines (debugging options). Final step: compile your code with the baseline javac (not NetBeans).
UPDATE:
Actually, I have found something. If you are creating GWT applications, I don't think java.util.Locale is available on the client side (only the server side). All of the references on the web to this error message point to GWT and its limitations on the client side which are, after all, converted to Javascript goodies, so cannot be expected to support the entire set of Java libraries.
This page here shows how to do i18n on GWT apps and there's no mention of java.util.Locale except on the server side.
Looks like there might be something fishy in your build environment, as Locale.getISOCountries() should work just fine. Try compiling a small test program manually and see if you get the same error.
Definitely try to boil this down to a minimum, three-line program (or so), compile from the command-line, then put that class into your IDE and see if you still get the error, and if not, then change/add one line at a time until you have the original failing program, looking for what causes the problem. I'm thinking maybe some other import in your code is importing a Locale class? Why in the world would it be looking for source code?
See what happens when you compile this from the command-line:
import java.util.*;
public class LocaleTest {
public static void main(String[] args) {
Locale.getISOCountries();
}
}

Categories

Resources