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.
Related
I have been searching for about 2 hours now and cannot find a way to catch and ignore an incompatible type error in Java. This is compile-time error caused by attempting to assign a float value to an integer. I know why this error is occurring (narrow conversion) and specifically WANT this error to occur as it is for a demonstration of the error itself (needed for a course I am in), but in attempting to catch the error using a try-catch statement I am having no luck.
I know you should specify the error type you are catching, but for the purpose of this code it is okay if I take the generic route here, because everything is pretty well contained and very simple. I would never use this kind of error checking for anything more involved than the purposes of this project, but if you know the exact error type causing the message "error: incompatible types: possible lossy conversion from float to int" to be displayed when attempting to compile, I'll use that error type. I looked through multiple sites and could not find the exact type of this error (such as IOException, for example).
Here is the code I am trying to execute:
import java.lang.*;
public class AssignTests {
int integer = 10;
float floater = 1234.1234f;
public static void main(String[] args){
AssignTests test = new AssignTests();
try{
test.integer = test.floater;
}
catch (Exception t)
{
System.out.println("An integer cannot be assigned the value of a float.");
}
}
}
When I go to compile this code I still see the error message of incompatible types despite trying to ignore this issue via the try-catch statement, and after searching for this long on how to resolve this am pretty stumped.
Any idea on how to get this to compile and give me the output message I am looking for?
Use this instead of normal conversion
test.integer = (int) test.floater;
You cannot try-catch for compilation errors.
The incompatible error: incompatible types: possible lossy conversion from is a compile-time error and therefore this is an impossible task without typecasting or other workarounds.
Thanks to PM 77-1 for the response (in comments), figured I'd write it as an answer to close the question.
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.
/********************************************
// Problems.java
\\
// Provide lots of syntax errors for the user to correct.
\\
********************************************/
public class Problems
{
public static void main(String[] args)
{
System.out.println ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println ("This program still has lots of problems,");
System.out.print ("but" + "," + " if it prints this, you fixed them all.");
System.out.println (" *** Hurray! ***");
System.out.println ("!!!!!!!!!!!!!!!"+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
I'm quite new here and am wondering what is wrong with the code. All it does is show text, but when I compile it, I get an error "Class, interface, or enum expected".
It says there's something wrong with the last line (where the last } is). I'm not sure what is wrong here.
This assignment is for my computer science class. We were supposed to fix a few syntax errors (I do have bad eyes, so I might have not seen things like semicolons) and compile it and run, but it won't compile.
I was using JCreator 4.5 for this. I also tried compiling it at school and with a DOS prompt. HELP!
Based on the error you provided
'Problems', are only accepted if annotation processing is explicitly requested (according to the DOS/cmd prompt)
your command line arguments may be wrong, there is a post about this error in the following article
http://docs.oracle.com/javase/tutorial/getStarted/problems/
The exact qoute being
Class names, 'HelloWorldApp', are only accepted if annotation
processing is explicitly requested
If you receive this error, you forgot to include the .java suffix when
compiling the program. Remember, the command is javac
HelloWorldApp.java not javac HelloWorldApp.
This is how I compile your code. Try to recreate my steps and say if you get any errors.
Since this class is not in any specific package I created Problems.java file in d:\java tests. This file contains
/********************************************
//Problems.java
\\
//Provide lots of syntax errors for the user to correct.
\\
********************************************/
public class Problems
{
public static void main(String[] args)
{
System.out.println ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println (" This program still has lots of problems,");
System.out.println ("but" + "," + " if it prints this, you fixed them all.");
System.out.println (" *** Hurray! ***");
System.out.println ("!!!!!!!!!!!!!!!"+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
I used UTF-8 coding.
Next in console I went to d:\java tests> and from this directory I used
d:\java tests>javac Problems.java
(you need to add .java suffix in class name here) which successfully created Problems.class file. To run main method from this class I used
d:\java tests>java Problems
(no suffixes here) which printed
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This program still has lots of problems,
but, if it prints this, you fixed them all.
*** Hurray! ***
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I am trying to write a sample program that can call use the main method of "SequenceFilesFromDirectory", which aims to convert a set of files into sequence file format.
public class TestSequenceFileConverter {
public static void main(String args[]){
String inputDir = "inputDir";
String outputDir = "outoutDir";
SequenceFilesFromDirectory.main(new String[] {"--input",
inputDir.toString(), "--output", outputDir.toString(), "--chunkSize",
"64", "--charset",Charsets.UTF_8.name()});
}
}
But the Eclipse tells me that what I did was wrong with the following error message
Multiple markers at this line
- Syntax error on token "main", = expected after this
token
- Syntax error on token(s), misplaced construct(s)
- SequenceFilesFromDirectory cannot be resolved
I think I did not use this method correctly, but I don't know how to fix it? Thanks a lot.
The following is how the SequenceFilesFromDirectory defines. The API link for SequenceFilesFromDirectory is http://search-lucene.com/jd/mahout/utils/org/apache/mahout/text/SequenceFilesFromDirectory.html
My guess is that you're missing an import line from the first section of your file:
import org.apache.mahout.text.SequenceFilesFromDirectory;
I think your purpose for using SequenceFilesFromDirectory is to convert doc files to sequence files. If so, better to call the run()/runSequential()/runMapReduce() methods ater creating an object of SequenceFilesFromDirectory, because SequenceFilesFromDirectory.main() internally calls haddop ToolRunner.run() method for processing.
Whereas the run methods of SequenceFilesFromDirectory do the actual processings.
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)?