i'm a new to java and just bought this amazing book to start learning with. One of the exercises, it asked me to do this ( it's exactly like in the book ) :
class SimpleDotComTestDrive {
public static void main (String[] args) {
SimpleDotComTestDrive hu = new SimpleDotComTestDrive();
int[] locations = {2,3,4};
hu.setLocationCells(locations);
String userGuess = "2";
String result = hu.checkYourself(userGuess);
String testResult = "failed";
if (result.equals("hit") ) {
testResult = "passed";
}
System.out.println(testResult);
}
}
I compiled this code on Notepad++ which compiles normally for the pass few weeks , until i compiled this code and got this error :
SimpleDotComTestDrive.java:8: error: cannot find symbol
hu.setLocationCells(locations);
^
symbol: method setLocationCells(int[])
location: variable hu of type SimpleDotCom
SimpleDotComTestDrive.java:12: error: cannot find symbol
String result = hu.checkYourself(userGuess);
^
symbol: method checkYourself(String)
location: variable hu of type SimpleDotCom
2 errors
It's pretty annoying since I've searched over the internet for the past few hours and couldn't fix it, please, if you have any idea what's wrong with this then please let me know as soon as possible, thanks in advance !!!
LOOK !!!
i know notepad++ isn't the best IDE but the book recommended me to use simple IDE for learning purposes so please, don't ask me to use other IDE, thanks !
You mentioned that hu.setLocationCells(locations); which means that there needs to be a method setLocationCells() that takes int[] as parameter. Add that method for you to work.
By the way, Notepad++ is not an IDE at all. But yes, it's right to start with this. Good luck.
The variable hu which you are trying to use is declared in the main method in SimpleDotCom class and you are trying to access it from SimpleDotComTestDrive class. So the scope of hu is limited to main method itself.
Try declaring it at the class level, either static or instance variable and try to compile the code.
Related
This problem only started recently, but all my main functions on various programs don't work (they have worked in the past).
They now all return:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at com.avaje.ebeaninternal.server.lib.sql.Prefix.main(Prefix.java:40)
Here is an example of a class that gives this error:
package PACKAGE_NAME_HERE;
public class SomeClass {
public static void main(String[] args) {
System.out.println("Test");
}
}
You're calling method main in com.avaje.ebeaninternal.server.lib.sql.Prefix (which is part of Maven, source can be found here) from somewhere.
Line 40: String m = e(args[0]);
And if you don't pass any command-line arguments, you don't have args[0], and you got ArrayIndexOutOfBoundsException.
That's all I can tell from your question(can't use comments yet, but more clarification/what did you change recently(anything about Maven?) would be nice).
Deleting the project and making a new one fixed this error. Not sure why, maybe I accidentally messed up the setup or something.
This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 9 years ago.
Im a java noob. Basically I am trying to create a program that compares two command line arguments while ignoring case and prints out the lesser of the two strings. Here is what I have so far:
public class CompareStrings
{
public static void main(String[] args)
{
String s1 = new String(args[0]);
String s2 = new String(args[1]);
if ( s1.compareToIgnoreCase(s2) > 0 )
System.out.println(s2);
else if ( s1.compareToIgnoreCase(s2) < 0 )
System.out.println(s1);
else
System.out.println("Both strings are equal.");
}
}
I keep getting the error
Error: Could not find or load main class CompareString
when I try to run it. What am I doing wrong?
"Error: Could not find or load main class CompareString"
Your error message says you couldn't load class "CompareString", but your code says your class name is CompareStrings.
Your class name is wrong.
Your error says
Error: Could not find or load main class CompareString
but the name of class is CompareStrings not CompareString
launch using java CompareStrings
Read this good tutorial on compiling and launching java programs
First of all you are trying to use wrong class, should be CompareStrings and not CompareString.
Second, I would recommend using a nice utility lib for handling command line called Cliche from Google Code site
And third, it would be good to check if the given string is null before you call compareToIgnoreCase on it
as per your error i can gues that you are running CompareString but your class is CompareStrings
So either run java CompareStrings or rename your class to CompareString
If this is the only code you have, save that file as CompareStrings.java not CompareString, and in command prompt javac CompareStrings.java. (to do this you need to configure java). then use java CompareStrings abc cbs to run this. and this will give you out put as abc
I was starting to learn Java. I followed the tutorial on how to install it.
I also checked by typing "javac"(without the quotation marks) in cmd if it works. And yes it gives a whole list of text which means its supposed to work, right?
This is my java code:
class apples{
public static void main(String args[]) {
System.out.printIn("hello youtube");
}
}
I saved it in a folder called 'test' in my c drive.
This is what I typed in cmd:
cd \
dir
now it lists everything in my c drive and one of them is test
cd test
dir
Now it lists everything in test and one of them is 'youtube.java'(the file I named), so I type
javac youtube.java
This doesn't work
This is what it gives me:
youtube.java:3: error: cannot find symbol
System.out.printIn("hello youtube");
symbol: method printIn(string)
location: variable out of type PrintStream
1 error
Can someone help me out with this?
You have a typo in your call. Change
System.out.printIn("hello youtube"); // capital 'I'
to
System.out.println("hello youtube"); // lowercase 'l'
And as has been mentioned already, in Java, the public class in a file must match the filename.
The name of the class must match the filename.
You should use
public class Youtube{
in your code(as class names are capitalized) and call the file Youtube.java.
Also you used In instead of ln.
Use:
System.out.println(
which means "print line to System.out".
I am brand new to Java. I am having an issue compiling a basic java program, and I am trying to understand why. (note that the TextIO class in the code is used in book I am studying to simplify the IO process, I don't believe that is where the issue is) Here is my code:
public class ProcessSales {
public static void main(String[] args) {
String ln;
String tmp;
int i;
int noval;
TextIO.readFile("sales.dat");
while (TextIO.eof() == false){
ln = TextIO.getln();
for (i = 0; i < ln.length(); i++) {
if (ln.charAt(i) == ':'){
tmp = ln.subString(i + 1);
}
} // end line for loop
try {
System.out.printf("%8.2f\n", Double(tmp.trim()));
}
catch (NumberFormatException e) {
noval++;
}
} // end of file while loop
System.out.printf("\nThere were a total of %d cities that didnt have data\n", noval);
} // end of main subroutine
} // end of ProcessSales class
The compile error I get is as follows:
[seldon#PrimeRadiant Exercises]$ javac ProcessSales.java
ProcessSales.java:15: cannot find symbol
symbol : method subString(int)
location: class java.lang.String
tmp = ln.subString(i + 1);
^
ProcessSales.java:20: cannot find symbol
symbol : method Double(java.lang.String)
location: class ProcessSales
System.out.printf("%8.2f\n", Double(tmp.trim()));
^
2 errors
Ive declared ln as a String object. The subString method is straight out of the java api for a String object. I'm not understanding why I'm getting a cannot find symbol compile error, especially if it lists the method signature and location right below the error.
I marked the questions as homework, since I am working out of a textbook, and I am looking to understand the issue, rather than a flat solution. However it is self study, and not part of any actual class (right now).
The great thing about the Java compiler is, it gives you alot of information to use in determining where problems exist in your code. For you, the first problem is here:
tmp = ln.subString(i + 1);
In this case you capitalized a letter that you shouldn't have. It should be:
tmp = ln.substring(i + 1);
Whenever you see compiler output saying 'cannot find symbol' its because the Java compiler could not find a method matching the outputted name, either due to a syntax error or missing dependency. For your second problem, you didn't post the appropriate code, but from the error message I can see you are missing the new keyword.
System.out.printf("%8.2f\n", Double(tmp.trim()));
Should be
System.out.printf("%8.2f\n", new Double(tmp.trim()));
If this is not your first programming language then I would recommend using an IDE like Eclipse, as it will give you auto-completion and syntax checking. It's a great tool for quickly learning the API's for the language. However if Java is your first programming language please do continue without hand-holding, as the hard knocks will cement in the lessons learned, faster.
I haven't verified any of this, I just looked at the source and the error messages.
The first error seems to be a case error. The Java String class does not have a subString method, it has a substring method, note the lowercase s. Reference
The second error would probably be resolved if you used new Double or Double.valueof instead of Double. This is because you are probably trying to construct a new Double object and using new operator or the valueof method in the Double class do this for you. Reference
In Java, method names are case sensitive. Check back with the String API specification for the correct "casing".
Regarding your first error, you have a typo. It should be ln.substring(i+1) not ln.subString(i+1). All source code text in Java is case sensitive.
Regarding your second error, you need to use the new keyword to instantiate the Double object. Without new, the compiler is not looking for the constructor; instead it is looking for a method Double within your ProcessSales class and cannot find it.
This is part of a lab exercise for a course I'm doing, it's not assessable, just a learning exercise. Not sure why but the tut didn't go through it, so I just went through it at home but I'm stuck on the last part.
I'm trying to write a java WSDL client to access http://www.nanonull.com/TimeService/TimeService.asmx?WSDL - I should input UTC+10 to display the current time. Below is the code that I have written:
package time;
class Client {
public static void main(String args[]){
TimeService service = new TimeService();
TimeServiceSoap port= service.getTimeServiceSoap();
String result = port.GetTimeZoneTime("UTC+10");
System.out.println("Time is "+result);
}
}
When I try and compile the code I get the following error:
C:\Program Files\Java\jdk1.6.0_22\bin>javac -d . "c:\Program Files\Java\jdk1.6.0
_22\bin\time\Client.java"
c:\Program Files\Java\jdk1.6.0_22\bin\time\Client.java:13: cannot find symbol
symbol : method GetTimeZoneTimeResponse(java.lang.String)
location: interface time.TimeServiceSoap
String result = port.GetTimeZoneTime("UTC+10");
^
1 error
Any thoughts on what I'm doing wrong?
Did you mean
String result = port.getTimeZoneTime("UTC+10");
with a lowercase g? Java method names are case-sensitive, so it won't recognize the method if you get its letter casing wrong. As per both WSDL's TimeServiceSoap documentation and Java naming conventions, method names are in camel case beginning with a lowercase letter.
What does your TimeServiceSoap look like?
Perhaps you meant to use getTimeZoneTime() (starting with a lower case letter)?